From ed0182736a20e0987c6dc9c5e086a30fd1b02f8b Mon Sep 17 00:00:00 2001 From: pommicket Date: Wed, 10 Sep 2025 02:14:54 -0400 Subject: More schemas, but probably going to get rid of it --- src/lib.rs | 55 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 12 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index e079fca..0763fa7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,6 +38,19 @@ impl Location { pub fn line(&self) -> u64 { self.line } + + /// Dummy location for internal use + fn dummy() -> Self { + Self { + file: Arc::from(""), + line: 0, + } + } + + fn is_dummy(&self) -> bool { + // we never use line number of 0 ordinarily + self.line == 0 + } } impl fmt::Display for Location { @@ -127,6 +140,12 @@ pub enum Error { SchemaBadMaxLength(Location, Box), /// Invalid schema key SchemaBadKey(Location, Box), + /// Value is less than the schema-imposed minimum + SchemaValueLessThanMin(Location, Box, f64, f64), + /// Value is greater than the schema-imposed maximum + SchemaValueGreaterThanMax(Location, Box, f64, f64), + /// Value is greater than the schema-imposed maxlength + SchemaValueTooLong(Location, Box, usize, usize), } impl fmt::Display for Error { @@ -188,6 +207,18 @@ impl fmt::Display for Error { Self::SchemaBadType(l, t) => write!(f, "{l}: invalid type: {t:?}"), Self::SchemaBadMaxLength(l, m) => write!(f, "{l}: invalid maxlength: {m:?}"), Self::SchemaBadKey(l, k) => write!(f, "{l}: invalid schema key: {k}"), + Self::SchemaValueLessThanMin(l, key, val, min) => write!( + f, + "{l}: {key}'s value of {val} is less than the minimum ({min})" + ), + Self::SchemaValueGreaterThanMax(l, key, val, max) => write!( + f, + "{l}: {key}'s value of {val} is greater than the maximum ({max})" + ), + Self::SchemaValueTooLong(l, key, len, maxlen) => write!( + f, + "{l}: {key}'s value has length {len}, which exceeds the maximum of {maxlen}" + ), } } } @@ -417,6 +448,16 @@ fn parse_hex_digit(c: char) -> Option { }) } +/// Returns `Ok(())` if `errors` is empty, otherwise a compound error +/// containing all the `errors`. +fn check_error_vec(mut errors: Vec) -> Result<()> { + match errors.len() { + 0 => Ok(()), + 1 => Err(errors.pop().unwrap()), + _ => Err(Error::Multiple(errors.into())), + } +} + #[derive(Default)] struct Parser { nonfatal_errors: Vec, @@ -664,11 +705,8 @@ impl Parser { )))); } } - match self.nonfatal_errors.len() { - 0 => Ok(Configuration { items }), - 1 => Err(self.nonfatal_errors.pop().unwrap()), - 2.. => Err(Error::Multiple(take(&mut self.nonfatal_errors).into())), - } + check_error_vec(take(&mut self.nonfatal_errors))?; + Ok(Configuration { items }) } } @@ -933,13 +971,6 @@ impl Configuration { pub fn to_schema(&self) -> Result { Schema::try_from(self) } - - /// Check that `self` follows the given schema. - /// - /// Equivalent to `schema.check(self)`. - pub fn check_against(&self, schema: &Schema) -> Result<()> { - schema.check(self) - } } /// Opaque type returned by [`Configuration::iter`]. -- cgit v1.2.3