diff options
author | pommicket <pommicket@gmail.com> | 2025-09-10 02:14:54 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2025-09-10 02:14:54 -0400 |
commit | ed0182736a20e0987c6dc9c5e086a30fd1b02f8b (patch) | |
tree | 4113e76e73e60fe43d72c707bcbbdb47a1ced0f7 /src/lib.rs | |
parent | 8439ed183206659ad581caf525e078c6bb2c6a64 (diff) |
More schemas, but probably going to get rid of it
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 55 |
1 files changed, 43 insertions, 12 deletions
@@ -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<str>), /// Invalid schema key SchemaBadKey(Location, Box<str>), + /// Value is less than the schema-imposed minimum + SchemaValueLessThanMin(Location, Box<str>, f64, f64), + /// Value is greater than the schema-imposed maximum + SchemaValueGreaterThanMax(Location, Box<str>, f64, f64), + /// Value is greater than the schema-imposed maxlength + SchemaValueTooLong(Location, Box<str>, 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<u32> { }) } +/// Returns `Ok(())` if `errors` is empty, otherwise a compound error +/// containing all the `errors`. +fn check_error_vec(mut errors: Vec<Error>) -> Result<()> { + match errors.len() { + 0 => Ok(()), + 1 => Err(errors.pop().unwrap()), + _ => Err(Error::Multiple(errors.into())), + } +} + #[derive(Default)] struct Parser { nonfatal_errors: Vec<Error>, @@ -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> { 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`]. |