Skip to content

Commit

Permalink
feat(cognitarium): add support for triples serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Jun 17, 2023
1 parent db4f1be commit 2bf805f
Showing 1 changed file with 80 additions and 3 deletions.
83 changes: 80 additions & 3 deletions contracts/okp4-cognitarium/src/rdf.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
use crate::msg::DataFormat;
use cosmwasm_std::{StdError, StdResult};
use rio_api::formatter::TriplesFormatter;
use rio_api::model::{Quad, Triple};
use rio_api::parser::{QuadsParser, TriplesParser};
use rio_turtle::{NQuadsParser, NTriplesParser, TurtleError, TurtleParser};
use rio_xml::{RdfXmlError, RdfXmlParser};
use std::io::BufRead;
use rio_turtle::{
NQuadsFormatter, NQuadsParser, NTriplesFormatter, NTriplesParser, TurtleError, TurtleFormatter,
TurtleParser,
};
use rio_xml::{RdfXmlError, RdfXmlFormatter, RdfXmlParser};
use std::io::{self, BufRead};

pub struct TripleReader<R: BufRead> {
parser: TriplesParserKind<R>,
}

pub struct TripleWriter<W: std::io::Write> {
writer: TriplesWriterKind<W>,
}

#[allow(clippy::large_enum_variant)]
pub enum TriplesParserKind<R: BufRead> {
NTriples(NTriplesParser<R>),
Expand All @@ -18,6 +26,13 @@ pub enum TriplesParserKind<R: BufRead> {
NQuads(NQuadsParser<R>),
}

pub enum TriplesWriterKind<W: std::io::Write> {
NTriples(NTriplesFormatter<W>),
Turtle(TurtleFormatter<W>),
RdfXml(io::Result<RdfXmlFormatter<W>>),
NQuads(NQuadsFormatter<W>),
}

impl<R: BufRead> TripleReader<R> {
pub fn new(format: DataFormat, src: R) -> Self {
TripleReader {
Expand Down Expand Up @@ -52,6 +67,68 @@ impl<R: BufRead> TripleReader<R> {
}
}

impl<W: std::io::Write> TripleWriter<W> {
pub fn new(format: DataFormat, dst: W) -> Self {
TripleWriter {
writer: match format {
DataFormat::RDFXml => TriplesWriterKind::RdfXml(RdfXmlFormatter::new(dst)),
DataFormat::Turtle => TriplesWriterKind::Turtle(TurtleFormatter::new(dst)),
DataFormat::NTriples => TriplesWriterKind::NTriples(NTriplesFormatter::new(dst)),
DataFormat::NQuads => TriplesWriterKind::NQuads(NQuadsFormatter::new(dst)),
},
}
}

pub fn write(&mut self, triple: &Triple<'_>) -> io::Result<()> {
match &mut self.writer {
TriplesWriterKind::Turtle(formatter) => {
formatter.format(triple)
},
TriplesWriterKind::NTriples(formatter) => {
formatter.format(triple)
},
TriplesWriterKind::NQuads(formatter) => {
use rio_api::formatter::QuadsFormatter;

let quad = &Quad {
subject: triple.subject,
predicate: triple.predicate,
object: triple.object,
graph_name: None,
};

formatter.format(quad)
},
TriplesWriterKind::RdfXml(format_result) => {
match format_result {
Ok(formatter) => formatter.format(triple),
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e.to_string())),
}
},
}
}

#[allow(dead_code)]
pub fn write_all(&mut self, triples: Vec<&Triple<'_>>) -> io::Result<()> {
for triple in triples {
self.write(triple)?;
}
Ok(())
}

pub fn finish(self) -> io::Result<()> {
match self.writer {
TriplesWriterKind::Turtle(formatter) => formatter.finish(),
TriplesWriterKind::NTriples(formatter) => formatter.finish(),
TriplesWriterKind::NQuads(formatter) => formatter.finish(),
TriplesWriterKind::RdfXml(format_result) => match format_result {
Ok(formatter) => formatter.finish(),
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e.to_string())),
},
}.map(|_| ())
}
}

pub fn explode_iri(iri: &str) -> StdResult<(String, String)> {
let mut marker_index: Option<usize> = None;
for delim in ['#', '/', ':'] {
Expand Down

0 comments on commit 2bf805f

Please sign in to comment.