#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum BigNumberError {
InvalidFormat(String),
}
impl std::fmt::Display for BigNumberError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BigNumberError::InvalidFormat(s) => write!(f, "invalid number format: {s}"),
}
}
}
impl std::error::Error for BigNumberError {}
fn is_valid_big_integer(s: &str) -> bool {
if s.is_empty() {
return false;
}
let mut chars = s.chars();
match chars.next() {
Some('-') | Some('+') | Some('0'..='9') => {}
_ => return false,
}
chars.all(|c| c.is_ascii_digit())
}
fn is_valid_big_decimal(s: &str) -> bool {
if s.is_empty() {
return false;
}
s.chars()
.all(|c| matches!(c, '0'..='9' | '-' | '+' | '.' | 'e' | 'E'))
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BigInteger(String);
impl Default for BigInteger {
fn default() -> Self {
Self("0".to_string())
}
}
impl std::str::FromStr for BigInteger {
type Err = BigNumberError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if !is_valid_big_integer(s) {
return Err(BigNumberError::InvalidFormat(s.to_string()));
}
Ok(Self(s.to_string()))
}
}
impl AsRef<str> for BigInteger {
fn as_ref(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BigDecimal(String);
impl Default for BigDecimal {
fn default() -> Self {
Self("0.0".to_string())
}
}
impl std::str::FromStr for BigDecimal {
type Err = BigNumberError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if !is_valid_big_decimal(s) {
return Err(BigNumberError::InvalidFormat(s.to_string()));
}
Ok(Self(s.to_string()))
}
}
impl AsRef<str> for BigDecimal {
fn as_ref(&self) -> &str {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
#[test]
fn big_integer_basic() {
let bi = BigInteger::from_str("12345678901234567890").unwrap();
assert_eq!(bi.as_ref(), "12345678901234567890");
}
#[test]
fn big_integer_default() {
let bi = BigInteger::default();
assert_eq!(bi.as_ref(), "0");
}
#[test]
fn big_decimal_basic() {
let bd = BigDecimal::from_str("123.456789").unwrap();
assert_eq!(bd.as_ref(), "123.456789");
}
#[test]
fn big_decimal_default() {
let bd = BigDecimal::default();
assert_eq!(bd.as_ref(), "0.0");
}
#[test]
fn big_integer_negative() {
let bi = BigInteger::from_str("-12345").unwrap();
assert_eq!(bi.as_ref(), "-12345");
}
#[test]
fn big_decimal_scientific() {
let bd = BigDecimal::from_str("1.23e10").unwrap();
assert_eq!(bd.as_ref(), "1.23e10");
let bd = BigDecimal::from_str("1.23E-10").unwrap();
assert_eq!(bd.as_ref(), "1.23E-10");
}
#[test]
fn big_integer_rejects_json_injection() {
assert!(BigInteger::from_str("123, \"injected\": true").is_err());
assert!(BigInteger::from_str("123}").is_err());
assert!(BigInteger::from_str("{\"hacked\": 1}").is_err());
assert!(BigInteger::from_str("123\"").is_err());
assert!(BigInteger::from_str("123\\n456").is_err());
}
#[test]
fn big_decimal_rejects_json_injection() {
assert!(BigDecimal::from_str("123.45, \"injected\": true").is_err());
assert!(BigDecimal::from_str("123.45}").is_err());
assert!(BigDecimal::from_str("{\"hacked\": 1.0}").is_err());
}
#[test]
fn big_integer_rejects_invalid_chars() {
assert!(BigInteger::from_str("abc").is_err());
assert!(BigInteger::from_str("123abc").is_err());
assert!(BigInteger::from_str("12 34").is_err());
assert!(BigInteger::from_str("").is_err());
}
#[test]
fn big_integer_rejects_decimal_and_scientific() {
assert!(BigInteger::from_str("123.45").is_err());
assert!(BigInteger::from_str("123.0").is_err());
assert!(BigInteger::from_str("1e10").is_err());
assert!(BigInteger::from_str("1E10").is_err());
assert!(BigInteger::from_str("1.23e10").is_err());
}
#[test]
fn big_integer_accepts_signs() {
assert!(BigInteger::from_str("+123").is_ok());
assert!(BigInteger::from_str("-123").is_ok());
assert_eq!(BigInteger::from_str("+123").unwrap().as_ref(), "+123");
}
#[test]
fn big_decimal_rejects_invalid_chars() {
assert!(BigDecimal::from_str("abc").is_err());
assert!(BigDecimal::from_str("123.45abc").is_err());
assert!(BigDecimal::from_str("12.34 56").is_err());
assert!(BigDecimal::from_str("").is_err());
}
}