diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 111805a..ddfe91e 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -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" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 861b30e..98c71d0 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -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" diff --git a/src-tauri/src/utils/help.rs b/src-tauri/src/utils/help.rs index 3ddf1bd..133b0a5 100644 --- a/src-tauri/src/utils/help.rs +++ b/src-tauri/src/utils/help.rs @@ -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