Skip to content

Commit

Permalink
Added prefix map
Browse files Browse the repository at this point in the history
  • Loading branch information
labra committed Mar 19, 2023
1 parent 94420d2 commit 218291d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
10 changes: 10 additions & 0 deletions prefix_map/src/prefix_map.rs
Expand Up @@ -100,5 +100,15 @@ mod tests {
assert_eq!(pm.to_string(),
"ex <http://example.org/>\nrdf <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\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()));
}

}

20 changes: 16 additions & 4 deletions 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<Box<IriS>>,
pub base: Option<Box<IriS>>,
pub prefixes: Option<PrefixMap<'a>>
pub(crate) id: Option<IriS>,
pub(crate) base: Option<IriS>,
pub(crate) prefixes: Option<PrefixMap<'a>>
}


impl <'a> Schema<'a> {
pub fn base(&self) -> &Option<Box<IriS>> { &self.base }

pub fn id(&self) -> Option<IriS> { self.id.clone() }

pub fn base(&self) -> Option<IriS> { self.base.clone() }

pub fn resolve(&self, alias: &str) -> Result<Option<IriS>, IriError> {
match &self.prefixes {
Some(pm) => pm.resolve(alias),
None => Ok(None)
}
}

}

25 changes: 20 additions & 5 deletions shex_ast/src/schema_builder.rs
Expand Up @@ -25,8 +25,8 @@ impl Error for ErrorBuildingSchema {}
// type Result<T> = Result<T,ErrorBuildingSchema>

struct SchemaParts<'a> {
id: Option<Box<IriS>>,
base: Option<Box<IriS>>,
id: Option<IriS>,
base: Option<IriS>,
prefixes: PrefixMap<'a>,
shapes_counter: u32
}
Expand All @@ -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)
})
}
Expand Down Expand Up @@ -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<SchemaBuilder<'a>, 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())
);
}


}

0 comments on commit 218291d

Please sign in to comment.