mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-23 17:07:32 +00:00
75 lines
1.4 KiB
Rust
75 lines
1.4 KiB
Rust
use std::{
|
|
ops::Range,
|
|
time::{Duration, SystemTime},
|
|
};
|
|
|
|
use arrayvec::ArrayString;
|
|
use rand::{Rng, seq::SliceRandom, thread_rng};
|
|
|
|
pub fn shuffle<T>(vec: &mut [T]) {
|
|
let mut rng = thread_rng();
|
|
vec.shuffle(&mut rng);
|
|
}
|
|
|
|
pub fn string(length: usize) -> String {
|
|
thread_rng()
|
|
.sample_iter(&rand::distributions::Alphanumeric)
|
|
.take(length)
|
|
.map(char::from)
|
|
.collect()
|
|
}
|
|
|
|
#[inline]
|
|
pub fn string_array<const LENGTH: usize>() -> ArrayString<LENGTH> {
|
|
let mut ret = ArrayString::<LENGTH>::new();
|
|
thread_rng()
|
|
.sample_iter(&rand::distributions::Alphanumeric)
|
|
.take(LENGTH)
|
|
.map(char::from)
|
|
.for_each(|c| ret.push(c));
|
|
|
|
ret
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn truncate_string(mut str: String, range: Range<u64>) -> String {
|
|
let len = thread_rng()
|
|
.gen_range(range)
|
|
.try_into()
|
|
.unwrap_or(usize::MAX);
|
|
|
|
if let Some((i, _)) = str.char_indices().nth(len) {
|
|
str.truncate(i);
|
|
}
|
|
|
|
str
|
|
}
|
|
|
|
#[inline]
|
|
#[must_use]
|
|
pub fn truncate_str(str: &str, range: Range<u64>) -> &str {
|
|
let len = thread_rng()
|
|
.gen_range(range)
|
|
.try_into()
|
|
.unwrap_or(usize::MAX);
|
|
|
|
str.char_indices()
|
|
.nth(len)
|
|
.map(|(i, _)| str.split_at(i).0)
|
|
.unwrap_or(str)
|
|
}
|
|
|
|
#[inline]
|
|
#[must_use]
|
|
pub fn time_from_now_secs(range: Range<u64>) -> SystemTime {
|
|
SystemTime::now()
|
|
.checked_add(secs(range))
|
|
.expect("range does not overflow SystemTime")
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn secs(range: Range<u64>) -> Duration {
|
|
let mut rng = thread_rng();
|
|
Duration::from_secs(rng.gen_range(range))
|
|
}
|