Skip to content

Commit

Permalink
feat: Add FencingToken::generate method (#110)
Browse files Browse the repository at this point in the history
Signed-off-by: Vaibhav Rabber <[email protected]>
  • Loading branch information
vrongmeal authored Dec 13, 2024
1 parent 2c78da9 commit 1dfefe8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ hyper = "1.5.0"
hyper-util = "0.1.10"
prost = "0.13.3"
prost-types = "0.13.3"
rand = "0.8.5"
regex = "1.11.1"
secrecy = "0.8.0"
sync_docs = "0.2.0"
Expand Down
11 changes: 11 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::{ops::Deref, str::FromStr, sync::OnceLock, time::Duration};

use bytes::Bytes;
use rand::{distributions::Uniform, Rng};
use regex::Regex;
use sync_docs::sync_docs;

Expand Down Expand Up @@ -849,6 +850,16 @@ impl FencingToken {
Ok(Self(bytes))
}
}

/// Generate a random fencing token with `n` bytes.
pub fn generate(n: usize) -> Result<Self, ConvertError> {
Self::new(
rand::thread_rng()
.sample_iter(&Uniform::new_inclusive(0, u8::MAX))
.take(n)
.collect::<Bytes>(),
)
}
}

impl AsRef<Bytes> for FencingToken {
Expand Down

0 comments on commit 1dfefe8

Please sign in to comment.