zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use core::str::FromStr;
use rust_decimal::prelude::*;

use crate::core::error2::Error;
use crate::core::error2::Result;

pub fn string_to_number<T>(input: &str) -> Result<T>
where
    T: std::str::FromStr,
    <T as FromStr>::Err: std::fmt::Display,
{
    let result = input.trim().replace('_', "").parse::<T>().map_err(|e| {
        let msg = format!("Parse `{}` to number failed", input);

        Error::throw(msg, Some(e.to_string()))
    })?;

    Ok(result)
}

pub fn string_to_decimal(str: &str) -> Result<Decimal> {
    Decimal::from_str(&remove_trailing_zeros(str)).map_err(|e| {
        let msg = format!("Parse `{}` to decimal failed", str);

        Error::throw(msg, Some(e.to_string()))
    })
}

pub fn remove_trailing_zeros(input: &str) -> String {
    // 使用 chars 方法迭代字符串的 Unicode 字符
    let trimmed: String = input
        .chars()
        .rev()
        .skip_while(|&c| c == '0') // 跳过末尾的零
        .collect::<String>()
        .chars()
        .rev() // 恢复字符串顺序
        .collect();

    // 如果字符串末尾是小数点,也去除
    if trimmed.ends_with('.') {
        return trimmed[..trimmed.len() - 1].to_string();
    }

    trimmed
}

pub trait ToDecimal {
    fn to_decimal(&self) -> rust_decimal::Decimal;
}

impl ToDecimal for sqlx::types::BigDecimal {
    fn to_decimal(&self) -> rust_decimal::Decimal {
        rust_decimal::Decimal::from_str(&self.to_string()).unwrap()
    }
}