diff --git a/Cargo.toml b/Cargo.toml index 9af9ae5..52996ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,8 @@ langtag = "0.4.0" thiserror = "1.0.50" serde = "1.0.192" serde_json = { version = "1.0", features = ["arbitrary_precision"] } +# json-syntax = "0.12.2" +json-syntax = { path = "/home/timothee/Projets/utils/json/json-syntax" } locspan = "0.8.2" nquads-syntax = "0.19.0" diff --git a/generators/rust/treeldr-rs-macros/src/generate/de.rs b/generators/rust/treeldr-rs-macros/src/generate/de.rs index 031b82a..4df7936 100644 --- a/generators/rust/treeldr-rs-macros/src/generate/de.rs +++ b/generators/rust/treeldr-rs-macros/src/generate/de.rs @@ -15,7 +15,7 @@ pub enum Error { Parse(#[from] crate::parse::Error), #[error(transparent)] - Build(#[from] treeldr_layouts::abs::syntax::Error), + Build(#[from] treeldr_layouts::abs::syntax::BuildError), #[error("invalid datatype `{0}`")] InvalidDatatype(String), diff --git a/generators/rust/treeldr-rs-macros/src/generate/ser.rs b/generators/rust/treeldr-rs-macros/src/generate/ser.rs index bdf7108..5d6f858 100644 --- a/generators/rust/treeldr-rs-macros/src/generate/ser.rs +++ b/generators/rust/treeldr-rs-macros/src/generate/ser.rs @@ -15,7 +15,7 @@ pub enum Error { Parse(#[from] crate::parse::Error), #[error(transparent)] - Build(#[from] treeldr_layouts::abs::syntax::Error), + Build(#[from] treeldr_layouts::abs::syntax::BuildError), #[error("invalid datatype `{0}`")] InvalidDatatype(String), diff --git a/generators/rust/treeldr-rs-macros/src/lib.rs b/generators/rust/treeldr-rs-macros/src/lib.rs index 119efaf..cc1ec15 100644 --- a/generators/rust/treeldr-rs-macros/src/lib.rs +++ b/generators/rust/treeldr-rs-macros/src/lib.rs @@ -28,7 +28,7 @@ enum Error { Json(serde_json::Error), #[error("build error: {0}")] - Layout(abs::syntax::Error), + Layout(abs::syntax::BuildError), } struct Attribute(syn::punctuated::Punctuated); diff --git a/layouts/Cargo.toml b/layouts/Cargo.toml index bdb5cd2..1dd77b8 100644 --- a/layouts/Cargo.toml +++ b/layouts/Cargo.toml @@ -30,6 +30,7 @@ serde = { workspace = true, features = ["derive"] } thiserror.workspace = true locspan.workspace = true serde_json = "1.0" +json-syntax.workspace = true lazy_static = "1.4.0" static_assertions = "1.1.0" diff --git a/layouts/src/abs/syntax/build.rs b/layouts/src/abs/syntax/build.rs index 46e633f..d78286d 100644 --- a/layouts/src/abs/syntax/build.rs +++ b/layouts/src/abs/syntax/build.rs @@ -15,7 +15,7 @@ use crate::{ use super::LayoutHeader; #[derive(Debug, thiserror::Error)] -pub enum Error { +pub enum BuildError { #[error("no base IRI to resolve `{0}`")] NoBaseIri(IriRefBuf), @@ -135,13 +135,13 @@ where pub trait Build { type Target; - fn build(&self, context: &mut C, scope: &Scope) -> Result; + fn build(&self, context: &mut C, scope: &Scope) -> Result; } impl> Build for Box { type Target = T::Target; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { T::build(&**self, context, scope) } } @@ -167,7 +167,7 @@ impl Scope { /// Creates a new scope by combining this scope with the given layout /// `header` which may define a new base IRI, new IRI prefixes and new /// variables. - pub fn with_header(&self, header: &LayoutHeader) -> Result { + pub fn with_header(&self, header: &LayoutHeader) -> Result { let mut result = self.clone(); if let Some(base_iri) = &header.base { @@ -197,7 +197,7 @@ impl Scope { pub fn with_intro<'s>( &self, intro: impl IntoIterator, - ) -> Result { + ) -> Result { let mut result = self.clone(); for name in intro { @@ -240,10 +240,10 @@ impl Scope { } /// Returns the unique index of the given variable. - pub fn variable(&self, name: &str) -> Result { + pub fn variable(&self, name: &str) -> Result { self.variables .get(name) .copied() - .ok_or_else(|| Error::UndeclaredVariable(name.to_owned())) + .ok_or_else(|| BuildError::UndeclaredVariable(name.to_owned())) } } diff --git a/layouts/src/abs/syntax/dataset.rs b/layouts/src/abs/syntax/dataset.rs index c1b8faf..f7a1175 100644 --- a/layouts/src/abs/syntax/dataset.rs +++ b/layouts/src/abs/syntax/dataset.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use super::{Build, Context, Error, Pattern, Scope}; +use super::{Build, Context, BuildError, Pattern, Scope}; #[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[serde(transparent)] @@ -24,7 +24,7 @@ where { type Target = crate::Dataset; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let mut dataset = crate::Dataset::new(); for quad in &self.0 { dataset.insert(quad.build(context, scope)?); @@ -45,7 +45,7 @@ pub struct Quad( impl Build for Pattern { type Target = crate::Pattern; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { match self { Self::Var(name) => Ok(crate::Pattern::Var(scope.variable(name)?)), Self::Iri(compact_iri) => { @@ -67,7 +67,7 @@ impl Build for Quad { crate::Pattern, >; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { Ok(rdf_types::Quad( self.0.build(context, scope)?, self.1.build(context, scope)?, diff --git a/layouts/src/abs/syntax/layout/intersection.rs b/layouts/src/abs/syntax/layout/intersection.rs index d7f8bdf..c897de2 100644 --- a/layouts/src/abs/syntax/layout/intersection.rs +++ b/layouts/src/abs/syntax/layout/intersection.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use crate::{ - abs::syntax::{Build, Context, Error, Scope}, + abs::syntax::{Build, Context, BuildError, Scope}, layout::LayoutType, Ref, }; @@ -21,7 +21,7 @@ pub struct IntersectionLayout { impl Build for IntersectionLayout { type Target = Vec>; - fn build(&self, _context: &mut C, _scope: &Scope) -> Result { + fn build(&self, _context: &mut C, _scope: &Scope) -> Result { unimplemented!() } } diff --git a/layouts/src/abs/syntax/layout/list.rs b/layouts/src/abs/syntax/layout/list.rs index 11fe064..153998b 100644 --- a/layouts/src/abs/syntax/layout/list.rs +++ b/layouts/src/abs/syntax/layout/list.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use crate::abs::{ self, syntax::{ - Build, CompactIri, Context, Dataset, Error, Pattern, Scope, ValueFormatOrLayout, ValueIntro, + Build, CompactIri, Context, Dataset, BuildError, Pattern, Scope, ValueFormatOrLayout, ValueIntro, }, }; @@ -36,7 +36,7 @@ where { type Target = abs::layout::ListLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { match self { Self::Ordered(l) => l .build(context, scope) @@ -114,7 +114,7 @@ where { type Target = abs::layout::list::ordered::NodeLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let head = scope.variable_count(); let rest = head + 1; let first = rest + 1; @@ -181,7 +181,7 @@ where { type Target = abs::layout::OrderedListLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let (header, scope) = self.header.build(context, scope)?; Ok(abs::layout::OrderedListLayout { input: header.input, @@ -201,7 +201,7 @@ where { type Target = abs::layout::list::ordered::NodeLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let head = scope.variable_count(); let rest = head + 1; let scope = scope.with_intro( @@ -266,7 +266,7 @@ where { type Target = abs::layout::UnorderedListLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let subject = if self.header.input.is_empty() { None } else { @@ -291,7 +291,7 @@ impl ListItem { context: &mut C, scope: &Scope, subject: Option, - ) -> Result, Error> + ) -> Result, BuildError> where C::Resource: Clone, { @@ -316,9 +316,9 @@ impl ListItem { None, )); } - None => return Err(Error::NoPropertyObject), + None => return Err(BuildError::NoPropertyObject), }, - None => return Err(Error::NoPropertySubject), + None => return Err(BuildError::NoPropertySubject), } } @@ -349,7 +349,7 @@ where { type Target = abs::layout::SizedListLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let subject = if self.header.input.is_empty() { None } else { diff --git a/layouts/src/abs/syntax/layout/literal.rs b/layouts/src/abs/syntax/layout/literal.rs index e83d303..d4ba481 100644 --- a/layouts/src/abs/syntax/layout/literal.rs +++ b/layouts/src/abs/syntax/layout/literal.rs @@ -1,9 +1,10 @@ +use json_syntax::TryFromJsonSyntax; use serde::{Deserialize, Serialize}; use crate::{ abs::{ self, - syntax::{Build, CompactIri, Context, Error, Pattern, Scope}, + syntax::{check_type, expect_object, get_entry, Build, BuildError, CompactIri, Context, Error, Pattern, Scope}, RegExp, }, Value, @@ -36,7 +37,7 @@ where { type Target = abs::layout::LiteralLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { match self { Self::Data(l) => Ok(abs::layout::LiteralLayout::Data(l.build(context, scope)?)), Self::Id(l) => Ok(abs::layout::LiteralLayout::Id(l.build(context, scope)?)), @@ -72,7 +73,7 @@ where { type Target = abs::layout::DataLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { match self { Self::Unit(l) => l.build(context, scope).map(abs::layout::DataLayout::Unit), Self::Boolean(l) => l @@ -108,7 +109,7 @@ where { type Target = abs::layout::UnitLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let (header, _) = self.header.build(context, scope)?; Ok(abs::layout::UnitLayout { input: header.input, @@ -140,12 +141,12 @@ fn literal_resource( scope: &Scope, input: &LayoutInput, resource: Option<&Pattern>, -) -> Result, Error> { +) -> Result, BuildError> { match resource { Some(r) => r.build(context, scope), None => { if input.is_empty() { - Err(Error::MissingLiteralTargetResource) + Err(BuildError::MissingLiteralTargetResource) } else { Ok(crate::Pattern::Var(0)) } @@ -159,7 +160,7 @@ where { type Target = abs::layout::BooleanLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let (header, scope) = self.header.build(context, scope)?; Ok(abs::layout::BooleanLayout { @@ -203,7 +204,7 @@ where { type Target = abs::layout::NumberLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let (header, scope) = self.header.build(context, scope)?; Ok(abs::layout::NumberLayout { @@ -242,7 +243,7 @@ where { type Target = abs::layout::ByteStringLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let (header, scope) = self.header.build(context, scope)?; Ok(abs::layout::ByteStringLayout { @@ -285,7 +286,7 @@ where { type Target = abs::layout::TextStringLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let (header, scope) = self.header.build(context, scope)?; Ok(abs::layout::TextStringLayout { @@ -325,13 +326,32 @@ pub struct IdLayout { pub resource: Option, } +impl TryFromJsonSyntax for IdLayout { + type Error = Error; + + fn try_from_json_syntax_at(json: &json_syntax::Value, code_map: &json_syntax::CodeMap, offset: usize) -> Result { + let object = expect_object(json, offset)?; + check_type(object, IdLayoutType::NAME, code_map, offset)?; + let header = LayoutHeader::try_from_json_syntax_at(object, code_map, offset)?; + let pattern = get_entry(object, "pattern", code_map, offset)?; + let resource = get_entry(object, "resource", code_map, offset)?; + + Ok(Self { + type_: IdLayoutType, + header, + pattern, + resource + }) + } +} + impl Build for IdLayout where C::Resource: Clone, { type Target = abs::layout::IdLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let (header, scope) = self.header.build(context, scope)?; Ok(abs::layout::IdLayout { diff --git a/layouts/src/abs/syntax/layout/mod.rs b/layouts/src/abs/syntax/layout/mod.rs index 285d7cf..f845b5a 100644 --- a/layouts/src/abs/syntax/layout/mod.rs +++ b/layouts/src/abs/syntax/layout/mod.rs @@ -29,8 +29,7 @@ use crate::{ }; use super::{ - Build, CompactIri, Context, Dataset, Error, OneOrMany, Pattern, Resource, Scope, ValueFormat, - VariableName, + Build, BuildError, CompactIri, Context, Dataset, Error, OneOrMany, Pattern, Resource, Scope, ValueFormat, VariableName }; /// Abstract syntax layout. @@ -110,7 +109,7 @@ impl Layout { } } - pub fn build(&self, builder: &mut Builder) -> Result, Error> { + pub fn build(&self, builder: &mut Builder) -> Result, BuildError> { let mut context = builder.with_generator_mut(generator::Blank::new()); self.build_with_context(&mut context) } @@ -120,7 +119,7 @@ impl Layout { vocabulary: &mut V, interpretation: &mut I, builder: &mut Builder, - ) -> Result, Error> + ) -> Result, BuildError> where V: VocabularyMut, I: IriInterpretationMut @@ -135,7 +134,7 @@ impl Layout { pub fn build_with_context( &self, context: &mut C, - ) -> Result, Error> + ) -> Result, BuildError> where C::Resource: Clone, { @@ -150,7 +149,7 @@ where { type Target = Ref; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let id = match self.id() { Some(id) => { let iri = id.resolve(scope)?; @@ -173,7 +172,7 @@ where let (layout_ref, old_layout) = context.insert_layout(id, layout); if old_layout.is_some() { - Err(Error::LayoutRedefinition) + Err(BuildError::LayoutRedefinition) } else { Ok(layout_ref) } @@ -193,7 +192,7 @@ where { type Target = Ref; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let scope = scope.without_variables(); match self { Self::Ref(r) => r.build(context, &scope).map(Ref::new), @@ -268,13 +267,19 @@ pub struct LayoutHeader { pub extra: ExtraProperties, } +impl LayoutHeader { + fn try_from_json_syntax_at(object: &json_syntax::Object, code_map: &json_syntax::CodeMap, offset: usize) -> Result { + todo!() + } +} + impl Build for LayoutHeader where C::Resource: Clone, { type Target = (BuiltLayoutHeader, Scope); - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let scope = scope.with_header(self)?; let header = BuiltLayoutHeader { @@ -312,7 +317,7 @@ impl ExtraProperties { impl Build for ExtraProperties { type Target = BTreeMap; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let mut result = BTreeMap::new(); for (prop, value) in &self.0 { @@ -370,7 +375,7 @@ where { type Target = crate::ValueFormat; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let mut inputs = Vec::with_capacity(self.input.len()); for i in self.input.as_slice() { inputs.push(i.build(context, scope)?); @@ -395,6 +400,8 @@ macro_rules! type_markers { pub struct $id; impl $id { + pub const NAME: &'static str = $value; + pub fn as_str(&self) -> &'static str { $value } diff --git a/layouts/src/abs/syntax/layout/product.rs b/layouts/src/abs/syntax/layout/product.rs index 876cbbc..e866b88 100644 --- a/layouts/src/abs/syntax/layout/product.rs +++ b/layouts/src/abs/syntax/layout/product.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use crate::abs::{ self, - syntax::{Build, Context, Dataset, Error, Pattern, Scope, ValueFormatOrLayout, ValueIntro}, + syntax::{Build, Context, Dataset, BuildError, Pattern, Scope, ValueFormatOrLayout, ValueIntro}, }; use super::{LayoutHeader, ProductLayoutType}; @@ -28,7 +28,7 @@ where { type Target = abs::layout::ProductLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let (header, scope) = self.header.build(context, scope)?; let mut fields = BTreeMap::new(); @@ -40,11 +40,11 @@ where if let Some(property) = &field.property { if self.header.input.is_empty() { - return Err(Error::NoPropertySubject); + return Err(BuildError::NoPropertySubject); } else { let subject = crate::Pattern::Var(0); if field.intro.is_empty() { - return Err(Error::NoPropertyObject); + return Err(BuildError::NoPropertyObject); } else { let object = crate::Pattern::Var( (self.header.input.len() + self.header.intro.len()) as u32, diff --git a/layouts/src/abs/syntax/layout/sum.rs b/layouts/src/abs/syntax/layout/sum.rs index 51f7c44..a6e7a88 100644 --- a/layouts/src/abs/syntax/layout/sum.rs +++ b/layouts/src/abs/syntax/layout/sum.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use crate::abs::{ self, - syntax::{Build, Context, Dataset, Error, OneOrMany, Pattern, Scope, VariableName}, + syntax::{Build, Context, Dataset, BuildError, OneOrMany, Pattern, Scope, VariableName}, }; use super::{LayoutHeader, LayoutRef, SumLayoutType}; @@ -40,7 +40,7 @@ where { type Target = abs::layout::SumLayout; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let (header, scope) = self.header.build(context, scope)?; let mut variants = Vec::with_capacity(self.variants.len()); @@ -78,7 +78,7 @@ where { type Target = crate::ValueFormat; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { match self { Self::Format(f) => f.build(context, scope), Self::Layout(layout) => Ok(crate::ValueFormat { @@ -108,7 +108,7 @@ where { type Target = crate::ValueFormat; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let mut inputs = Vec::with_capacity(self.input.len()); for i in self.input.as_slice() { inputs.push(i.build(context, scope)?); diff --git a/layouts/src/abs/syntax/layout/union.rs b/layouts/src/abs/syntax/layout/union.rs index 9b06593..a16a334 100644 --- a/layouts/src/abs/syntax/layout/union.rs +++ b/layouts/src/abs/syntax/layout/union.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use crate::{ - abs::syntax::{Build, Context, Error, Scope}, + abs::syntax::{Build, Context, BuildError, Scope}, layout::LayoutType, Ref, }; @@ -21,7 +21,7 @@ pub struct UnionLayout { impl Build for UnionLayout { type Target = Vec>; - fn build(&self, _context: &mut C, _scope: &Scope) -> Result { + fn build(&self, _context: &mut C, _scope: &Scope) -> Result { unimplemented!() } } diff --git a/layouts/src/abs/syntax/mod.rs b/layouts/src/abs/syntax/mod.rs index 6cc5112..5805bd8 100644 --- a/layouts/src/abs/syntax/mod.rs +++ b/layouts/src/abs/syntax/mod.rs @@ -12,6 +12,92 @@ pub use layout::*; pub use pattern::*; pub use resource::*; +pub enum Error { + Unexpected { + offset: usize, + expected: json_syntax::Kind, + found: json_syntax::Kind + }, + DuplicateEntry { + offset: usize, + key: String, + other_offset: usize + }, + MissingType(usize), + InvalidType { + offset: usize, + expected: &'static str, + found: String + } +} + +impl Error { + pub fn duplicate<'a>(key: &str) -> impl '_ + FnOnce(json_syntax::object::Duplicate>) -> Self { + move |e| { + Self::DuplicateEntry { offset: e.0.offset, key: key.to_owned(), other_offset: e.1.offset } + } + } +} + +pub(crate) fn expect_object( + json: &json_syntax::Value, + offset: usize, +) -> Result<&json_syntax::Object, Error> { + match json { + json_syntax::Value::Object(object) => { + Ok(object) + } + json => Err(Error::Unexpected { + offset, + expected: json_syntax::Kind::Object, + found: json.kind() + }) + } +} + +pub(crate) fn get_entry<'a, T: json_syntax::TryFromJsonSyntax>( + object: &'a json_syntax::Object, + key: &str, + code_map: &json_syntax::CodeMap, + offset: usize +) -> Result, Error> { + let value = object.get_unique_mapped(code_map, offset, key) + .map_err(Error::duplicate(key))?; + + match value { + Some(value) => { + let t = T::try_from_json_syntax_at(value.value, code_map, value.offset)?; + Ok(Some(t)) + } + None => Ok(None) + } +} + +pub(crate) fn check_type(object: &json_syntax::Object, expected: &'static str, code_map: &json_syntax::CodeMap, offset: usize) -> Result<(), Error> { + let ty = object.get_unique_mapped(code_map, offset, "type") + .map_err(Error::duplicate("type"))? + .ok_or(Error::MissingType(offset))?; + + match ty.value { + json_syntax::Value::String(found) => { + if found == expected { + Ok(()) + } else { + Err(Error::InvalidType { + offset: ty.offset, + expected, + found: found.to_string() + }) + } + } + other => Err(Error::Unexpected { + offset: ty.offset, + expected: json_syntax::Kind::String, + found: other.kind() + }) + } +} + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[serde(untagged)] pub enum OneOrMany { @@ -71,7 +157,7 @@ where { type Target = crate::ValueFormat; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { match self { Self::Format(f) => f.build(context, scope), Self::Layout(layout) => Ok(crate::ValueFormat { diff --git a/layouts/src/abs/syntax/pattern/compact_iri.rs b/layouts/src/abs/syntax/pattern/compact_iri.rs index e76fda1..fa98bc4 100644 --- a/layouts/src/abs/syntax/pattern/compact_iri.rs +++ b/layouts/src/abs/syntax/pattern/compact_iri.rs @@ -2,25 +2,25 @@ use iref::{IriBuf, IriRefBuf}; use serde::{Deserialize, Serialize}; use xsd_types::XSD_STRING; -use crate::abs::syntax::{Build, Context, Error, Scope}; +use crate::abs::syntax::{Build, Context, BuildError, Scope}; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[serde(transparent)] pub struct CompactIri(pub IriRefBuf); impl CompactIri { - pub fn resolve(&self, scope: &Scope) -> Result { + pub fn resolve(&self, scope: &Scope) -> Result { match self.0.as_iri() { Some(iri) => match scope.iri_prefix(iri.scheme().as_str()) { Some(prefix) => { let suffix = iri.split_once(':').unwrap().1; - IriBuf::new(format!("{prefix}{suffix}")).map_err(|e| Error::InvalidIri(e.0)) + IriBuf::new(format!("{prefix}{suffix}")).map_err(|e| BuildError::InvalidIri(e.0)) } None => Ok(iri.to_owned()), }, None => match &scope.base_iri() { Some(base_iri) => Ok(self.0.resolved(base_iri)), - None => Err(Error::NoBaseIri(self.0.clone())), + None => Err(BuildError::NoBaseIri(self.0.clone())), }, } } @@ -45,7 +45,7 @@ impl Build for CompactIri { /// Build this layout fragment using the given `context` in the given /// `scope`. - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let iri = self.resolve(scope)?; Ok(context.iri_resource(&iri)) } diff --git a/layouts/src/abs/syntax/pattern/literal.rs b/layouts/src/abs/syntax/pattern/literal.rs index 56bb30b..9d7858b 100644 --- a/layouts/src/abs/syntax/pattern/literal.rs +++ b/layouts/src/abs/syntax/pattern/literal.rs @@ -2,7 +2,7 @@ use iref::IriBuf; use langtag::LangTagBuf; use rdf_types::XSD_STRING; -use crate::abs::syntax::{Error, Scope}; +use crate::abs::syntax::{BuildError, Scope}; use super::CompactIri; @@ -43,7 +43,7 @@ impl Default for LiteralType { } impl LiteralType { - pub fn resolve(&self, scope: &Scope) -> Result { + pub fn resolve(&self, scope: &Scope) -> Result { match self { Self::Iri(iri) => Ok(rdf_types::LiteralType::Any(iri.resolve(scope)?)), Self::Language(lang) => Ok(rdf_types::LiteralType::LangString(lang.language.clone())), @@ -75,7 +75,7 @@ pub struct LiteralTypeIri { } impl LiteralTypeIri { - pub fn resolve(&self, scope: &Scope) -> Result { + pub fn resolve(&self, scope: &Scope) -> Result { self.type_.resolve(scope) } } diff --git a/layouts/src/abs/syntax/pattern/mod.rs b/layouts/src/abs/syntax/pattern/mod.rs index a6a5d6f..40d7f2f 100644 --- a/layouts/src/abs/syntax/pattern/mod.rs +++ b/layouts/src/abs/syntax/pattern/mod.rs @@ -11,7 +11,7 @@ use rdf_types::{BlankIdBuf, Id, Term, RDF_NIL}; use serde::{Deserialize, Serialize}; pub use variable::*; -use super::{Error, Scope}; +use super::{BuildError, Scope}; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Pattern { @@ -58,7 +58,7 @@ impl Pattern { } } - pub fn to_term(&self, scope: &Scope) -> Result { + pub fn to_term(&self, scope: &Scope) -> Result { match self { Self::Var(name) => Ok(Term::blank(BlankIdBuf::from_suffix(name).unwrap())), Self::Iri(compact_iri) => compact_iri.resolve(scope).map(Term::iri), diff --git a/layouts/src/abs/syntax/resource.rs b/layouts/src/abs/syntax/resource.rs index d41ae44..63fd12c 100644 --- a/layouts/src/abs/syntax/resource.rs +++ b/layouts/src/abs/syntax/resource.rs @@ -2,7 +2,7 @@ use rdf_types::LexicalLiteralTypeRef; use serde::{Deserialize, Serialize}; use xsd_types::{XSD_BOOLEAN, XSD_STRING}; -use super::{Build, CompactIri, Context, Error, Scope}; +use super::{Build, CompactIri, Context, BuildError, Scope}; /// RDF Resource description. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] @@ -24,7 +24,7 @@ pub enum Resource { impl Build for Resource { type Target = C::Resource; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { match self { &Self::Boolean(b) => { let value = if b { "true" } else { "false" }; @@ -62,7 +62,7 @@ pub struct TypedString { impl Build for TypedString { type Target = C::Resource; - fn build(&self, context: &mut C, scope: &Scope) -> Result { + fn build(&self, context: &mut C, scope: &Scope) -> Result { let type_ = self.type_.resolve(scope)?; Ok(context.literal_resource(&self.value, LexicalLiteralTypeRef::Any(&type_))) }