From 2bf805f1f51850f1026ca01ed3f1a1dd743dfd23 Mon Sep 17 00:00:00 2001 From: ccamel Date: Tue, 13 Jun 2023 13:38:47 +0200 Subject: [PATCH] feat(cognitarium): add support for triples serialization --- contracts/okp4-cognitarium/src/rdf.rs | 83 ++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/contracts/okp4-cognitarium/src/rdf.rs b/contracts/okp4-cognitarium/src/rdf.rs index c3b09d84..a08a650f 100644 --- a/contracts/okp4-cognitarium/src/rdf.rs +++ b/contracts/okp4-cognitarium/src/rdf.rs @@ -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 { parser: TriplesParserKind, } +pub struct TripleWriter { + writer: TriplesWriterKind, +} + #[allow(clippy::large_enum_variant)] pub enum TriplesParserKind { NTriples(NTriplesParser), @@ -18,6 +26,13 @@ pub enum TriplesParserKind { NQuads(NQuadsParser), } +pub enum TriplesWriterKind { + NTriples(NTriplesFormatter), + Turtle(TurtleFormatter), + RdfXml(io::Result>), + NQuads(NQuadsFormatter), +} + impl TripleReader { pub fn new(format: DataFormat, src: R) -> Self { TripleReader { @@ -52,6 +67,68 @@ impl TripleReader { } } +impl TripleWriter { + 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 = None; for delim in ['#', '/', ':'] {