Skip to content

Commit

Permalink
chore(cognitarium): add atoms definition to help with rdf serde
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Jun 17, 2023
1 parent 74fa932 commit 91326eb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
38 changes: 38 additions & 0 deletions contracts/okp4-cognitarium/src/rdf/atom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum Value {
NamedNode(String),
BlankNode(String),
LiteralSimple(String),
LiteralLang(String, String),
LiteralDatatype(String, String),
}

use std::fmt;
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Value::NamedNode(s) => write!(f, "{}", s),
Value::BlankNode(s) => write!(f, "{}", s),
Value::LiteralSimple(s) => write!(f, "{}", s),
Value::LiteralLang(s, l) => write!(f, "{}@{}", s, l),
Value::LiteralDatatype(s, d) => write!(f, "{}^^{}", s, d),
}
}
}

#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct Atom {
pub subject: String,
pub property: String,
pub value: Value,
}

impl std::fmt::Display for Atom {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
fmt.write_str(&format!(
"<{}> <{}> '{}'",
self.subject, self.property, self.value
))?;
Ok(())
}
}
2 changes: 2 additions & 0 deletions contracts/okp4-cognitarium/src/rdf/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod serde;
mod uri;
mod atom;

pub use self::serde::*;
pub use self::uri::*;
pub use self::atom::*;
20 changes: 0 additions & 20 deletions contracts/okp4-cognitarium/src/rdf/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,3 @@ impl<W: std::io::Write> TripleWriter<W> {
}
}
}

// Convenient type which simplifies the management of the lifetime of the IRI
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub struct OwnedNamedNode {
pub iri: String,
}

impl From<NamedNode<'_>> for OwnedNamedNode {
fn from(n: NamedNode<'_>) -> Self {
Self {
iri: n.iri.to_owned(),
}
}
}

impl<'a> From<&'a OwnedNamedNode> for NamedNode<'a> {
fn from(n: &'a OwnedNamedNode) -> Self {
Self { iri: &n.iri }
}
}

0 comments on commit 91326eb

Please sign in to comment.