use std::fmt::Write;
use crate::{constants::COMMA_CHAR, InlineString};
#[macro_export]
macro_rules! sub_unsigned {
($arg_lhs: expr, $arg_rhs: expr) => {
$arg_lhs.saturating_sub($arg_rhs)
};
}
#[macro_export]
macro_rules! add_unsigned {
($arg_lhs: expr, $arg_rhs: expr) => {
$arg_lhs.saturating_add($arg_rhs)
};
}
#[macro_export]
macro_rules! mul_unsigned {
($arg_lhs: expr, $arg_rhs: expr) => {
$arg_lhs.saturating_mul($arg_rhs)
};
}
#[macro_export]
macro_rules! inc_unsigned {
($arg_lhs: expr) => {
$arg_lhs = $arg_lhs.saturating_add(1);
};
($arg_lhs: expr, by: $arg_amount: expr) => {
$arg_lhs = $arg_lhs.saturating_add($arg_amount);
};
($arg_lhs: expr, max: $arg_max: expr) => {
$arg_lhs = std::cmp::min($arg_lhs.saturating_add(1), $arg_max);
};
($arg_lhs: expr, by: $arg_amount:expr, max: $arg_max: expr) => {
$arg_lhs = std::cmp::min($arg_lhs.saturating_add($arg_amount), $arg_max);
};
}
#[macro_export]
macro_rules! dec_unsigned {
($arg_lhs: expr) => {
$arg_lhs = $arg_lhs.saturating_sub(1);
};
($arg_lhs: expr, by: $arg_amount: expr) => {
$arg_lhs = $arg_lhs.saturating_sub($arg_amount);
};
}
#[must_use]
pub fn format_as_kilobytes_with_commas(bytes_size: usize) -> InlineString {
if bytes_size < 1024 {
let mut acc = format_with_commas(bytes_size);
write!(acc, " B").ok();
acc
} else {
let kilobytes = bytes_size / 1024;
let mut acc = format_with_commas(kilobytes);
write!(acc, " KB").ok();
acc
}
}
#[test]
fn test_format_as_kilobytes_with_commas() {
{
let bytes_size = 512;
let result = format_as_kilobytes_with_commas(bytes_size);
assert_eq!(result, "512 B");
}
{
let bytes_size = 1024;
let result = format_as_kilobytes_with_commas(bytes_size);
assert_eq!(result, "1 KB");
}
{
let bytes_size = 2048;
let result = format_as_kilobytes_with_commas(bytes_size);
assert_eq!(result, "2 KB");
}
}
#[must_use]
pub fn format_with_commas(num: usize) -> InlineString {
let num_str = num.to_string();
let ir = {
let mut acc = InlineString::with_capacity(num_str.len());
for (digit_position, ch) in num_str.chars().rev().enumerate() {
let should_add_comma = digit_position > 0 && digit_position.is_multiple_of(3);
if should_add_comma {
acc.push(COMMA_CHAR);
}
acc.push(ch);
}
acc
};
{
let mut acc = InlineString::with_capacity(ir.len());
for ch in ir.chars().rev() {
acc.push(ch);
}
acc
}
}
#[test]
fn test_format_with_commas() {
{
let num = 5;
let result = format_with_commas(num);
assert_eq!(result, "5");
}
{
let num = 12;
let result = format_with_commas(num);
assert_eq!(result, "12");
}
{
let num = 123;
let result = format_with_commas(num);
assert_eq!(result, "123");
}
{
let num = 987_654;
let result = format_with_commas(num);
assert_eq!(result, "987,654");
}
{
let num = 123_456_789;
let result = format_with_commas(num);
assert_eq!(result, "123,456,789");
}
}
pub trait LossyConvertToByte {
#[must_use]
fn to_u8_lossy(self) -> u8;
}
impl LossyConvertToByte for f64 {
#[allow(
clippy::cast_sign_loss,
clippy::cast_lossless,
clippy::cast_possible_truncation
)]
fn to_u8_lossy(self) -> u8 { self as u8 }
}
impl LossyConvertToByte for f32 {
#[allow(
clippy::cast_sign_loss,
clippy::cast_lossless,
clippy::cast_possible_truncation
)]
fn to_u8_lossy(self) -> u8 { self as u8 }
}
impl LossyConvertToByte for i32 {
#[allow(
clippy::cast_sign_loss,
clippy::cast_lossless,
clippy::cast_possible_truncation
)]
fn to_u8_lossy(self) -> u8 { self as u8 }
}
impl LossyConvertToByte for u32 {
#[allow(
clippy::cast_sign_loss,
clippy::cast_lossless,
clippy::cast_possible_truncation
)]
fn to_u8_lossy(self) -> u8 { self as u8 }
}
impl LossyConvertToByte for usize {
#[allow(
clippy::cast_sign_loss,
clippy::cast_lossless,
clippy::cast_possible_truncation
)]
fn to_u8_lossy(self) -> u8 { self as u8 }
}
impl LossyConvertToByte for u64 {
#[allow(
clippy::cast_sign_loss,
clippy::cast_lossless,
clippy::cast_possible_truncation
)]
fn to_u8_lossy(self) -> u8 { self as u8 }
}
impl LossyConvertToByte for u16 {
#[allow(
clippy::cast_sign_loss,
clippy::cast_lossless,
clippy::cast_possible_truncation
)]
fn to_u8_lossy(self) -> u8 { self as u8 }
}
impl LossyConvertToByte for i16 {
#[allow(
clippy::cast_sign_loss,
clippy::cast_lossless,
clippy::cast_possible_truncation
)]
fn to_u8_lossy(self) -> u8 { self as u8 }
}
impl LossyConvertToByte for i8 {
#[allow(
clippy::cast_sign_loss,
clippy::cast_lossless,
clippy::cast_possible_truncation
)]
fn to_u8_lossy(self) -> u8 { self as u8 }
}