Skip to content

Commit

Permalink
feat(cagnotarium): add nquads insert support
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Jun 1, 2023
1 parent 3ebeefe commit f963d21
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions contracts/okp4-cognitarium/src/rdf.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
use crate::msg::DataFormat;
use cosmwasm_std::{StdError, StdResult};
use rio_api::model::Triple;
use rio_api::parser::TriplesParser;
use rio_turtle::{NTriplesParser, TurtleError, TurtleParser};
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;

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

#[allow(clippy::large_enum_variant)]
pub enum TriplesParserKind<R: BufRead> {
NTriples(NTriplesParser<R>),
Turtle(TurtleParser<R>),
RdfXml(RdfXmlParser<R>),
NQuads,
NQuads(NQuadsParser<R>),
}

pub fn read_triples<R: BufRead>(format: DataFormat, src: R) -> TripleReader<R> {
TripleReader::new(match format {
DataFormat::RDFXml => TriplesParserKind::RdfXml(RdfXmlParser::new(src, None)),
DataFormat::Turtle => TriplesParserKind::Turtle(TurtleParser::new(src, None)),
DataFormat::NTriples => TriplesParserKind::NTriples(NTriplesParser::new(src)),
DataFormat::NQuads => TriplesParserKind::NQuads,
DataFormat::NQuads => TriplesParserKind::NQuads(NQuadsParser::new(src)),
})
}

Expand All @@ -40,7 +41,15 @@ impl<R: BufRead> TripleReader<R> {
TriplesParserKind::NTriples(parser) => parser.parse_all(&mut use_fn),
TriplesParserKind::Turtle(parser) => parser.parse_all(&mut use_fn),
TriplesParserKind::RdfXml(parser) => parser.parse_all(&mut use_fn),
TriplesParserKind::NQuads => Ok(()),
TriplesParserKind::NQuads(parser) => {
parser.parse_all(&mut |quad: Quad| -> Result<(), E> {
use_fn(Triple {
subject: quad.subject,
predicate: quad.predicate,
object: quad.object,
})
})
}
}
}
}
Expand Down

0 comments on commit f963d21

Please sign in to comment.