Uses "ParseDecimalError" naming just like "ParseFloatError"

Improves and simplifies tests
pull/360/head
Tpt 2 years ago committed by Thomas Tanon
parent ea0b4e22e7
commit f969a66d05
  1. 14
      lib/oxsdatatypes/src/boolean.rs
  2. 845
      lib/oxsdatatypes/src/date_time.rs
  3. 205
      lib/oxsdatatypes/src/decimal.rs
  4. 35
      lib/oxsdatatypes/src/double.rs
  5. 258
      lib/oxsdatatypes/src/duration.rs
  6. 35
      lib/oxsdatatypes/src/float.rs
  7. 53
      lib/oxsdatatypes/src/integer.rs
  8. 2
      lib/oxsdatatypes/src/lib.rs
  9. 8
      lib/oxsdatatypes/src/parser.rs

@ -88,19 +88,13 @@ mod tests {
#[test]
fn from_str() -> Result<(), ParseBoolError> {
assert_eq!(Boolean::from(true), Boolean::from_str("true")?);
assert_eq!(Boolean::from(true), Boolean::from_str("1")?);
assert_eq!(Boolean::from(false), Boolean::from_str("false")?);
assert_eq!(Boolean::from(false), Boolean::from_str("0")?);
assert_eq!(Boolean::from_str("true")?.to_string(), "true");
assert_eq!(Boolean::from_str("1")?.to_string(), "true");
assert_eq!(Boolean::from_str("false")?.to_string(), "false");
assert_eq!(Boolean::from_str("0")?.to_string(), "false");
Ok(())
}
#[test]
fn to_string() {
assert_eq!("true", Boolean::from(true).to_string());
assert_eq!("false", Boolean::from(false).to_string());
}
#[test]
fn from_integer() {
assert_eq!(Boolean::from(false), Integer::from(0).into());

File diff suppressed because it is too large Load Diff

@ -358,10 +358,10 @@ impl TryFrom<Decimal> for Integer {
}
impl FromStr for Decimal {
type Err = DecimalParseError;
type Err = ParseDecimalError;
/// Parses decimals lexical mapping
fn from_str(input: &str) -> Result<Self, DecimalParseError> {
fn from_str(input: &str) -> Result<Self, ParseDecimalError> {
// (\+|-)?([0-9]+(\.[0-9]*)?|\.[0-9]+)
let input = input.as_bytes();
if input.is_empty() {
@ -515,7 +515,7 @@ impl Neg for Decimal {
/// An error when parsing a [`Decimal`].
#[derive(Debug, Clone)]
pub struct DecimalParseError {
pub struct ParseDecimalError {
kind: DecimalParseErrorKind,
}
@ -527,20 +527,20 @@ enum DecimalParseErrorKind {
UnexpectedEnd,
}
const PARSE_OVERFLOW: DecimalParseError = DecimalParseError {
const PARSE_OVERFLOW: ParseDecimalError = ParseDecimalError {
kind: DecimalParseErrorKind::Overflow,
};
const PARSE_UNDERFLOW: DecimalParseError = DecimalParseError {
const PARSE_UNDERFLOW: ParseDecimalError = ParseDecimalError {
kind: DecimalParseErrorKind::Underflow,
};
const PARSE_UNEXPECTED_CHAR: DecimalParseError = DecimalParseError {
const PARSE_UNEXPECTED_CHAR: ParseDecimalError = ParseDecimalError {
kind: DecimalParseErrorKind::UnexpectedChar,
};
const PARSE_UNEXPECTED_END: DecimalParseError = DecimalParseError {
const PARSE_UNEXPECTED_END: ParseDecimalError = ParseDecimalError {
kind: DecimalParseErrorKind::UnexpectedEnd,
};
impl fmt::Display for DecimalParseError {
impl fmt::Display for ParseDecimalError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.kind {
DecimalParseErrorKind::Overflow => write!(f, "Value overflow"),
@ -551,9 +551,9 @@ impl fmt::Display for DecimalParseError {
}
}
impl Error for DecimalParseError {}
impl Error for ParseDecimalError {}
impl From<DecimalOverflowError> for DecimalParseError {
impl From<DecimalOverflowError> for ParseDecimalError {
fn from(_: DecimalOverflowError) -> Self {
Self {
kind: DecimalParseErrorKind::Overflow,
@ -578,46 +578,43 @@ mod tests {
use super::*;
#[test]
fn new() {
assert_eq!(Decimal::new(1, 0).unwrap().to_string(), "1");
assert_eq!(Decimal::new(1, 1).unwrap().to_string(), "0.1");
assert_eq!(Decimal::new(10, 0).unwrap().to_string(), "10");
assert_eq!(Decimal::new(10, 1).unwrap().to_string(), "1");
assert_eq!(Decimal::new(10, 2).unwrap().to_string(), "0.1");
fn new() -> Result<(), ParseDecimalError> {
assert_eq!(Decimal::new(1, 0)?.to_string(), "1");
assert_eq!(Decimal::new(1, 1)?.to_string(), "0.1");
assert_eq!(Decimal::new(10, 0)?.to_string(), "10");
assert_eq!(Decimal::new(10, 1)?.to_string(), "1");
assert_eq!(Decimal::new(10, 2)?.to_string(), "0.1");
Ok(())
}
#[test]
fn from_str() {
assert_eq!(Decimal::from_str("210").unwrap().to_string(), "210");
assert_eq!(Decimal::from_str("1000").unwrap().to_string(), "1000");
assert_eq!(Decimal::from_str("-1.23").unwrap().to_string(), "-1.23");
fn from_str() -> Result<(), ParseDecimalError> {
assert_eq!(Decimal::from_str("210")?.to_string(), "210");
assert_eq!(Decimal::from_str("1000")?.to_string(), "1000");
assert_eq!(Decimal::from_str("-1.23")?.to_string(), "-1.23");
assert_eq!(
Decimal::from_str("12678967.543233").unwrap().to_string(),
Decimal::from_str("12678967.543233")?.to_string(),
"12678967.543233"
);
assert_eq!(
Decimal::from_str("+100000.00").unwrap().to_string(),
"100000"
);
assert_eq!(Decimal::from_str("0.1220").unwrap().to_string(), "0.122");
assert_eq!(Decimal::from_str(".12200").unwrap().to_string(), "0.122");
assert_eq!(Decimal::from_str("1.").unwrap().to_string(), "1");
assert_eq!(Decimal::from_str("01.0").unwrap().to_string(), "1");
assert_eq!(Decimal::from_str("0").unwrap().to_string(), "0");
assert_eq!(
Decimal::from_str(&Decimal::MAX.to_string()).unwrap(),
Decimal::MAX
);
assert_eq!(Decimal::from_str("+100000.00")?.to_string(), "100000");
assert_eq!(Decimal::from_str("0.1220")?.to_string(), "0.122");
assert_eq!(Decimal::from_str(".12200")?.to_string(), "0.122");
assert_eq!(Decimal::from_str("1.")?.to_string(), "1");
assert_eq!(Decimal::from_str("1.0")?.to_string(), "1");
assert_eq!(Decimal::from_str("01.0")?.to_string(), "1");
assert_eq!(Decimal::from_str("0")?.to_string(), "0");
assert_eq!(Decimal::from_str("-0")?.to_string(), "0");
assert_eq!(Decimal::from_str(&Decimal::MAX.to_string())?, Decimal::MAX);
assert_eq!(
Decimal::from_str(
&Decimal::MIN
.checked_add(Decimal::step())
.unwrap()
.to_string()
)
.unwrap(),
)?,
Decimal::MIN.checked_add(Decimal::step()).unwrap()
);
Ok(())
}
#[test]
@ -654,100 +651,75 @@ mod tests {
}
#[test]
fn mul() {
fn mul() -> Result<(), ParseDecimalError> {
assert_eq!(
Decimal::from_str("1")
.unwrap()
.checked_mul(Decimal::from_str("-1").unwrap()),
Some(Decimal::from_str("-1").unwrap())
Decimal::from_str("1")?.checked_mul(Decimal::from_str("-1")?),
Some(Decimal::from_str("-1")?)
);
assert_eq!(
Decimal::from_str("1000")
.unwrap()
.checked_mul(Decimal::from_str("1000").unwrap()),
Some(Decimal::from_str("1000000").unwrap())
Decimal::from_str("1000")?.checked_mul(Decimal::from_str("1000")?),
Some(Decimal::from_str("1000000")?)
);
assert_eq!(
Decimal::from_str("0.1")
.unwrap()
.checked_mul(Decimal::from_str("0.01").unwrap()),
Some(Decimal::from_str("0.001").unwrap())
Decimal::from_str("0.1")?.checked_mul(Decimal::from_str("0.01")?),
Some(Decimal::from_str("0.001")?)
);
Ok(())
}
#[test]
fn div() {
fn div() -> Result<(), ParseDecimalError> {
assert_eq!(
Decimal::from_str("1")
.unwrap()
.checked_div(Decimal::from_str("1").unwrap()),
Some(Decimal::from_str("1").unwrap())
Decimal::from_str("1")?.checked_div(Decimal::from_str("1")?),
Some(Decimal::from_str("1")?)
);
assert_eq!(
Decimal::from_str("100")
.unwrap()
.checked_div(Decimal::from_str("10").unwrap()),
Some(Decimal::from_str("10").unwrap())
Decimal::from_str("100")?.checked_div(Decimal::from_str("10")?),
Some(Decimal::from_str("10")?)
);
assert_eq!(
Decimal::from_str("10")
.unwrap()
.checked_div(Decimal::from_str("100").unwrap()),
Some(Decimal::from_str("0.1").unwrap())
Decimal::from_str("10")?.checked_div(Decimal::from_str("100")?),
Some(Decimal::from_str("0.1")?)
);
Ok(())
}
#[test]
fn round() {
assert_eq!(Decimal::from_str("10").unwrap().round(), Decimal::from(10));
assert_eq!(
Decimal::from_str("-10").unwrap().round(),
Decimal::from(-10)
);
assert_eq!(Decimal::from_str("2.5").unwrap().round(), Decimal::from(3));
assert_eq!(
Decimal::from_str("2.4999").unwrap().round(),
Decimal::from(2)
);
assert_eq!(
Decimal::from_str("-2.5").unwrap().round(),
Decimal::from(-2)
);
fn round() -> Result<(), ParseDecimalError> {
assert_eq!(Decimal::from_str("10")?.round(), Decimal::from(10));
assert_eq!(Decimal::from_str("-10")?.round(), Decimal::from(-10));
assert_eq!(Decimal::from_str("2.5")?.round(), Decimal::from(3));
assert_eq!(Decimal::from_str("2.4999")?.round(), Decimal::from(2));
assert_eq!(Decimal::from_str("-2.5")?.round(), Decimal::from(-2));
assert_eq!(Decimal::from(i64::MIN).round(), Decimal::from(i64::MIN));
assert_eq!(Decimal::from(i64::MAX).round(), Decimal::from(i64::MAX));
Ok(())
}
#[test]
fn ceil() {
assert_eq!(Decimal::from_str("10").unwrap().ceil(), Decimal::from(10));
assert_eq!(Decimal::from_str("-10").unwrap().ceil(), Decimal::from(-10));
assert_eq!(Decimal::from_str("10.5").unwrap().ceil(), Decimal::from(11));
assert_eq!(
Decimal::from_str("-10.5").unwrap().ceil(),
Decimal::from(-10)
);
fn ceil() -> Result<(), ParseDecimalError> {
assert_eq!(Decimal::from_str("10")?.ceil(), Decimal::from(10));
assert_eq!(Decimal::from_str("-10")?.ceil(), Decimal::from(-10));
assert_eq!(Decimal::from_str("10.5")?.ceil(), Decimal::from(11));
assert_eq!(Decimal::from_str("-10.5")?.ceil(), Decimal::from(-10));
assert_eq!(Decimal::from(i64::MIN).ceil(), Decimal::from(i64::MIN));
assert_eq!(Decimal::from(i64::MAX).ceil(), Decimal::from(i64::MAX));
Ok(())
}
#[test]
fn floor() {
assert_eq!(Decimal::from_str("10").unwrap().ceil(), Decimal::from(10));
assert_eq!(Decimal::from_str("-10").unwrap().ceil(), Decimal::from(-10));
assert_eq!(
Decimal::from_str("10.5").unwrap().floor(),
Decimal::from(10)
);
assert_eq!(
Decimal::from_str("-10.5").unwrap().floor(),
Decimal::from(-11)
);
fn floor() -> Result<(), ParseDecimalError> {
assert_eq!(Decimal::from_str("10")?.ceil(), Decimal::from(10));
assert_eq!(Decimal::from_str("-10")?.ceil(), Decimal::from(-10));
assert_eq!(Decimal::from_str("10.5")?.floor(), Decimal::from(10));
assert_eq!(Decimal::from_str("-10.5")?.floor(), Decimal::from(-11));
assert_eq!(Decimal::from(i64::MIN).floor(), Decimal::from(i64::MIN));
assert_eq!(Decimal::from(i64::MAX).floor(), Decimal::from(i64::MAX));
Ok(())
}
#[test]
fn to_be_bytes() {
fn to_be_bytes() -> Result<(), ParseDecimalError> {
assert_eq!(
Decimal::from_be_bytes(Decimal::from(i64::MIN).to_be_bytes()),
Decimal::from(i64::MIN)
@ -765,9 +737,10 @@ mod tests {
Decimal::from(0)
);
assert_eq!(
Decimal::from_be_bytes(Decimal::from_str("0.01").unwrap().to_be_bytes()),
Decimal::from_str("0.01").unwrap()
Decimal::from_be_bytes(Decimal::from_str("0.01")?.to_be_bytes()),
Decimal::from_str("0.01")?
);
Ok(())
}
#[test]
@ -777,18 +750,18 @@ mod tests {
}
#[test]
fn from_float() {
fn from_float() -> Result<(), ParseDecimalError> {
assert_eq!(
Decimal::try_from(Float::from(0.)).unwrap(),
Decimal::from_str("0").unwrap()
Decimal::try_from(Float::from(0.)).ok(),
Some(Decimal::from_str("0")?)
);
assert_eq!(
Decimal::try_from(Float::from(-0.)).unwrap(),
Decimal::from_str("0.").unwrap()
Decimal::try_from(Float::from(-0.)).ok(),
Some(Decimal::from_str("0.")?)
);
assert_eq!(
Decimal::try_from(Float::from(-123.5)).unwrap(),
Decimal::from_str("-123.5").unwrap()
Decimal::try_from(Float::from(-123.5)).ok(),
Some(Decimal::from_str("-123.5")?)
);
assert!(Decimal::try_from(Float::from(f32::NAN)).is_err());
assert!(Decimal::try_from(Float::from(f32::INFINITY)).is_err());
@ -798,31 +771,32 @@ mod tests {
assert!(
Decimal::try_from(Float::from(1672507302466.))
.unwrap()
.checked_sub(Decimal::from_str("1672507302466").unwrap())
.checked_sub(Decimal::from_str("1672507302466")?)
.unwrap()
.abs()
< Decimal::from(1_000_000)
);
Ok(())
}
#[test]
fn from_double() {
fn from_double() -> Result<(), ParseDecimalError> {
assert_eq!(
Decimal::try_from(Double::from(0.)).unwrap(),
Decimal::from_str("0").unwrap()
Decimal::try_from(Double::from(0.)).ok(),
Some(Decimal::from_str("0")?)
);
assert_eq!(
Decimal::try_from(Double::from(-0.)).unwrap(),
Decimal::from_str("0").unwrap()
Decimal::try_from(Double::from(-0.)).ok(),
Some(Decimal::from_str("0")?)
);
assert_eq!(
Decimal::try_from(Double::from(-123.1)).unwrap(),
Decimal::from_str("-123.1").unwrap()
Decimal::try_from(Double::from(-123.1)).ok(),
Some(Decimal::from_str("-123.1")?)
);
assert!(
Decimal::try_from(Double::from(1672507302466.))
.unwrap()
.checked_sub(Decimal::from_str("1672507302466").unwrap())
.checked_sub(Decimal::from_str("1672507302466")?)
.unwrap()
.abs()
< Decimal::from(1)
@ -832,5 +806,6 @@ mod tests {
assert!(Decimal::try_from(Double::from(f64::NEG_INFINITY)).is_err());
assert!(Decimal::try_from(Double::from(f64::MIN)).is_err());
assert!(Decimal::try_from(Double::from(f64::MAX)).is_err());
Ok(())
}
}

@ -278,19 +278,28 @@ mod tests {
#[test]
fn from_str() -> Result<(), ParseFloatError> {
assert!(Double::from(f64::NAN).is_identical_with(&Double::from_str("NaN")?));
assert_eq!(Double::from(f64::INFINITY), Double::from_str("INF")?);
assert_eq!(Double::from(f64::INFINITY), Double::from_str("+INF")?);
assert_eq!(Double::from(f64::NEG_INFINITY), Double::from_str("-INF")?);
assert_eq!(Double::from(0.), Double::from_str("0.0E0")?);
assert_eq!(Double::from(-0.), Double::from_str("-0.0E0")?);
assert_eq!(Double::from_str("NaN")?.to_string(), "NaN");
assert_eq!(Double::from_str("INF")?.to_string(), "INF");
assert_eq!(Double::from_str("+INF")?.to_string(), "INF");
assert_eq!(Double::from_str("-INF")?.to_string(), "-INF");
assert_eq!(Double::from_str("0.0E0")?.to_string(), "0");
assert_eq!(Double::from_str("-0.0E0")?.to_string(), "-0");
assert_eq!(Double::from_str("0.1e1")?.to_string(), "1");
assert_eq!(Double::from_str("-0.1e1")?.to_string(), "-1");
assert_eq!(Double::from_str("1.e1")?.to_string(), "10");
assert_eq!(Double::from_str("-1.e1")?.to_string(), "-10");
assert_eq!(Double::from_str("1")?.to_string(), "1");
assert_eq!(Double::from_str("-1")?.to_string(), "-1");
assert_eq!(Double::from_str("1.")?.to_string(), "1");
assert_eq!(Double::from_str("-1.")?.to_string(), "-1");
assert_eq!(
Double::from_str(&f64::MIN.to_string()).unwrap(),
Double::from(f64::MIN)
);
assert_eq!(
Double::from_str(&f64::MAX.to_string()).unwrap(),
Double::from(f64::MAX)
);
Ok(())
}
#[test]
fn to_string() {
assert_eq!("NaN", Double::from(f64::NAN).to_string());
assert_eq!("INF", Double::from(f64::INFINITY).to_string());
assert_eq!("-INF", Double::from(f64::NEG_INFINITY).to_string());
}
}

@ -596,75 +596,46 @@ mod tests {
use super::*;
#[test]
fn from_str() {
fn from_str() -> Result<(), XsdParseError> {
let min = Duration::new(
i64::MIN + 1,
Decimal::MIN.checked_add(Decimal::step()).unwrap(),
);
let max = Duration::new(i64::MAX, Decimal::MAX);
assert_eq!(
YearMonthDuration::from_str("P1Y").unwrap().to_string(),
"P1Y"
);
assert_eq!(Duration::from_str("P1Y").unwrap().to_string(), "P1Y");
assert_eq!(
YearMonthDuration::from_str("P1M").unwrap().to_string(),
"P1M"
);
assert_eq!(Duration::from_str("P1M").unwrap().to_string(), "P1M");
assert_eq!(DayTimeDuration::from_str("P1D").unwrap().to_string(), "P1D");
assert_eq!(Duration::from_str("P1D").unwrap().to_string(), "P1D");
assert_eq!(
DayTimeDuration::from_str("PT1H").unwrap().to_string(),
"PT1H"
);
assert_eq!(Duration::from_str("PT1H").unwrap().to_string(), "PT1H");
assert_eq!(
DayTimeDuration::from_str("PT1M").unwrap().to_string(),
"PT1M"
);
assert_eq!(Duration::from_str("PT1M").unwrap().to_string(), "PT1M");
assert_eq!(
DayTimeDuration::from_str("PT1.1S").unwrap().to_string(),
"PT1.1S"
);
assert_eq!(Duration::from_str("PT1.1S").unwrap().to_string(), "PT1.1S");
assert_eq!(
YearMonthDuration::from_str("-P1Y").unwrap().to_string(),
"-P1Y"
);
assert_eq!(Duration::from_str("-P1Y").unwrap().to_string(), "-P1Y");
assert_eq!(
YearMonthDuration::from_str("-P1M").unwrap().to_string(),
"-P1M"
);
assert_eq!(Duration::from_str("-P1M").unwrap().to_string(), "-P1M");
assert_eq!(
DayTimeDuration::from_str("-P1D").unwrap().to_string(),
"-P1D"
);
assert_eq!(Duration::from_str("-P1D").unwrap().to_string(), "-P1D");
assert_eq!(
DayTimeDuration::from_str("-PT1H").unwrap().to_string(),
"-PT1H"
);
assert_eq!(Duration::from_str("-PT1H").unwrap().to_string(), "-PT1H");
assert_eq!(
DayTimeDuration::from_str("-PT1M").unwrap().to_string(),
"-PT1M"
);
assert_eq!(Duration::from_str("-PT1M").unwrap().to_string(), "-PT1M");
assert_eq!(
DayTimeDuration::from_str("-PT1.1S").unwrap().to_string(),
"-PT1.1S"
);
assert_eq!(
Duration::from_str("-PT1.1S").unwrap().to_string(),
"-PT1.1S"
);
assert_eq!(Duration::from_str(&max.to_string()).unwrap(), max);
assert_eq!(Duration::from_str(&min.to_string()).unwrap(), min);
assert_eq!(YearMonthDuration::from_str("P1Y")?.to_string(), "P1Y");
assert_eq!(Duration::from_str("P1Y")?.to_string(), "P1Y");
assert_eq!(YearMonthDuration::from_str("P1M")?.to_string(), "P1M");
assert_eq!(Duration::from_str("P1M")?.to_string(), "P1M");
assert_eq!(DayTimeDuration::from_str("P1D")?.to_string(), "P1D");
assert_eq!(Duration::from_str("P1D")?.to_string(), "P1D");
assert_eq!(DayTimeDuration::from_str("PT1H")?.to_string(), "PT1H");
assert_eq!(Duration::from_str("PT1H")?.to_string(), "PT1H");
assert_eq!(DayTimeDuration::from_str("PT1M")?.to_string(), "PT1M");
assert_eq!(Duration::from_str("PT1M")?.to_string(), "PT1M");
assert_eq!(DayTimeDuration::from_str("PT1.1S")?.to_string(), "PT1.1S");
assert_eq!(Duration::from_str("PT1.1S")?.to_string(), "PT1.1S");
assert_eq!(YearMonthDuration::from_str("-P1Y")?.to_string(), "-P1Y");
assert_eq!(Duration::from_str("-P1Y")?.to_string(), "-P1Y");
assert_eq!(YearMonthDuration::from_str("-P1M")?.to_string(), "-P1M");
assert_eq!(Duration::from_str("-P1M")?.to_string(), "-P1M");
assert_eq!(DayTimeDuration::from_str("-P1D")?.to_string(), "-P1D");
assert_eq!(Duration::from_str("-P1D")?.to_string(), "-P1D");
assert_eq!(DayTimeDuration::from_str("-PT1H")?.to_string(), "-PT1H");
assert_eq!(Duration::from_str("-PT1H")?.to_string(), "-PT1H");
assert_eq!(DayTimeDuration::from_str("-PT1M")?.to_string(), "-PT1M");
assert_eq!(Duration::from_str("-PT1M")?.to_string(), "-PT1M");
assert_eq!(DayTimeDuration::from_str("-PT1S")?.to_string(), "-PT1S");
assert_eq!(Duration::from_str("-PT1S")?.to_string(), "-PT1S");
assert_eq!(DayTimeDuration::from_str("-PT1.1S")?.to_string(), "-PT1.1S");
assert_eq!(Duration::from_str("-PT1.1S")?.to_string(), "-PT1.1S");
assert_eq!(Duration::from_str(&max.to_string())?, max);
assert_eq!(Duration::from_str(&min.to_string())?, min);
assert_eq!(Duration::from_str("PT0H")?.to_string(), "PT0S");
assert_eq!(Duration::from_str("-PT0H")?.to_string(), "PT0S");
assert_eq!(YearMonthDuration::from_str("P0Y")?.to_string(), "P0M");
assert_eq!(DayTimeDuration::from_str("PT0H")?.to_string(), "PT0S");
Ok(())
}
#[test]
@ -678,149 +649,126 @@ mod tests {
}
#[test]
fn equals() {
fn equals() -> Result<(), XsdParseError> {
assert_eq!(
YearMonthDuration::from_str("P1Y").unwrap(),
YearMonthDuration::from_str("P12M").unwrap()
YearMonthDuration::from_str("P1Y")?,
YearMonthDuration::from_str("P12M")?
);
assert_eq!(
YearMonthDuration::from_str("P1Y").unwrap(),
Duration::from_str("P12M").unwrap()
YearMonthDuration::from_str("P1Y")?,
Duration::from_str("P12M")?
);
assert_eq!(
Duration::from_str("P1Y").unwrap(),
YearMonthDuration::from_str("P12M").unwrap()
Duration::from_str("P1Y")?,
YearMonthDuration::from_str("P12M")?
);
assert_eq!(Duration::from_str("P1Y")?, Duration::from_str("P12M")?);
assert_eq!(
Duration::from_str("P1Y").unwrap(),
Duration::from_str("P12M").unwrap()
DayTimeDuration::from_str("PT24H")?,
DayTimeDuration::from_str("P1D")?
);
assert_eq!(
DayTimeDuration::from_str("PT24H").unwrap(),
DayTimeDuration::from_str("P1D").unwrap()
DayTimeDuration::from_str("PT24H")?,
Duration::from_str("P1D")?
);
assert_eq!(
DayTimeDuration::from_str("PT24H").unwrap(),
Duration::from_str("P1D").unwrap()
Duration::from_str("PT24H")?,
DayTimeDuration::from_str("P1D")?
);
assert_eq!(Duration::from_str("PT24H")?, Duration::from_str("P1D")?);
assert_ne!(Duration::from_str("P1Y")?, Duration::from_str("P365D")?);
assert_eq!(Duration::from_str("P0Y")?, Duration::from_str("P0D")?);
assert_ne!(Duration::from_str("P1Y")?, Duration::from_str("P365D")?);
assert_eq!(Duration::from_str("P2Y")?, Duration::from_str("P24M")?);
assert_eq!(Duration::from_str("P10D")?, Duration::from_str("PT240H")?);
assert_eq!(
Duration::from_str("PT24H").unwrap(),
DayTimeDuration::from_str("P1D").unwrap()
Duration::from_str("P2Y0M0DT0H0M0S")?,
Duration::from_str("P24M")?
);
assert_eq!(
Duration::from_str("PT24H").unwrap(),
Duration::from_str("P1D").unwrap()
);
assert_ne!(
Duration::from_str("P1Y").unwrap(),
Duration::from_str("P365D").unwrap()
);
assert_eq!(
Duration::from_str("P0Y").unwrap(),
Duration::from_str("P0D").unwrap()
);
assert_ne!(
Duration::from_str("P1Y").unwrap(),
Duration::from_str("P365D").unwrap()
);
assert_eq!(
Duration::from_str("P2Y").unwrap(),
Duration::from_str("P24M").unwrap()
);
assert_eq!(
Duration::from_str("P10D").unwrap(),
Duration::from_str("PT240H").unwrap()
);
assert_eq!(
Duration::from_str("P2Y0M0DT0H0M0S").unwrap(),
Duration::from_str("P24M").unwrap()
);
assert_eq!(
Duration::from_str("P0Y0M10D").unwrap(),
Duration::from_str("PT240H").unwrap()
);
assert_ne!(
Duration::from_str("P1M").unwrap(),
Duration::from_str("P30D").unwrap()
Duration::from_str("P0Y0M10D")?,
Duration::from_str("PT240H")?
);
assert_ne!(Duration::from_str("P1M")?, Duration::from_str("P30D")?);
Ok(())
}
#[test]
fn years() {
assert_eq!(Duration::from_str("P20Y15M").unwrap().years(), 21);
assert_eq!(Duration::from_str("-P15M").unwrap().years(), -1);
assert_eq!(Duration::from_str("-P2DT15H").unwrap().years(), 0);
fn years() -> Result<(), XsdParseError> {
assert_eq!(Duration::from_str("P20Y15M")?.years(), 21);
assert_eq!(Duration::from_str("-P15M")?.years(), -1);
assert_eq!(Duration::from_str("-P2DT15H")?.years(), 0);
Ok(())
}
#[test]
fn months() {
assert_eq!(Duration::from_str("P20Y15M").unwrap().months(), 3);
assert_eq!(Duration::from_str("-P20Y18M").unwrap().months(), -6);
assert_eq!(Duration::from_str("-P2DT15H0M0S").unwrap().months(), 0);
fn months() -> Result<(), XsdParseError> {
assert_eq!(Duration::from_str("P20Y15M")?.months(), 3);
assert_eq!(Duration::from_str("-P20Y18M")?.months(), -6);
assert_eq!(Duration::from_str("-P2DT15H0M0S")?.months(), 0);
Ok(())
}
#[test]
fn days() {
assert_eq!(Duration::from_str("P3DT10H").unwrap().days(), 3);
assert_eq!(Duration::from_str("P3DT55H").unwrap().days(), 5);
assert_eq!(Duration::from_str("P3Y5M").unwrap().days(), 0);
fn days() -> Result<(), XsdParseError> {
assert_eq!(Duration::from_str("P3DT10H")?.days(), 3);
assert_eq!(Duration::from_str("P3DT55H")?.days(), 5);
assert_eq!(Duration::from_str("P3Y5M")?.days(), 0);
Ok(())
}
#[test]
fn hours() {
assert_eq!(Duration::from_str("P3DT10H").unwrap().hours(), 10);
assert_eq!(Duration::from_str("P3DT12H32M12S").unwrap().hours(), 12);
assert_eq!(Duration::from_str("PT123H").unwrap().hours(), 3);
assert_eq!(Duration::from_str("-P3DT10H").unwrap().hours(), -10);
fn hours() -> Result<(), XsdParseError> {
assert_eq!(Duration::from_str("P3DT10H")?.hours(), 10);
assert_eq!(Duration::from_str("P3DT12H32M12S")?.hours(), 12);
assert_eq!(Duration::from_str("PT123H")?.hours(), 3);
assert_eq!(Duration::from_str("-P3DT10H")?.hours(), -10);
Ok(())
}
#[test]
fn minutes() {
assert_eq!(Duration::from_str("P3DT10H").unwrap().minutes(), 0);
assert_eq!(Duration::from_str("-P5DT12H30M").unwrap().minutes(), -30);
fn minutes() -> Result<(), XsdParseError> {
assert_eq!(Duration::from_str("P3DT10H")?.minutes(), 0);
assert_eq!(Duration::from_str("-P5DT12H30M")?.minutes(), -30);
Ok(())
}
#[test]
fn seconds() {
fn seconds() -> Result<(), XsdParseError> {
assert_eq!(
Duration::from_str("P3DT10H12.5S").unwrap().seconds(),
Decimal::from_str("12.5").unwrap()
Duration::from_str("P3DT10H12.5S")?.seconds(),
Decimal::from_str("12.5")?
);
assert_eq!(
Duration::from_str("-PT256S").unwrap().seconds(),
Decimal::from_str("-16.0").unwrap()
Duration::from_str("-PT256S")?.seconds(),
Decimal::from_str("-16.0")?
);
Ok(())
}
#[test]
fn add() {
fn add() -> Result<(), XsdParseError> {
assert_eq!(
Duration::from_str("P2Y11M")
.unwrap()
.checked_add(Duration::from_str("P3Y3M").unwrap()),
Some(Duration::from_str("P6Y2M").unwrap())
Duration::from_str("P2Y11M")?.checked_add(Duration::from_str("P3Y3M")?),
Some(Duration::from_str("P6Y2M")?)
);
assert_eq!(
Duration::from_str("P2DT12H5M")
.unwrap()
.checked_add(Duration::from_str("P5DT12H").unwrap()),
Some(Duration::from_str("P8DT5M").unwrap())
Duration::from_str("P2DT12H5M")?.checked_add(Duration::from_str("P5DT12H")?),
Some(Duration::from_str("P8DT5M")?)
);
Ok(())
}
#[test]
fn sub() {
fn sub() -> Result<(), XsdParseError> {
assert_eq!(
Duration::from_str("P2Y11M")
.unwrap()
.checked_sub(Duration::from_str("P3Y3M").unwrap()),
Some(Duration::from_str("-P4M").unwrap())
Duration::from_str("P2Y11M")?.checked_sub(Duration::from_str("P3Y3M")?),
Some(Duration::from_str("-P4M")?)
);
assert_eq!(
Duration::from_str("P2DT12H")
.unwrap()
.checked_sub(Duration::from_str("P1DT10H30M").unwrap()),
Some(Duration::from_str("P1DT1H30M").unwrap())
Duration::from_str("P2DT12H")?.checked_sub(Duration::from_str("P1DT10H30M")?),
Some(Duration::from_str("P1DT1H30M")?)
);
Ok(())
}
}

@ -267,19 +267,28 @@ mod tests {
#[test]
fn from_str() -> Result<(), ParseFloatError> {
assert!(Float::from(f32::NAN).is_identical_with(&Float::from_str("NaN")?));
assert_eq!(Float::from(f32::INFINITY), Float::from_str("INF")?);
assert_eq!(Float::from(f32::INFINITY), Float::from_str("+INF")?);
assert_eq!(Float::from(f32::NEG_INFINITY), Float::from_str("-INF")?);
assert_eq!(Float::from(0.), Float::from_str("0.0E0")?);
assert_eq!(Float::from(-0.), Float::from_str("-0.0E0")?);
assert_eq!(Float::from_str("NaN")?.to_string(), "NaN");
assert_eq!(Float::from_str("INF")?.to_string(), "INF");
assert_eq!(Float::from_str("+INF")?.to_string(), "INF");
assert_eq!(Float::from_str("-INF")?.to_string(), "-INF");
assert_eq!(Float::from_str("0.0E0")?.to_string(), "0");
assert_eq!(Float::from_str("-0.0E0")?.to_string(), "-0");
assert_eq!(Float::from_str("0.1e1")?.to_string(), "1");
assert_eq!(Float::from_str("-0.1e1")?.to_string(), "-1");
assert_eq!(Float::from_str("1.e1")?.to_string(), "10");
assert_eq!(Float::from_str("-1.e1")?.to_string(), "-10");
assert_eq!(Float::from_str("1")?.to_string(), "1");
assert_eq!(Float::from_str("-1")?.to_string(), "-1");
assert_eq!(Float::from_str("1.")?.to_string(), "1");
assert_eq!(Float::from_str("-1.")?.to_string(), "-1");
assert_eq!(
Float::from_str(&f32::MIN.to_string())?,
Float::from(f32::MIN)
);
assert_eq!(
Float::from_str(&f32::MAX.to_string())?,
Float::from(f32::MAX)
);
Ok(())
}
#[test]
fn to_string() {
assert_eq!("NaN", Float::from(f32::NAN).to_string());
assert_eq!("INF", Float::from(f32::INFINITY).to_string());
assert_eq!("-INF", Float::from(f32::NEG_INFINITY).to_string());
}
}

@ -229,18 +229,28 @@ mod tests {
use super::*;
#[test]
fn from_float() {
fn from_str() -> Result<(), ParseIntError> {
assert_eq!(Integer::from_str("0")?.to_string(), "0");
assert_eq!(Integer::from_str("-0")?.to_string(), "0");
assert_eq!(Integer::from_str("123")?.to_string(), "123");
assert_eq!(Integer::from_str("-123")?.to_string(), "-123");
assert!(Integer::from_str("123456789123456789123456789123456789123456789").is_err());
Ok(())
}
#[test]
fn from_float() -> Result<(), ParseIntError> {
assert_eq!(
Integer::try_from(Float::from(0.)).unwrap(),
Integer::from_str("0").unwrap()
Integer::try_from(Float::from(0.)).ok(),
Some(Integer::from_str("0")?)
);
assert_eq!(
Integer::try_from(Float::from(-0.)).unwrap(),
Integer::from_str("0").unwrap()
Integer::try_from(Float::from(-0.)).ok(),
Some(Integer::from_str("0")?)
);
assert_eq!(
Integer::try_from(Float::from(-123.1)).unwrap(),
Integer::from_str("-123").unwrap()
Integer::try_from(Float::from(-123.1)).ok(),
Some(Integer::from_str("-123")?)
);
assert!(Integer::try_from(Float::from(f32::NAN)).is_err());
assert!(Integer::try_from(Float::from(f32::INFINITY)).is_err());
@ -250,26 +260,27 @@ mod tests {
assert!(
Integer::try_from(Float::from(1672507302466.))
.unwrap()
.checked_sub(Integer::from_str("1672507302466").unwrap())
.checked_sub(Integer::from_str("1672507302466")?)
.unwrap()
.abs()
< Integer::from(1_000_000)
);
Ok(())
}
#[test]
fn from_double() {
fn from_double() -> Result<(), ParseIntError> {
assert_eq!(
Integer::try_from(Double::from(0.0)).unwrap(),
Integer::from_str("0").unwrap()
Integer::try_from(Double::from(0.0)).ok(),
Some(Integer::from_str("0")?)
);
assert_eq!(
Integer::try_from(Double::from(-0.0)).unwrap(),
Integer::from_str("0").unwrap()
Integer::try_from(Double::from(-0.0)).ok(),
Some(Integer::from_str("0")?)
);
assert_eq!(
Integer::try_from(Double::from(-123.1)).unwrap(),
Integer::from_str("-123").unwrap()
Integer::try_from(Double::from(-123.1)).ok(),
Some(Integer::from_str("-123")?)
);
assert!(
Integer::try_from(Double::from(1672507302466.))
@ -284,19 +295,21 @@ mod tests {
assert!(Integer::try_from(Double::from(f64::NEG_INFINITY)).is_err());
assert!(Integer::try_from(Double::from(f64::MIN)).is_err());
assert!(Integer::try_from(Double::from(f64::MAX)).is_err());
Ok(())
}
#[test]
fn from_decimal() {
fn from_decimal() -> Result<(), ParseIntError> {
assert_eq!(
Integer::try_from(Decimal::from(0)).unwrap(),
Integer::from_str("0").unwrap()
Integer::try_from(Decimal::from(0)).ok(),
Some(Integer::from_str("0")?)
);
assert_eq!(
Integer::try_from(Decimal::from_str("-123.1").unwrap()).unwrap(),
Integer::from_str("-123").unwrap()
Integer::try_from(Decimal::from_str("-123.1").unwrap()).ok(),
Some(Integer::from_str("-123")?)
);
assert!(Integer::try_from(Decimal::MIN).is_err());
assert!(Integer::try_from(Decimal::MAX).is_err());
Ok(())
}
}

@ -17,7 +17,7 @@ pub use self::boolean::Boolean;
pub use self::date_time::{
Date, DateTime, DateTimeError, GDay, GMonth, GMonthDay, GYear, GYearMonth, Time, TimezoneOffset,
};
pub use self::decimal::{Decimal, DecimalOverflowError, DecimalParseError};
pub use self::decimal::{Decimal, DecimalOverflowError, ParseDecimalError};
pub use self::double::Double;
pub use self::duration::{DayTimeDuration, Duration, YearMonthDuration};
pub use self::float::Float;

@ -1,5 +1,5 @@
use super::date_time::{DateTimeError, GDay, GMonth, GMonthDay, GYear, GYearMonth, TimezoneOffset};
use super::decimal::DecimalParseError;
use super::decimal::ParseDecimalError;
use super::duration::{DayTimeDuration, YearMonthDuration};
use super::*;
use nom::branch::alt;
@ -29,7 +29,7 @@ enum XsdParseErrorKind {
TooMuchData { count: usize },
Overflow,
ParseInt(ParseIntError),
ParseDecimal(DecimalParseError),
ParseDecimal(ParseDecimalError),
OutOfIntegerRange { value: u8, min: u8, max: u8 },
DateTime(DateTimeError),
}
@ -108,8 +108,8 @@ impl From<ParseIntError> for XsdParseError {
}
}
impl From<DecimalParseError> for XsdParseError {
fn from(error: DecimalParseError) -> Self {
impl From<ParseDecimalError> for XsdParseError {
fn from(error: ParseDecimalError) -> Self {
Self {
kind: XsdParseErrorKind::ParseDecimal(error),
}

Loading…
Cancel
Save