shex_ast/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
//! ShEx Abstract Syntax
//!
//! Ths abstract syntax follows the [ShEx abstract syntax spec](https://shex.io/)
//!
// #![deny(rust_2018_idioms)]
pub mod ast;
pub mod compiled;
pub mod node;
pub mod pred;
pub mod shexr;
pub use ast::*;
pub use compiled::compiled_schema_error::*;
pub use compiled::shape_label_idx::*;
pub use node::*;
pub use pred::*;
use rbe::MatchCond;
type CResult<T> = Result<T, CompiledSchemaError>;
type Cond = MatchCond<Pred, Node, ShapeLabelIdx>;
#[cfg(test)]
mod tests {
#[derive(PartialEq, Debug)]
enum SE {
And { es: Vec<SE> },
Not { e: Box<SE> },
S { v: String },
}
#[derive(PartialEq, Debug)]
enum SE1 {
And { es: Vec<SE1> },
Not { e: Box<SE1> },
S { v: i32 },
}
#[derive(PartialEq, Debug)]
enum SErr {
Cnv { msg: String },
}
fn cnv(se: &SE) -> Result<SE1, SErr> {
match se {
SE::And { es } => {
let es: Vec<SE1> = es.iter().map(cnv).collect::<Result<Vec<_>, SErr>>()?;
Ok(SE1::And { es })
}
SE::Not { e } => {
let e = cnv(e)?;
Ok(SE1::Not { e: Box::new(e) })
}
SE::S { v } => match v.parse::<i32>() {
Ok(n) => Ok(SE1::S { v: n }),
Err(e) => Err(SErr::Cnv {
msg: format!("Error converting {v} to i32: {e}"),
}),
},
}
}
#[test]
fn test_se_conversion() {
let se = SE::And {
es: vec![
SE::Not {
e: Box::new(SE::S {
v: "23".to_string(),
}),
},
SE::S {
v: "43".to_string(),
},
],
};
let expected = SE1::And {
es: vec![
SE1::Not {
e: Box::new(SE1::S { v: 23 }),
},
SE1::S { v: 43 },
],
};
assert_eq!(cnv(&se), Ok(expected))
}
#[test]
fn test_se_conversion_err() {
let se = SE::And {
es: vec![
SE::Not {
e: Box::new(SE::S {
v: "foo".to_string(),
}),
},
SE::S {
v: "43".to_string(),
},
],
};
assert!(cnv(&se).is_err())
}
/* use super::*;
use srdf::*;
use prefix_map::PrefixMap;
#[test]
fn schema_build_test() {
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();
assert_eq!(foo.base.unwrap(),foo_from_builder.base.unwrap());
} */
}