Skip to content

Commit

Permalink
Seems to parse prefix decl
Browse files Browse the repository at this point in the history
  • Loading branch information
labra committed Mar 19, 2023
1 parent b034d72 commit 94420d2
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 106 deletions.
1 change: 1 addition & 0 deletions iri_s/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ edition.workspace = true
license.workspace = true

[dependencies]
oxiri = "0.2.2"
52 changes: 35 additions & 17 deletions iri_s/src/iri.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,69 @@
use std::{ops::Add, fmt};
use std::fmt;
use std::str::FromStr;
use oxiri::{IriRef, IriParseError};

pub trait IRI {
// fn to_string(&self) -> String ;
}

#[derive(Debug, Clone, PartialEq)]
pub struct IriS {
s: String
s: String,
iri: IriRef<String>
}
impl IriS {

pub fn as_str(&self) -> &str {
self.s.as_str()
}

pub fn from_str(str: &str) -> IriS {
IriS { s: str.to_owned() }
pub fn extend(&self, str: &str) -> Result<Self, IriError> {
let s = self.s.clone() + str;
let iri = IriRef::parse(s)?;
Ok(IriS { s: iri.to_string(), iri: iri })
}

pub fn extend(&self, str: &str) -> Self {
let s = self.s.clone() + str;
IriS { s: s }
pub fn is_absolute(&self) -> bool {
self.iri.is_absolute()
}

}

impl fmt::Display for IriS {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,"<{}>", self.s)
}
}

