feat(server):增加工具函数。
This commit is contained in:
parent
7a0964a24f
commit
dbc3d25fec
|
@ -1,3 +1,5 @@
|
||||||
|
use rand::{seq::SliceRandom, thread_rng};
|
||||||
|
|
||||||
pub fn empty_to_none<S: Into<String>>(s: S) -> Option<String> {
|
pub fn empty_to_none<S: Into<String>>(s: S) -> Option<String> {
|
||||||
let s = s.into();
|
let s = s.into();
|
||||||
if s.is_empty() {
|
if s.is_empty() {
|
||||||
|
@ -6,3 +8,18 @@ pub fn empty_to_none<S: Into<String>>(s: S) -> Option<String> {
|
||||||
Some(s)
|
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("")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user