e_utils/algorithm/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/// 运算代送
pub mod iter;
/// 随机集合
pub mod random;
cfg_base64! {
/// base64
pub mod base64;
}
/// Macro for various algorithm operations
///
/// This macro supports multiple random operations, including generating random booleans,
/// random types, random arrays, RGB colors, nanoids, and more.
///
/// # Examples
///
/// ```
/// use e_utils::algorithm;
///
/// fn main() {
/// // 生成随机布尔值
/// let random_bool = algorithm!();
/// println!("随机布尔值: {}", random_bool);
///
/// // 生成随机 u32
/// let random_u32: u32 = algorithm!(#u32);
/// println!("随机 u32: {}", random_u32);
///
/// // 生成随机数组
/// let random_array: [u8; 5] = algorithm!([u8; 5]);
/// println!("随机数组: {:?}", random_array);
///
/// // 生成随机 RGB 颜色
/// let rgb = algorithm!(rgb 0, 255);
/// println!("随机 RGB: {:?}", rgb);
///
/// // 生成默认长度(21)的 nanoid
/// let default_nanoid = algorithm!(nanoid);
/// println!("默认 nanoid: {}", default_nanoid);
///
/// // 生成自定义长度的 nanoid
/// let custom_nanoid = algorithm!(nanoid 10);
/// println!("自定义 nanoid: {}", custom_nanoid);
///
/// // 生成指定范围内的随机数
/// let random_range = algorithm!(0..100);
/// println!("随机数 (0-99): {}", random_range);
///
/// // 生成负数范围内的随机数
/// let negative_range = algorithm!((-50)..50);
/// println!("随机数 (-50 到 49): {}", negative_range);
///
/// // 生成自定义字母表的 nanoid
/// let custom_alphabet_nanoid = algorithm!(nanoid 8, &"abcdef123456".chars().collect::<Vec<char>>());
/// println!("自定义字母表 nanoid: {}", custom_alphabet_nanoid);
///
/// // 使用不安全模式生成 nanoid
/// let unsafe_nanoid = algorithm!(nanoid unsafe 15);
/// println!("不安全模式 nanoid: {}", unsafe_nanoid);
///
/// // 使用不安全模式和自定义字母表生成 nanoid
/// let unsafe_custom_nanoid = algorithm!(nanoid unsafe 12, &"ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().collect::<Vec<char>>());
/// println!("不安全模式自定义字母表 nanoid: {}", unsafe_custom_nanoid);
/// }
/// ```
#[macro_export]
#[cfg(feature = "algorithm")]
macro_rules! algorithm {
() => {
$crate::algorithm::random::Rand::Safe.random_bool()
};
(#$t:ty) => {
$crate::algorithm::random::Rand::Safe.random_type::<$t>()
};
([$t:ty; $size:expr]) => {
$crate::algorithm::random::Rand::Safe.random_type::<[$t; $size]>()
};
(rgb $min:tt, $max:tt) => {
$crate::algorithm::random::Rand::Safe.rgb_range($min, $max)
};
(nanoid) => {
$crate::algorithm::random::Rand::Safe.nanoid_format(&$crate::algorithm::random::NID_SAFE, 21)
};
// generate
(nanoid $size:tt) => {
$crate::algorithm::random::Rand::Safe.nanoid_format(&$crate::algorithm::random::NID_SAFE, $size)
};
// custom
(nanoid $size:tt, $alphabet:expr) => {
$crate::algorithm::random::Rand::Safe.nanoid_format($alphabet, $size)
};
// unsafe
(nanoid unsafe $size:tt) => {
$crate::algorithm::random::Rand::UnSafe
.nanoid_format(&$crate::algorithm::random::NID_SAFE, $size)
};
// unsafe custom
(nanoid unsafe $size:tt, $alphabet:expr) => {
$crate::algorithm::random::Rand::UnSafe.nanoid_format($alphabet, $size)
};
($size:tt) => {
$crate::algorithm::random::Rand::Safe.random_range(0..$size)
};
($min:tt..$max:tt) => {
$crate::algorithm::random::Rand::Safe.random_range($min..$max)
};
}
#[cfg(test)]
#[cfg(feature = "algorithm")]
mod tests {
use crate::algorithm;
#[test]
fn test_random_bool() {
let _ = algorithm!(); // 确保编译通过
}
#[test]
fn test_random_type() {
let _: u32 = algorithm!(#u32);
}
#[test]
fn test_random_array() {
let _: [u8; 5] = algorithm!([u8; 5]);
}
#[test]
fn test_rgb_range() {
let _ = algorithm!(rgb 0, 255);
}
#[test]
fn test_nanoid() {
let id = algorithm!(nanoid);
assert_eq!(id.len(), 21);
}
#[test]
fn test_nanoid_custom_size() {
let id = algorithm!(nanoid 10);
assert_eq!(id.len(), 10);
}
#[test]
fn test_random_range() {
let num = algorithm!(0..100);
assert!(num >= 0 && num < 100);
}
}