feat(server):增加工具函数。
This commit is contained in:
		| @@ -1,3 +1,5 @@ | ||||
| use rand::{seq::SliceRandom, thread_rng}; | ||||
|  | ||||
| pub fn empty_to_none<S: Into<String>>(s: S) -> Option<String> { | ||||
|     let s = s.into(); | ||||
|     if s.is_empty() { | ||||
| @@ -6,3 +8,18 @@ pub fn empty_to_none<S: Into<String>>(s: S) -> Option<String> { | ||||
|         Some(s) | ||||
|     } | ||||
| } | ||||
|  | ||||
| const RAND_LINCENSE_STR_SRC: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | ||||
|  | ||||
| pub fn generate_product_license_id() -> String { | ||||
|     let choices = RAND_LINCENSE_STR_SRC.as_bytes(); | ||||
|     let mut rng = thread_rng(); | ||||
|     let mut code: Vec<String> = vec![]; | ||||
|     while code.len() < 10 { | ||||
|         match choices.choose(&mut rng) { | ||||
|             Some(c) => code.push(String::from_utf8(vec![*c]).unwrap()), | ||||
|             None => continue, | ||||
|         } | ||||
|     } | ||||
|     code.join("") | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user