impl Add for IriS {
type Output = Self;
#[derive(Debug)]
pub struct IriError {
msg: String
}


impl FromStr for IriS {
type Err = IriError;

fn add(self, other: Self) -> Self {
IriS {
s: self.s + other.s.as_str()
}
fn from_str(s: &str) -> Result<Self, Self::Err> {
parse_iri(s)
}

}

impl From<IriParseError> for IriError {
fn from(e: IriParseError) -> Self {
IriError { msg: format!("IriParserError: {:?}",e.to_string())}
}
}

fn parse_iri(s:&str) -> Result<IriS, IriError> {
let iri = IriRef::parse(s.to_owned())?;
Ok(IriS { s: iri.to_string(), iri: iri })
}


impl IRI for IriS {

/* fn to_string(&self) -> String {
self.s.clone()
}*/
}


Expand All @@ -55,7 +73,7 @@ mod tests {

#[test]
fn creating_iris() {
let iri = IriS::from_str("http://example.org/") ;
let iri = IriS::from_str("http://example.org/").unwrap() ;
assert_eq!(iri.to_string(), "<http://example.org/>");
}

Expand Down
5 changes: 3 additions & 2 deletions iri_s/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
pub mod iri;

pub use iri::*;
use std::str::FromStr;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn iri_s_test() {
let iri1: IriS = IriS::from_str("http://example.org/iri");
let iri2 = IriS::from_str("http://example.org/iri");
let iri1: IriS = IriS::from_str("http://example.org/iri").unwrap();
let iri2 = IriS::from_str("http://example.org/iri").unwrap();
assert_eq!(iri1, iri2);
}
}
26 changes: 17 additions & 9 deletions prefix_map/src/prefix_map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt;
use indexmap::IndexMap;
use iri_s::*;
use std::str::FromStr;

#[derive(Debug)]
pub struct PrefixMap<'a> {
Expand Down Expand Up @@ -32,17 +33,24 @@ impl <'a> PrefixMap<'a> {
}
}

pub fn resolve(&self, str: &str) -> Option<IriS> {
pub fn resolve(&self, str: &str) -> Result<Option<IriS>, IriError> {
match split(str) {
Some((alias, local_name)) => {
match self.find(alias) {
Some(iri) => {
Some(iri.extend(local_name))
let new_iri = iri.extend(local_name)?;
Ok(Some(new_iri))
},
None => Some(IriS::from_str(str))
None => {
let iri = IriS::from_str(str)?;
Ok(Some(iri))
}
}
},
None => Some(IriS::from_str(str))
None => {
let iri = IriS::from_str(str)?;
Ok(Some(iri))
}
}

}
Expand Down Expand Up @@ -76,18 +84,18 @@ mod tests {
#[test]
fn prefix_map1() {
let mut pm = PrefixMap::new();
let binding = IriS::from_str("http://example.org/");
let binding = IriS::from_str("http://example.org/").unwrap();
pm.insert("ex", &binding);
let resolved = IriS::from_str("http://example.org/name");
assert_eq!(pm.resolve("ex:name").unwrap(), resolved);
let resolved = IriS::from_str("http://example.org/name").unwrap();
assert_eq!(pm.resolve("ex:name").unwrap().unwrap(), resolved);
}

#[test]
fn prefix_map_display() {
let mut pm = PrefixMap::new();
let ex_iri = IriS::from_str("http://example.org/");
let ex_iri = IriS::from_str("http://example.org/").unwrap();
pm.insert("ex", &ex_iri);
let ex_rdf = IriS::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
let ex_rdf = IriS::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#").unwrap();
pm.insert("rdf", &ex_rdf);
assert_eq!(pm.to_string(),
"ex <http://example.org/>\nrdf <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
Expand Down
48 changes: 4 additions & 44 deletions shex_ast/src/schema_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use prefix_map::PrefixMap;
use srdf::iri::IriS;
use std::{error::Error, fmt};
use crate::schema::Schema;
use std::str::FromStr;

pub struct SchemaBuilder<'a> {
inner: Result<SchemaParts<'a>, ErrorBuildingSchema>
Expand Down Expand Up @@ -106,51 +107,10 @@ mod tests {
#[test]
fn test_update() {
let sb = SchemaBuilder::new();
let schema =
update_base(sb, IriS::from_str("http://example.org/")).unwrap().build().unwrap();
assert_eq!(schema.base(), &Some(Box::new(IriS::from_str("http://example.org/"))));
/*match update_base(&mut sb, IriS::from_str("http://example.org/")) {
Ok(sb) => {
let s = sb.build();
assert_eq!(s.base.unwrap(), Box::new(IriS::from_str("http://example.org/")));
},
Err(err) => {
assert_eq!(2+2,4);
}
}; */
// let s = r.build();
//let r = sb;

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())));
}

}

/*#[test]
fn builder_test() {
use iri_s::IriS;
let foo = Schema {
id: None,
base: Some(Box::new(IriS::from_str("hi"))),
prefixes: Some(PrefixMap::new())
};
let mut builder = SchemaBuilder::new();
builder.set_base(IriS::from_str("hi"));
let foo_from_builder = builder.build();
let r1 = foo.base().map(|s| {Some(s)}).unwrap();
let r2 = foo_from_builder.unwrap().base();
assert_eq!(r1, *r2);
} */

/*#[test]
fn fn_builder() {
use iri_s::IriS;
let ex = IriS::from_str("http://example.org");
let mut sb = SchemaBuilder::new();
sb.set_base(IriS::from_str("hi"))
.add_prefix("rdf", &ex);
let schema = sb.build();
let schema_base = schema.unwrap().base;
assert_eq!(
schema_base,
Some(Box::new(IriS::from_str("hi"))));
}*/
5 changes: 3 additions & 2 deletions shex_pest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ iri_s = { path = "../iri_s" }
shex_ast = { path = "../shex_ast" }
pest = { version = "2.5.6" }
pest_derive = { version = "2.5.6" }

regex = "1.7"

[dev-dependencies]
env_logger = "0.10"
pretty_env_logger = "0.4"
pretty_env_logger = "0.4"
lazy_static = "1.4"
1 change: 1 addition & 0 deletions shex_pest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod shexc_error;
extern crate pest;
#[macro_use]
extern crate pest_derive;
extern crate regex;

#[cfg(test)]
mod tests {
Expand Down

0 comments on commit 94420d2

Please sign in to comment.