use std::fmt;
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct Triple {
pub subject: Id,
pub predicate: String,
pub object: Term,
}
impl Triple {
pub const fn new(subject: Id, predicate: String, object: Term) -> Self {
Triple { subject, predicate, object }
}
}
impl fmt::Display for Triple {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?} <{}> {:?} .", self.subject, self.predicate, self.object)
}
}
impl fmt::Debug for Triple {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
pub enum Id {
Named(String),
Blank(String),
}
impl fmt::Debug for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Id::Named(iri) => write!(f, "<{iri}>"),
Id::Blank(id) => write!(f, "_:{id}"),
}
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
pub enum Term {
Id(Id),
Literal(Literal),
}
impl fmt::Debug for Term {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Term::Id(id) => id.fmt(f),
Term::Literal(lit) => lit.fmt(f),
}
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct Literal {
form: String,
datatype: Option<String>,
lang: Option<String>,
}
impl fmt::Debug for Literal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(lang) = &self.lang {
write!(f, "\"{}\"@{lang}", self.form)
} else if let Some(dtype) = &self.datatype {
write!(f, "\"{}\"^^<{dtype}>", self.form)
} else {
write!(f, "\"{}\"", self.form)
}
}
}
impl Literal {
pub const fn new(form: String) -> Self {
Literal { form, datatype: None, lang: None }
}
pub const fn new_typed(form: String, datatype: String) -> Self {
Literal { form, datatype: Some(datatype), lang: None }
}
pub fn new_lang(form: String, lang: String) -> Self {
let datatype = String::from("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString");
Literal { form, datatype: Some(datatype), lang: Some(lang) }
}
}