#![feature(proc_macro_hygiene)]
extern crate async_std;
extern crate iref;
#[macro_use]
extern crate static_iref;
extern crate json_ld;
use std::convert::TryFrom;
use iref::Iri;
use json_ld::{
JsonContext,
NoLoader,
Document,
Object,
Lexicon
};
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum Foaf {
Name,
Mbox
}
impl<'a> TryFrom<Iri<'a>> for Foaf {
type Error = ();
fn try_from(iri: Iri<'a>) -> Result<Foaf, ()> {
match iri {
_ if iri == iri!("http://xmlns.com/foaf/0.1/name") => Ok(Foaf::Name),
_ if iri == iri!("http://xmlns.com/foaf/0.1/mbox") => Ok(Foaf::Mbox),
_ => Err(())
}
}
}
impl<'a> From<&'a Foaf> for Iri<'a> {
fn from(term: &'a Foaf) -> Iri<'a> {
match term {
Foaf::Name => iri!("http://xmlns.com/foaf/0.1/name"),
Foaf::Mbox => iri!("http://xmlns.com/foaf/0.1/mbox")
}
}
}
type Id = Lexicon<Foaf>;
#[async_std::main]
async fn main() {
let context: JsonContext<Id> = JsonContext::new(None);
let doc = json::parse(r#"
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"email": "http://xmlns.com/foaf/0.1/mbox"
},
"@id": "timothee.haudebourg.net",
"name": "Timothée Haudebourg",
"email": "author@haudebourg.net"
}
"#).unwrap();
let expanded_doc = doc.expand(&context, &mut NoLoader).await.unwrap();
for object in expanded_doc {
if let Object::Node(node) = object.as_ref() {
println!("node: {}", node.id().unwrap()); for name in node.get(Foaf::Name) { println!("name: {}", name.as_str().unwrap());
}
for name in node.get(Foaf::Mbox) { println!("email: {}", name.as_str().unwrap());
}
}
}
}