Skip to content

Commit

Permalink
feat: use nanoid
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi committed Mar 1, 2022
1 parent 19c7b59 commit 1880363
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 10 additions & 2 deletions src-tauri/src/utils/help.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use nanoid::nanoid;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};

Expand All @@ -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
Expand Down

0 comments on commit 1880363

Please sign in to comment.