Documentation
/// 运算代送
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);
  }
}