diff --git a/prefix_map/src/prefix_map.rs b/prefix_map/src/prefix_map.rs index 81c5c2e3..9c6b383c 100644 --- a/prefix_map/src/prefix_map.rs +++ b/prefix_map/src/prefix_map.rs @@ -100,5 +100,15 @@ mod tests { assert_eq!(pm.to_string(), "ex \nrdf \n"); } + + #[test] + fn prefix_map_resolve() { + let mut pm = PrefixMap::new(); + let ex_iri = IriS::from_str("http://example.org/").unwrap(); + pm.insert("ex", &ex_iri); + assert_eq!(pm.resolve(&"ex:pepe").unwrap(), + Some(IriS::from_str("http://example.org/pepe").unwrap())); + } + } diff --git a/shex_ast/src/schema.rs b/shex_ast/src/schema.rs index 2390094f..a0fcfd1d 100644 --- a/shex_ast/src/schema.rs +++ b/shex_ast/src/schema.rs @@ -1,15 +1,27 @@ +use iri_s::IriError; use prefix_map::PrefixMap; use srdf::iri::IriS; #[derive(Debug)] pub struct Schema<'a> { - pub id: Option>, - pub base: Option>, - pub prefixes: Option> + pub(crate) id: Option, + pub(crate) base: Option, + pub(crate) prefixes: Option> } impl <'a> Schema<'a> { - pub fn base(&self) -> &Option> { &self.base } + + pub fn id(&self) -> Option { self.id.clone() } + + pub fn base(&self) -> Option { self.base.clone() } + + pub fn resolve(&self, alias: &str) -> Result, IriError> { + match &self.prefixes { + Some(pm) => pm.resolve(alias), + None => Ok(None) + } + } + } diff --git a/shex_ast/src/schema_builder.rs b/shex_ast/src/schema_builder.rs index 58c2c918..36723bbe 100644 --- a/shex_ast/src/schema_builder.rs +++ b/shex_ast/src/schema_builder.rs @@ -25,8 +25,8 @@ impl Error for ErrorBuildingSchema {} // type Result = Result struct SchemaParts<'a> { - id: Option>, - base: Option>, + id: Option, + base: Option, prefixes: PrefixMap<'a>, shapes_counter: u32 } @@ -45,7 +45,7 @@ impl <'a> SchemaBuilder<'a> { pub fn set_base(self, base: IriS) -> SchemaBuilder<'a> { self.and_then(move |mut schema_parts| { - schema_parts.base = Some(Box::new(base)); + schema_parts.base = Some(base); Ok(schema_parts) }) } @@ -103,14 +103,29 @@ mod tests { Ok(sb.set_base(iri)) } + fn update_prefix_map<'a>(sb: SchemaBuilder<'a>, alias: &'a str, iri: &'a IriS) -> Result, ErrorBuildingSchema> { + Ok(sb.add_prefix(alias,&iri)) + } #[test] - fn test_update() { + fn test_update_base() { let sb = SchemaBuilder::new(); let iri = IriS::from_str("http://example.org/").unwrap(); let schema = update_base(sb, iri).unwrap().build().unwrap(); - assert_eq!(schema.base(), &Some(Box::new(IriS::from_str("http://example.org/").unwrap()))); + assert_eq!(schema.base(), Some(IriS::from_str("http://example.org/").unwrap())); } + #[test] + fn test_update_prefix_map() { + let sb = SchemaBuilder::new(); + let iri = IriS::from_str("http://example.org/").unwrap(); + let schema = update_prefix_map(sb, &"ss", &iri).unwrap().build().unwrap(); + assert_eq!( + schema.resolve(&"ss:foo").unwrap(), + Some(IriS::from_str("http://example.org/foo").unwrap()) + ); + } + + }