extern crate async_std;
extern crate iref;
#[macro_use]
extern crate static_iref;
extern crate json_ld;
use iref::Iri;
use json_ld::{context, Document, Lexicon, NoLoader, Object};
use serde_json::Value;
use std::convert::TryFrom;
#[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 iref::AsIri for Foaf {
fn as_iri(&self) -> Iri {
match self {
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 doc: Value = serde_json::from_str(
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 mut loader = NoLoader::<Value>::new();
let expanded_doc = doc
.expand::<context::Json<Value, Id>, _>(&mut loader)
.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());
}
}
}
}