feat: use nanoid

This commit is contained in:
GyDi 2022-03-02 00:56:05 +08:00
parent 19c7b59883
commit 1880363aeb
No known key found for this signature in database
GPG Key ID: 1C95E0D3467B3084
3 changed files with 21 additions and 2 deletions

10
src-tauri/Cargo.lock generated
View File

@ -461,6 +461,7 @@ dependencies = [
"dunce",
"log",
"log4rs",
"nanoid",
"port_scanner",
"reqwest",
"serde",
@ -2052,6 +2053,15 @@ dependencies = [
"twoway",
]
[[package]]
name = "nanoid"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ffa00dec017b5b1a8b7cf5e2c008bfda1aa7e0697ac1508b491fdf2622fb4d8"
dependencies = [
"rand 0.8.5",
]
[[package]]
name = "native-tls"
version = "0.2.8"

View File

@ -16,6 +16,7 @@ tauri-build = { version = "1.0.0-rc.3", features = [] }
anyhow = "1.0"
dirs = "4.0.0"
dunce = "1.0.2"
nanoid = "0.4.0"
chrono = "0.4.19"
serde_json = "1.0"
serde_yaml = "0.8"

View File

@ -1,3 +1,4 @@
use nanoid::nanoid;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};
@ -8,10 +9,17 @@ pub fn get_now() -> usize {
.as_secs() as _
}
const ALPHABET: [char; 62] = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B',
'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z',
];
/// generate the uid
pub fn get_uid(prefix: &str) -> String {
let now = get_now();
format!("{prefix}{now}")
let id = nanoid!(11, &ALPHABET);
format!("{prefix}{id}")
}
/// parse the string