feat(server):增加工具函数。

This commit is contained in:
徐涛 2024-04-03 13:14:47 +08:00
parent 7a0964a24f
commit dbc3d25fec

View File

@ -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("")
}