diff options
author | pommicket <pommicket@gmail.com> | 2025-09-09 23:34:05 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2025-09-09 23:34:05 -0400 |
commit | 8439ed183206659ad581caf525e078c6bb2c6a64 (patch) | |
tree | 50e803277cb078f9bee2e270bface4054ae70c47 /src/lib.rs | |
parent | 00ba4ae712a7a372bd937b56053d601ddc6eb3e8 (diff) |
Schema parsing
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 32 |
1 files changed, 27 insertions, 5 deletions
@@ -14,6 +14,8 @@ use alloc::{format, vec}; use core::fmt; use core::mem::take; +mod schema; +pub use schema::Schema; #[cfg(test)] mod tests; @@ -119,6 +121,12 @@ pub enum Error { /// None of the errors in the array will be [`Error::Multiple`]'s, /// and the array will contain at least two elements. Multiple(Box<[Error]>), + /// Bad type in schema + SchemaBadType(Location, Box<str>), + /// Bad maxlength in schema + SchemaBadMaxLength(Location, Box<str>), + /// Invalid schema key + SchemaBadKey(Location, Box<str>), } impl fmt::Display for Error { @@ -177,6 +185,9 @@ impl fmt::Display for Error { } Ok(()) } + 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}"), } } } @@ -752,6 +763,14 @@ impl Configuration { self.into_iter() } + /// Same as `iter()` (for now), but explicitly marks that keys should be in sorted order. + /// + /// This is used internally so that we know what we have to fix if `iter()` + /// is ever changed to return a non-sorted iterator. + fn iter_sorted(&self) -> ConfigurationIter<'_> { + self.iter() + } + fn get_val(&self, key: &str) -> Option<&Value> { let idx = self.binary_search_for(key).ok()?; Some(&self.items[idx].1) @@ -910,13 +929,16 @@ impl Configuration { } } + /// Parse `self` as a [`Schema`]. + pub fn to_schema(&self) -> Result<Schema> { + Schema::try_from(self) + } + /// Check that `self` follows the given schema. /// - /// See the [POM specification](https://www.pom.computer/spec.html) for a description - /// of schemas. - pub fn check_against_schema(&self, schema: &Configuration) -> Result<()> { - _ = schema; - todo!() + /// Equivalent to `schema.check(self)`. + pub fn check_against(&self, schema: &Schema) -> Result<()> { + schema.check(self) } } |