diff --git a/js/src/store.rs b/js/src/store.rs index 3df65bec..36324ee6 100644 --- a/js/src/store.rs +++ b/js/src/store.rs @@ -2,9 +2,10 @@ use crate::format_err; use crate::model::*; use crate::utils::to_err; use js_sys::{Array, Map}; +use oxigraph::io::{DatasetFormat, GraphFormat}; use oxigraph::model::GraphName; use oxigraph::sparql::{QueryOptions, QueryResult}; -use oxigraph::{DatasetSyntax, GraphSyntax, MemoryStore}; +use oxigraph::MemoryStore; use std::convert::TryInto; use std::io::Cursor; use wasm_bindgen::prelude::*; @@ -157,23 +158,23 @@ impl JsMemoryStore { None }; - if let Some(graph_syntax) = GraphSyntax::from_media_type(mime_type) { + if let Some(graph_format) = GraphFormat::from_media_type(mime_type) { self.store .load_graph( Cursor::new(data), - graph_syntax, + graph_format, &to_graph_name.unwrap_or(GraphName::DefaultGraph), base_iri.as_deref(), ) .map_err(to_err) - } else if let Some(dataset_syntax) = DatasetSyntax::from_media_type(mime_type) { + } else if let Some(dataset_format) = DatasetFormat::from_media_type(mime_type) { if to_graph_name.is_some() { return Err(format_err!( "The target graph name parameter is not available for dataset formats" )); } self.store - .load_dataset(Cursor::new(data), dataset_syntax, base_iri.as_deref()) + .load_dataset(Cursor::new(data), dataset_format, base_iri.as_deref()) .map_err(to_err) } else { Err(format_err!("Not supported MIME type: {}", mime_type)) diff --git a/lib/src/io/syntax.rs b/lib/src/io/format.rs similarity index 57% rename from lib/src/io/syntax.rs rename to lib/src/io/format.rs index 4eb3ecb7..5789e478 100644 --- a/lib/src/io/syntax.rs +++ b/lib/src/io/format.rs @@ -1,6 +1,6 @@ /// A file serialization format. /// -/// Is implemented by `GraphSyntax` for graph files and `DatasetSyntax` for dataset files. +/// Is implemented by `GraphFormat` for graph files and `DatasetFormat` for dataset files. #[deprecated(note = "Use directly the methods on the implementing types")] pub trait FileSyntax: Sized { /// Its canonical IRI according to the [Unique URIs for file formats registry](https://www.w3.org/ns/formats/). @@ -12,16 +12,16 @@ pub trait FileSyntax: Sized { /// Its [IANA-registered](https://tools.ietf.org/html/rfc2046) file extension. fn file_extension(self) -> &'static str; - /// Looks for a known syntax from a media type. + /// Looks for a known format from a media type. fn from_mime_type(media_type: &str) -> Option; } /// [RDF graph](https://www.w3.org/TR/rdf11-concepts/#dfn-graph) serialization formats. /// -/// This enumeration is non exhaustive. New syntaxes like JSON-LD will be added in the future. +/// This enumeration is non exhaustive. New formats like JSON-LD will be added in the future. #[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)] #[non_exhaustive] -pub enum GraphSyntax { +pub enum GraphFormat { /// [N-Triples](https://www.w3.org/TR/n-triples/) NTriples, /// [Turtle](https://www.w3.org/TR/turtle/) @@ -30,70 +30,70 @@ pub enum GraphSyntax { RdfXml, } -impl GraphSyntax { - /// The syntax canonical IRI according to the [Unique URIs for file formats registry](https://www.w3.org/ns/formats/). +impl GraphFormat { + /// The format canonical IRI according to the [Unique URIs for file formats registry](https://www.w3.org/ns/formats/). /// /// ``` - /// use oxigraph::io::GraphSyntax; + /// use oxigraph::io::GraphFormat; /// - /// assert_eq!(GraphSyntax::NTriples.iri(), "http://www.w3.org/ns/formats/N-Triples") + /// assert_eq!(GraphFormat::NTriples.iri(), "http://www.w3.org/ns/formats/N-Triples") /// ``` pub fn iri(self) -> &'static str { match self { - GraphSyntax::NTriples => "http://www.w3.org/ns/formats/N-Triples", - GraphSyntax::Turtle => "http://www.w3.org/ns/formats/Turtle", - GraphSyntax::RdfXml => "http://www.w3.org/ns/formats/RDF_XML", + GraphFormat::NTriples => "http://www.w3.org/ns/formats/N-Triples", + GraphFormat::Turtle => "http://www.w3.org/ns/formats/Turtle", + GraphFormat::RdfXml => "http://www.w3.org/ns/formats/RDF_XML", } } - /// The syntax [IANA media type](https://tools.ietf.org/html/rfc2046). + /// The format [IANA media type](https://tools.ietf.org/html/rfc2046). /// /// ``` - /// use oxigraph::io::GraphSyntax; + /// use oxigraph::io::GraphFormat; /// - /// assert_eq!(GraphSyntax::NTriples.media_type(), "application/n-triples") + /// assert_eq!(GraphFormat::NTriples.media_type(), "application/n-triples") /// ``` pub fn media_type(self) -> &'static str { match self { - GraphSyntax::NTriples => "application/n-triples", - GraphSyntax::Turtle => "text/turtle", - GraphSyntax::RdfXml => "application/rdf+xml", + GraphFormat::NTriples => "application/n-triples", + GraphFormat::Turtle => "text/turtle", + GraphFormat::RdfXml => "application/rdf+xml", } } - /// The syntax [IANA-registered](https://tools.ietf.org/html/rfc2046) file extension. + /// The format [IANA-registered](https://tools.ietf.org/html/rfc2046) file extension. /// /// ``` - /// use oxigraph::io::GraphSyntax; + /// use oxigraph::io::GraphFormat; /// - /// assert_eq!(GraphSyntax::NTriples.file_extension(), "nt") + /// assert_eq!(GraphFormat::NTriples.file_extension(), "nt") /// ``` pub fn file_extension(self) -> &'static str { match self { - GraphSyntax::NTriples => "nt", - GraphSyntax::Turtle => "ttl", - GraphSyntax::RdfXml => "rdf", + GraphFormat::NTriples => "nt", + GraphFormat::Turtle => "ttl", + GraphFormat::RdfXml => "rdf", } } - /// Looks for a known syntax from a media type. + /// Looks for a known format from a media type. /// /// It supports some media type aliases. - /// For example "application/xml" is going to return `GraphSyntax::RdfXml` even if it is not its canonical media type. + /// For example "application/xml" is going to return `GraphFormat::RdfXml` even if it is not its canonical media type. /// /// Example: /// ``` - /// use oxigraph::io::GraphSyntax; + /// use oxigraph::io::GraphFormat; /// - /// assert_eq!(GraphSyntax::from_media_type("text/turtle; charset=utf-8"), Some(GraphSyntax::Turtle)) + /// assert_eq!(GraphFormat::from_media_type("text/turtle; charset=utf-8"), Some(GraphFormat::Turtle)) /// ``` pub fn from_media_type(media_type: &str) -> Option { if let Some(base_type) = media_type.split(';').next() { match base_type.trim() { - "application/n-triples" | "text/plain" => Some(GraphSyntax::NTriples), + "application/n-triples" | "text/plain" => Some(GraphFormat::NTriples), "text/turtle" | "application/turtle" | "application/x-turtle" => { - Some(GraphSyntax::Turtle) + Some(GraphFormat::Turtle) } - "application/rdf+xml" | "application/xml" | "text/xml" => Some(GraphSyntax::RdfXml), + "application/rdf+xml" | "application/xml" | "text/xml" => Some(GraphFormat::RdfXml), _ => None, } } else { @@ -103,7 +103,7 @@ impl GraphSyntax { } #[allow(deprecated)] -impl FileSyntax for GraphSyntax { +impl FileSyntax for GraphFormat { fn iri(self) -> &'static str { self.iri() } @@ -123,75 +123,75 @@ impl FileSyntax for GraphSyntax { /// [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) serialization formats. /// -/// This enumeration is non exhaustive. New syntaxes like JSON-LD will be added in the future. +/// This enumeration is non exhaustive. New formats like JSON-LD will be added in the future. #[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)] #[non_exhaustive] -pub enum DatasetSyntax { +pub enum DatasetFormat { /// [N-Quads](https://www.w3.org/TR/n-quads/) NQuads, /// [TriG](https://www.w3.org/TR/trig/) TriG, } -impl DatasetSyntax { - /// The syntax canonical IRI according to the [Unique URIs for file formats registry](https://www.w3.org/ns/formats/). +impl DatasetFormat { + /// The format canonical IRI according to the [Unique URIs for file formats registry](https://www.w3.org/ns/formats/). /// /// ``` - /// use oxigraph::io::DatasetSyntax; + /// use oxigraph::io::DatasetFormat; /// - /// assert_eq!(DatasetSyntax::NQuads.iri(), "http://www.w3.org/ns/formats/N-Quads") + /// assert_eq!(DatasetFormat::NQuads.iri(), "http://www.w3.org/ns/formats/N-Quads") /// ``` pub fn iri(self) -> &'static str { match self { - DatasetSyntax::NQuads => "http://www.w3.org/ns/formats/N-Quads", - DatasetSyntax::TriG => "http://www.w3.org/ns/formats/TriG", + DatasetFormat::NQuads => "http://www.w3.org/ns/formats/N-Quads", + DatasetFormat::TriG => "http://www.w3.org/ns/formats/TriG", } } - /// The syntax [IANA media type](https://tools.ietf.org/html/rfc2046). + /// The format [IANA media type](https://tools.ietf.org/html/rfc2046). /// /// ``` - /// use oxigraph::io::DatasetSyntax; + /// use oxigraph::io::DatasetFormat; /// - /// assert_eq!(DatasetSyntax::NQuads.media_type(), "application/n-quads") + /// assert_eq!(DatasetFormat::NQuads.media_type(), "application/n-quads") /// ``` pub fn media_type(self) -> &'static str { match self { - DatasetSyntax::NQuads => "application/n-quads", - DatasetSyntax::TriG => "application/trig", + DatasetFormat::NQuads => "application/n-quads", + DatasetFormat::TriG => "application/trig", } } - /// The syntax [IANA-registered](https://tools.ietf.org/html/rfc2046) file extension. + /// The format [IANA-registered](https://tools.ietf.org/html/rfc2046) file extension. /// /// ``` - /// use oxigraph::io::DatasetSyntax; + /// use oxigraph::io::DatasetFormat; /// - /// assert_eq!(DatasetSyntax::NQuads.file_extension(), "nq") + /// assert_eq!(DatasetFormat::NQuads.file_extension(), "nq") /// ``` pub fn file_extension(self) -> &'static str { match self { - DatasetSyntax::NQuads => "nq", - DatasetSyntax::TriG => "trig", + DatasetFormat::NQuads => "nq", + DatasetFormat::TriG => "trig", } } - /// Looks for a known syntax from a media type. + /// Looks for a known format from a media type. /// /// It supports some media type aliases. /// /// Example: /// ``` - /// use oxigraph::io::DatasetSyntax; + /// use oxigraph::io::DatasetFormat; /// - /// assert_eq!(DatasetSyntax::from_media_type("application/n-quads; charset=utf-8"), Some(DatasetSyntax::NQuads)) + /// assert_eq!(DatasetFormat::from_media_type("application/n-quads; charset=utf-8"), Some(DatasetFormat::NQuads)) /// ``` pub fn from_media_type(media_type: &str) -> Option { if let Some(base_type) = media_type.split(';').next() { match base_type.trim() { "application/n-quads" | "text/x-nquads" | "text/nquads" => { - Some(DatasetSyntax::NQuads) + Some(DatasetFormat::NQuads) } - "application/trig" | "application/x-trig" => Some(DatasetSyntax::TriG), + "application/trig" | "application/x-trig" => Some(DatasetFormat::TriG), _ => None, } } else { @@ -201,7 +201,7 @@ impl DatasetSyntax { } #[allow(deprecated)] -impl FileSyntax for DatasetSyntax { +impl FileSyntax for DatasetFormat { fn iri(self) -> &'static str { self.iri() } diff --git a/lib/src/io/mod.rs b/lib/src/io/mod.rs index 2e547b47..1b3fb6c3 100644 --- a/lib/src/io/mod.rs +++ b/lib/src/io/mod.rs @@ -1,14 +1,14 @@ //! Utilities to read and write RDF graphs and datasets +mod format; pub mod read; -mod syntax; pub mod write; +pub use self::format::DatasetFormat; +#[allow(deprecated)] +pub use self::format::FileSyntax; +pub use self::format::GraphFormat; pub use self::read::DatasetParser; pub use self::read::GraphParser; -pub use self::syntax::DatasetSyntax; -#[allow(deprecated)] -pub use self::syntax::FileSyntax; -pub use self::syntax::GraphSyntax; pub use self::write::DatasetSerializer; pub use self::write::GraphSerializer; diff --git a/lib/src/io/read.rs b/lib/src/io/read.rs index 49a256b1..4687edad 100644 --- a/lib/src/io/read.rs +++ b/lib/src/io/read.rs @@ -1,8 +1,7 @@ //! Utilities to read RDF graphs and datasets -use super::GraphSyntax; +use crate::io::{DatasetFormat, GraphFormat}; use crate::model::*; -use crate::DatasetSyntax; use oxiri::{Iri, IriParseError}; use rio_api::model as rio; use rio_api::parser::{QuadsParser, TriplesParser}; @@ -16,17 +15,17 @@ use std::io::BufRead; /// A reader for RDF graph serialization formats. /// /// It currently supports the following formats: -/// * [N-Triples](https://www.w3.org/TR/n-triples/) (`GraphSyntax::NTriples`) -/// * [Turtle](https://www.w3.org/TR/turtle/) (`GraphSyntax::Turtle`) -/// * [RDF XML](https://www.w3.org/TR/rdf-syntax-grammar/) (`GraphSyntax::RdfXml`) +/// * [N-Triples](https://www.w3.org/TR/n-triples/) (`GraphFormat::NTriples`) +/// * [Turtle](https://www.w3.org/TR/turtle/) (`GraphFormat::Turtle`) +/// * [RDF XML](https://www.w3.org/TR/rdf-syntax-grammar/) (`GraphFormat::RdfXml`) /// /// ``` -/// use oxigraph::io::{GraphSyntax, GraphParser}; +/// use oxigraph::io::{GraphFormat, GraphParser}; /// use std::io::Cursor; /// /// let file = " ."; /// -/// let parser = GraphParser::from_syntax(GraphSyntax::NTriples); +/// let parser = GraphParser::from_format(GraphFormat::NTriples); /// let triples = parser.read_triples(Cursor::new(file))?.collect::,_>>()?; /// ///assert_eq!(triples.len(), 1); @@ -34,14 +33,14 @@ use std::io::BufRead; /// # std::io::Result::Ok(()) /// ``` pub struct GraphParser { - syntax: GraphSyntax, + format: GraphFormat, base_iri: String, } impl GraphParser { - pub fn from_syntax(syntax: GraphSyntax) -> Self { + pub fn from_format(format: GraphFormat) -> Self { Self { - syntax, + format, base_iri: String::new(), } } @@ -49,12 +48,12 @@ impl GraphParser { /// Provides an IRI that could be used to resolve the file relative IRIs /// /// ``` - /// use oxigraph::io::{GraphSyntax, GraphParser}; + /// use oxigraph::io::{GraphFormat, GraphParser}; /// use std::io::Cursor; /// /// let file = "

."; /// - /// let parser = GraphParser::from_syntax(GraphSyntax::Turtle).with_base_iri("http://example.com")?; + /// let parser = GraphParser::from_format(GraphFormat::Turtle).with_base_iri("http://example.com")?; /// let triples = parser.read_triples(Cursor::new(file))?.collect::,_>>()?; /// ///assert_eq!(triples.len(), 1); @@ -71,14 +70,14 @@ impl GraphParser { //TODO: drop the error when possible Ok(TripleReader { mapper: RioMapper::default(), - parser: match self.syntax { - GraphSyntax::NTriples => { + parser: match self.format { + GraphFormat::NTriples => { TripleReaderKind::NTriples(NTriplesParser::new(reader).map_err(invalid_input)?) } - GraphSyntax::Turtle => TripleReaderKind::Turtle( + GraphFormat::Turtle => TripleReaderKind::Turtle( TurtleParser::new(reader, &self.base_iri).map_err(invalid_input)?, ), - GraphSyntax::RdfXml => TripleReaderKind::RdfXml( + GraphFormat::RdfXml => TripleReaderKind::RdfXml( RdfXmlParser::new(reader, &self.base_iri).map_err(invalid_input)?, ), }, @@ -91,12 +90,12 @@ impl GraphParser { /// Could be built using a `GraphParser`. /// /// ``` -/// use oxigraph::io::{GraphSyntax, GraphParser}; +/// use oxigraph::io::{GraphFormat, GraphParser}; /// use std::io::Cursor; /// /// let file = " ."; /// -/// let parser = GraphParser::from_syntax(GraphSyntax::NTriples); +/// let parser = GraphParser::from_format(GraphFormat::NTriples); /// let triples = parser.read_triples(Cursor::new(file))?.collect::,_>>()?; /// ///assert_eq!(triples.len(), 1); @@ -165,16 +164,16 @@ impl TripleReader { /// A reader for RDF dataset serialization formats. /// /// It currently supports the following formats: -/// * [N-Quads](https://www.w3.org/TR/n-quads/) (`DatasetSyntax::NQuads`) -/// * [TriG](https://www.w3.org/TR/trig/) (`DatasetSyntax::TriG`) +/// * [N-Quads](https://www.w3.org/TR/n-quads/) (`DatasetFormat::NQuads`) +/// * [TriG](https://www.w3.org/TR/trig/) (`DatasetFormat::TriG`) /// /// ``` -/// use oxigraph::io::{DatasetSyntax, DatasetParser}; +/// use oxigraph::io::{DatasetFormat, DatasetParser}; /// use std::io::Cursor; /// /// let file = " ."; /// -/// let parser = DatasetParser::from_syntax(DatasetSyntax::NQuads); +/// let parser = DatasetParser::from_format(DatasetFormat::NQuads); /// let quads = parser.read_quads(Cursor::new(file))?.collect::,_>>()?; /// ///assert_eq!(quads.len(), 1); @@ -182,14 +181,14 @@ impl TripleReader { /// # std::io::Result::Ok(()) /// ``` pub struct DatasetParser { - syntax: DatasetSyntax, + format: DatasetFormat, base_iri: String, } impl DatasetParser { - pub fn from_syntax(syntax: DatasetSyntax) -> Self { + pub fn from_format(format: DatasetFormat) -> Self { Self { - syntax, + format, base_iri: String::new(), } } @@ -197,12 +196,12 @@ impl DatasetParser { /// Provides an IRI that could be used to resolve the file relative IRIs /// /// ``` - /// use oxigraph::io::{DatasetSyntax, DatasetParser}; + /// use oxigraph::io::{DatasetFormat, DatasetParser}; /// use std::io::Cursor; /// /// let file = " {

}"; /// - /// let parser = DatasetParser::from_syntax(DatasetSyntax::TriG).with_base_iri("http://example.com")?; + /// let parser = DatasetParser::from_format(DatasetFormat::TriG).with_base_iri("http://example.com")?; /// let triples = parser.read_quads(Cursor::new(file))?.collect::,_>>()?; /// ///assert_eq!(triples.len(), 1); @@ -219,11 +218,11 @@ impl DatasetParser { //TODO: drop the error when possible Ok(QuadReader { mapper: RioMapper::default(), - parser: match self.syntax { - DatasetSyntax::NQuads => { + parser: match self.format { + DatasetFormat::NQuads => { QuadReaderKind::NQuads(NQuadsParser::new(reader).map_err(invalid_input)?) } - DatasetSyntax::TriG => QuadReaderKind::TriG( + DatasetFormat::TriG => QuadReaderKind::TriG( TriGParser::new(reader, &self.base_iri).map_err(invalid_input)?, ), }, @@ -236,12 +235,12 @@ impl DatasetParser { /// Could be built using a `DatasetParser`. /// /// ``` -/// use oxigraph::io::{DatasetSyntax, DatasetParser}; +/// use oxigraph::io::{DatasetFormat, DatasetParser}; /// use std::io::Cursor; /// /// let file = " ."; /// -/// let parser = DatasetParser::from_syntax(DatasetSyntax::NQuads); +/// let parser = DatasetParser::from_format(DatasetFormat::NQuads); /// let quads = parser.read_quads(Cursor::new(file))?.collect::,_>>()?; /// ///assert_eq!(quads.len(), 1); diff --git a/lib/src/io/write.rs b/lib/src/io/write.rs index ad16915f..3df0d665 100644 --- a/lib/src/io/write.rs +++ b/lib/src/io/write.rs @@ -1,8 +1,7 @@ //! Utilities to write RDF graphs and datasets -use super::GraphSyntax; +use crate::io::{DatasetFormat, GraphFormat}; use crate::model::*; -use crate::DatasetSyntax; use rio_api::formatter::{QuadsFormatter, TriplesFormatter}; use rio_turtle::{NQuadsFormatter, NTriplesFormatter, TriGFormatter, TurtleFormatter}; use rio_xml::{RdfXmlError, RdfXmlFormatter}; @@ -12,16 +11,16 @@ use std::io::Write; /// A serializer for RDF graph serialization formats. /// /// It currently supports the following formats: -/// * [N-Triples](https://www.w3.org/TR/n-triples/) (`GraphSyntax::NTriples`) -/// * [Turtle](https://www.w3.org/TR/turtle/) (`GraphSyntax::Turtle`) -/// * [RDF XML](https://www.w3.org/TR/rdf-syntax-grammar/) (`GraphSyntax::RdfXml`) +/// * [N-Triples](https://www.w3.org/TR/n-triples/) (`GraphFormat::NTriples`) +/// * [Turtle](https://www.w3.org/TR/turtle/) (`GraphFormat::Turtle`) +/// * [RDF XML](https://www.w3.org/TR/rdf-syntax-grammar/) (`GraphFormat::RdfXml`) /// /// ``` -/// use oxigraph::io::{GraphSyntax, GraphSerializer}; +/// use oxigraph::io::{GraphFormat, GraphSerializer}; /// use oxigraph::model::*; /// /// let mut buffer = Vec::new(); -/// let mut writer = GraphSerializer::from_syntax(GraphSyntax::NTriples).triple_writer(&mut buffer)?; +/// let mut writer = GraphSerializer::from_format(GraphFormat::NTriples).triple_writer(&mut buffer)?; /// writer.write(&Triple { /// subject: NamedNode::new("http://example.com/s")?.into(), /// predicate: NamedNode::new("http://example.com/p")?, @@ -34,21 +33,21 @@ use std::io::Write; /// ``` #[allow(missing_copy_implementations)] pub struct GraphSerializer { - syntax: GraphSyntax, + format: GraphFormat, } impl GraphSerializer { - pub fn from_syntax(syntax: GraphSyntax) -> Self { - Self { syntax } + pub fn from_format(format: GraphFormat) -> Self { + Self { format } } /// Returns a `TripleWriter` allowing writing triples into the given `Write` implementation pub fn triple_writer(&self, writer: W) -> Result, io::Error> { Ok(TripleWriter { - formatter: match self.syntax { - GraphSyntax::NTriples => TripleWriterKind::NTriples(NTriplesFormatter::new(writer)), - GraphSyntax::Turtle => TripleWriterKind::Turtle(TurtleFormatter::new(writer)), - GraphSyntax::RdfXml => { + formatter: match self.format { + GraphFormat::NTriples => TripleWriterKind::NTriples(NTriplesFormatter::new(writer)), + GraphFormat::Turtle => TripleWriterKind::Turtle(TurtleFormatter::new(writer)), + GraphFormat::RdfXml => { TripleWriterKind::RdfXml(RdfXmlFormatter::new(writer).map_err(map_xml_err)?) } }, @@ -62,11 +61,11 @@ impl GraphSerializer { /// Warning: Do not forget to run the `finish` method to properly write the last bytes of the file. /// /// ``` -/// use oxigraph::io::{GraphSyntax, GraphSerializer}; +/// use oxigraph::io::{GraphFormat, GraphSerializer}; /// use oxigraph::model::*; /// /// let mut buffer = Vec::new(); -/// let mut writer = GraphSerializer::from_syntax(GraphSyntax::NTriples).triple_writer(&mut buffer)?; +/// let mut writer = GraphSerializer::from_format(GraphFormat::NTriples).triple_writer(&mut buffer)?; /// writer.write(&Triple { /// subject: NamedNode::new("http://example.com/s")?.into(), /// predicate: NamedNode::new("http://example.com/p")?, @@ -114,15 +113,15 @@ impl TripleWriter { /// A serializer for RDF graph serialization formats. /// /// It currently supports the following formats: -/// * [N-Quads](https://www.w3.org/TR/n-quads/) (`DatasetSyntax::NQuads`) -/// * [TriG](https://www.w3.org/TR/trig/) (`DatasetSyntax::TriG`) +/// * [N-Quads](https://www.w3.org/TR/n-quads/) (`DatasetFormat::NQuads`) +/// * [TriG](https://www.w3.org/TR/trig/) (`DatasetFormat::TriG`) /// /// ``` -/// use oxigraph::io::{DatasetSyntax, DatasetSerializer}; +/// use oxigraph::io::{DatasetFormat, DatasetSerializer}; /// use oxigraph::model::*; /// /// let mut buffer = Vec::new(); -/// let mut writer = DatasetSerializer::from_syntax(DatasetSyntax::NQuads).quad_writer(&mut buffer)?; +/// let mut writer = DatasetSerializer::from_format(DatasetFormat::NQuads).quad_writer(&mut buffer)?; /// writer.write(&Quad { /// subject: NamedNode::new("http://example.com/s")?.into(), /// predicate: NamedNode::new("http://example.com/p")?, @@ -136,20 +135,20 @@ impl TripleWriter { /// ``` #[allow(missing_copy_implementations)] pub struct DatasetSerializer { - syntax: DatasetSyntax, + format: DatasetFormat, } impl DatasetSerializer { - pub fn from_syntax(syntax: DatasetSyntax) -> Self { - Self { syntax } + pub fn from_format(format: DatasetFormat) -> Self { + Self { format } } /// Returns a `QuadWriter` allowing writing triples into the given `Write` implementation pub fn quad_writer(&self, writer: W) -> Result, io::Error> { Ok(QuadWriter { - formatter: match self.syntax { - DatasetSyntax::NQuads => QuadWriterKind::NQuads(NQuadsFormatter::new(writer)), - DatasetSyntax::TriG => QuadWriterKind::TriG(TriGFormatter::new(writer)), + formatter: match self.format { + DatasetFormat::NQuads => QuadWriterKind::NQuads(NQuadsFormatter::new(writer)), + DatasetFormat::TriG => QuadWriterKind::TriG(TriGFormatter::new(writer)), }, }) } @@ -161,11 +160,11 @@ impl DatasetSerializer { /// Warning: Do not forget to run the `finish` method to properly write the last bytes of the file. /// /// ``` -/// use oxigraph::io::{DatasetSyntax, DatasetSerializer}; +/// use oxigraph::io::{DatasetFormat, DatasetSerializer}; /// use oxigraph::model::*; /// /// let mut buffer = Vec::new(); -/// let mut writer = DatasetSerializer::from_syntax(DatasetSyntax::NQuads).quad_writer(&mut buffer)?; +/// let mut writer = DatasetSerializer::from_format(DatasetFormat::NQuads).quad_writer(&mut buffer)?; /// writer.write(&Quad { /// subject: NamedNode::new("http://example.com/s")?.into(), /// predicate: NamedNode::new("http://example.com/p")?, diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 8d159466..c97664eb 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -111,13 +111,13 @@ pub mod store; pub use error::Error; pub type Result = ::std::result::Result; -#[deprecated(note = "Use oxigraph::io::DatasetSyntax instead")] -pub use crate::io::DatasetSyntax; +#[deprecated(note = "Use oxigraph::io::DatasetFormat instead")] +pub type DatasetSyntax = crate::io::DatasetFormat; #[deprecated(note = "Use oxigraph::io::FileSyntax instead")] #[allow(deprecated)] pub use crate::io::FileSyntax; -#[deprecated(note = "Use oxigraph::io::GraphSyntax instead")] -pub use crate::io::GraphSyntax; +#[deprecated(note = "Use oxigraph::io::GraphFormat instead")] +pub type GraphSyntax = crate::io::GraphFormat; pub use crate::store::memory::MemoryStore; #[cfg(feature = "rocksdb")] pub use crate::store::rocksdb::RocksDbStore; diff --git a/lib/src/sparql/mod.rs b/lib/src/sparql/mod.rs index 7af2f357..fb685eb7 100644 --- a/lib/src/sparql/mod.rs +++ b/lib/src/sparql/mod.rs @@ -25,7 +25,9 @@ pub use crate::sparql::model::QueryTriplesIterator; #[deprecated(note = "Please directly use QuerySolutionsIterator type instead")] pub type BindingsIterator<'a> = QuerySolutionsIterator; pub use crate::sparql::model::QueryResult; -pub use crate::sparql::model::QueryResultSyntax; +pub use crate::sparql::model::QueryResultFormat; +#[deprecated(note = "Use QueryResultFormat instead")] +pub type QueryResultSyntax = QueryResultFormat; pub use crate::sparql::model::Variable; pub use crate::sparql::parser::Query; pub use crate::sparql::parser::SparqlParseError; diff --git a/lib/src/sparql/model.rs b/lib/src/sparql/model.rs index 5a863b62..6274dbd7 100644 --- a/lib/src/sparql/model.rs +++ b/lib/src/sparql/model.rs @@ -1,6 +1,6 @@ use crate::io::GraphSerializer; #[allow(deprecated)] -use crate::io::{FileSyntax, GraphSyntax}; +use crate::io::{FileSyntax, GraphFormat}; use crate::model::*; use crate::sparql::json_results::write_json_results; use crate::sparql::xml_results::{read_xml_results, write_xml_results}; @@ -21,10 +21,10 @@ pub enum QueryResult { } impl QueryResult { - pub fn read(reader: impl BufRead + 'static, syntax: QueryResultSyntax) -> Result { - match syntax { - QueryResultSyntax::Xml => read_xml_results(reader), - QueryResultSyntax::Json => Err(Error::msg( + pub fn read(reader: impl BufRead + 'static, format: QueryResultFormat) -> Result { + match format { + QueryResultFormat::Xml => read_xml_results(reader), + QueryResultFormat::Json => Err(Error::msg( //TODO: implement "JSON SPARQL results format parsing has not been implemented yet", )), @@ -38,21 +38,21 @@ impl QueryResult { /// ``` /// use oxigraph::MemoryStore; /// use oxigraph::model::*; - /// use oxigraph::sparql::{QueryOptions, QueryResultSyntax}; + /// use oxigraph::sparql::{QueryOptions, QueryResultFormat}; /// /// let store = MemoryStore::new(); /// let ex = NamedNode::new("http://example.com")?; /// store.insert(Quad::new(ex.clone(), ex.clone(), ex.clone(), None)); /// /// let mut results = Vec::new(); - /// store.query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default())?.write(&mut results, QueryResultSyntax::Json)?; + /// store.query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default())?.write(&mut results, QueryResultFormat::Json)?; /// assert_eq!(results, "{\"head\":{\"vars\":[\"s\"]},\"results\":{\"bindings\":[{\"s\":{\"type\":\"uri\",\"value\":\"http://example.com\"}}]}}".as_bytes()); /// # oxigraph::Result::Ok(()) /// ``` - pub fn write(self, writer: impl Write, syntax: QueryResultSyntax) -> Result<()> { - match syntax { - QueryResultSyntax::Xml => write_xml_results(self, writer), - QueryResultSyntax::Json => write_json_results(self, writer), + pub fn write(self, writer: impl Write, format: QueryResultFormat) -> Result<()> { + match format { + QueryResultFormat::Xml => write_xml_results(self, writer), + QueryResultFormat::Json => write_json_results(self, writer), } } @@ -61,7 +61,8 @@ impl QueryResult { /// This method fails if it is called on the `Solution` or `Boolean` results /// /// ``` - /// use oxigraph::{MemoryStore, GraphSyntax}; + /// use oxigraph::MemoryStore; + /// use oxigraph::io::GraphFormat; /// use oxigraph::sparql::QueryOptions; /// use oxigraph::model::*; /// use std::io::Cursor; @@ -69,16 +70,16 @@ impl QueryResult { /// let graph = " .\n".as_bytes(); /// /// let store = MemoryStore::new(); - /// store.load_graph(Cursor::new(graph), GraphSyntax::NTriples, &GraphName::DefaultGraph, None)?; + /// store.load_graph(Cursor::new(graph), GraphFormat::NTriples, &GraphName::DefaultGraph, None)?; /// /// let mut results = Vec::new(); - /// store.query("CONSTRUCT WHERE { ?s ?p ?o }", QueryOptions::default())?.write_graph(&mut results, GraphSyntax::NTriples)?; + /// store.query("CONSTRUCT WHERE { ?s ?p ?o }", QueryOptions::default())?.write_graph(&mut results, GraphFormat::NTriples)?; /// assert_eq!(results, graph); /// # oxigraph::Result::Ok(()) /// ``` - pub fn write_graph(self, write: impl Write, syntax: GraphSyntax) -> Result<()> { + pub fn write_graph(self, write: impl Write, format: GraphFormat) -> Result<()> { if let QueryResult::Graph(triples) = self { - let mut writer = GraphSerializer::from_syntax(syntax).triple_writer(write)?; + let mut writer = GraphSerializer::from_format(format).triple_writer(write)?; for triple in triples { writer.write(&triple?)?; } @@ -100,77 +101,77 @@ impl From for QueryResult { /// [SPARQL query](https://www.w3.org/TR/sparql11-query/) serialization formats /// -/// This enumeration is non exhaustive. New syntaxes like CSV will be added in the future. +/// This enumeration is non exhaustive. New formats like CSV will be added in the future. #[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)] #[non_exhaustive] -pub enum QueryResultSyntax { +pub enum QueryResultFormat { /// [SPARQL Query Results XML Format](http://www.w3.org/TR/rdf-sparql-XMLres/) Xml, /// [SPARQL Query Results JSON Format](https://www.w3.org/TR/sparql11-results-json/) Json, } -impl QueryResultSyntax { - /// The syntax canonical IRI according to the [Unique URIs for file formats registry](https://www.w3.org/ns/formats/). +impl QueryResultFormat { + /// The format canonical IRI according to the [Unique URIs for file formats registry](https://www.w3.org/ns/formats/). /// /// ``` - /// use oxigraph::sparql::QueryResultSyntax; + /// use oxigraph::sparql::QueryResultFormat; /// - /// assert_eq!(QueryResultSyntax::Json.iri(), "http://www.w3.org/ns/formats/SPARQL_Results_JSON") + /// assert_eq!(QueryResultFormat::Json.iri(), "http://www.w3.org/ns/formats/SPARQL_Results_JSON") /// ``` pub fn iri(self) -> &'static str { match self { - QueryResultSyntax::Xml => "http://www.w3.org/ns/formats/SPARQL_Results_XML", - QueryResultSyntax::Json => "http://www.w3.org/ns/formats/SPARQL_Results_JSON", + QueryResultFormat::Xml => "http://www.w3.org/ns/formats/SPARQL_Results_XML", + QueryResultFormat::Json => "http://www.w3.org/ns/formats/SPARQL_Results_JSON", } } - /// The syntax [IANA media type](https://tools.ietf.org/html/rfc2046). + /// The format [IANA media type](https://tools.ietf.org/html/rfc2046). /// /// ``` - /// use oxigraph::sparql::QueryResultSyntax; + /// use oxigraph::sparql::QueryResultFormat; /// - /// assert_eq!(QueryResultSyntax::Json.media_type(), "application/sparql-results+json") + /// assert_eq!(QueryResultFormat::Json.media_type(), "application/sparql-results+json") /// ``` pub fn media_type(self) -> &'static str { match self { - QueryResultSyntax::Xml => "application/sparql-results+xml", - QueryResultSyntax::Json => "application/sparql-results+json", + QueryResultFormat::Xml => "application/sparql-results+xml", + QueryResultFormat::Json => "application/sparql-results+json", } } - /// The syntax [IANA-registered](https://tools.ietf.org/html/rfc2046) file extension. + /// The format [IANA-registered](https://tools.ietf.org/html/rfc2046) file extension. /// /// ``` - /// use oxigraph::sparql::QueryResultSyntax; + /// use oxigraph::sparql::QueryResultFormat; /// - /// assert_eq!(QueryResultSyntax::Json.file_extension(), "srj") + /// assert_eq!(QueryResultFormat::Json.file_extension(), "srj") /// ``` pub fn file_extension(self) -> &'static str { match self { - QueryResultSyntax::Xml => "srx", - QueryResultSyntax::Json => "srj", + QueryResultFormat::Xml => "srx", + QueryResultFormat::Json => "srj", } } - /// Looks for a known syntax from a media type. + /// Looks for a known format from a media type. /// /// It supports some media type aliases. - /// For example "application/xml" is going to return `QueryResultSyntax::Xml` even if it is not its canonical media type. + /// For example "application/xml" is going to return `QueryResultFormat::Xml` even if it is not its canonical media type. /// /// Example: /// ``` - /// use oxigraph::sparql::QueryResultSyntax; + /// use oxigraph::sparql::QueryResultFormat; /// - /// assert_eq!(QueryResultSyntax::from_media_type("application/sparql-results+json; charset=utf-8"), Some(QueryResultSyntax::Json)) + /// assert_eq!(QueryResultFormat::from_media_type("application/sparql-results+json; charset=utf-8"), Some(QueryResultFormat::Json)) /// ``` pub fn from_media_type(media_type: &str) -> Option { if let Some(base_type) = media_type.split(';').next() { match base_type { "application/sparql-results+xml" | "application/xml" | "text/xml" => { - Some(QueryResultSyntax::Xml) + Some(QueryResultFormat::Xml) } "application/sparql-results+json" | "application/json" | "text/json" => { - Some(QueryResultSyntax::Json) + Some(QueryResultFormat::Json) } _ => None, } @@ -181,7 +182,7 @@ impl QueryResultSyntax { } #[allow(deprecated)] -impl FileSyntax for QueryResultSyntax { +impl FileSyntax for QueryResultFormat { fn iri(self) -> &'static str { self.iri() } diff --git a/lib/src/store/memory.rs b/lib/src/store/memory.rs index 18ab8e42..6e6deae6 100644 --- a/lib/src/store/memory.rs +++ b/lib/src/store/memory.rs @@ -1,13 +1,14 @@ //! In-memory store. use crate::error::{Infallible, UnwrapInfallible}; +use crate::io::{DatasetFormat, GraphFormat}; use crate::model::*; use crate::sparql::{Query, QueryOptions, QueryResult, SimplePreparedQuery}; use crate::store::numeric_encoder::*; use crate::store::{ dump_dataset, dump_graph, load_dataset, load_graph, ReadableEncodedStore, WritableEncodedStore, }; -use crate::{DatasetSyntax, Error, GraphSyntax}; +use crate::Error; use std::collections::hash_map::DefaultHasher; use std::collections::{HashMap, HashSet}; use std::convert::TryInto; @@ -251,14 +252,15 @@ impl MemoryStore { /// /// Usage example: /// ``` - /// use oxigraph::{MemoryStore, GraphSyntax}; + /// use oxigraph::MemoryStore; + /// use oxigraph::io::GraphFormat; /// use oxigraph::model::*; /// /// let store = MemoryStore::new(); /// /// // insertion /// let file = b" ."; - /// store.load_graph(file.as_ref(), GraphSyntax::NTriples, &GraphName::DefaultGraph, None)?; + /// store.load_graph(file.as_ref(), GraphFormat::NTriples, &GraphName::DefaultGraph, None)?; /// /// // quad filter /// let results: Vec = store.quads_for_pattern(None, None, None, None).collect(); @@ -272,26 +274,27 @@ impl MemoryStore { pub fn load_graph( &self, reader: impl BufRead, - syntax: GraphSyntax, + format: GraphFormat, to_graph_name: &GraphName, base_iri: Option<&str>, ) -> Result<(), io::Error> { let mut store = self; - load_graph(&mut store, reader, syntax, to_graph_name, base_iri) + load_graph(&mut store, reader, format, to_graph_name, base_iri) } /// Loads a dataset file (i.e. quads) into the store. /// /// Usage example: /// ``` - /// use oxigraph::{MemoryStore, DatasetSyntax}; + /// use oxigraph::MemoryStore; + /// use oxigraph::io::DatasetFormat; /// use oxigraph::model::*; /// /// let store = MemoryStore::new(); /// /// // insertion /// let file = b" ."; - /// store.load_dataset(file.as_ref(), DatasetSyntax::NQuads, None)?; + /// store.load_dataset(file.as_ref(), DatasetFormat::NQuads, None)?; /// /// // quad filter /// let results: Vec = store.quads_for_pattern(None, None, None, None).collect(); @@ -305,11 +308,11 @@ impl MemoryStore { pub fn load_dataset( &self, reader: impl BufRead, - syntax: DatasetSyntax, + format: DatasetFormat, base_iri: Option<&str>, ) -> Result<(), io::Error> { let mut store = self; - load_dataset(&mut store, reader, syntax, base_iri) + load_dataset(&mut store, reader, format, base_iri) } /// Adds a quad to this store. @@ -341,16 +344,17 @@ impl MemoryStore { /// /// Usage example: /// ``` - /// use oxigraph::{MemoryStore, GraphSyntax}; + /// use oxigraph::{MemoryStore}; + /// use oxigraph::io::GraphFormat; /// use oxigraph::model::GraphName; /// /// let file = " .\n".as_bytes(); /// /// let store = MemoryStore::new(); - /// store.load_graph(file, GraphSyntax::NTriples, &GraphName::DefaultGraph, None)?; + /// store.load_graph(file, GraphFormat::NTriples, &GraphName::DefaultGraph, None)?; /// /// let mut buffer = Vec::new(); - /// store.dump_graph(&mut buffer, GraphSyntax::NTriples, &GraphName::DefaultGraph)?; + /// store.dump_graph(&mut buffer, GraphFormat::NTriples, &GraphName::DefaultGraph)?; /// assert_eq!(file, buffer.as_slice()); /// # oxigraph::Result::Ok(()) /// ``` @@ -360,14 +364,14 @@ impl MemoryStore { pub fn dump_graph( &self, writer: impl Write, - syntax: GraphSyntax, + format: GraphFormat, from_graph_name: &GraphName, ) -> Result<(), io::Error> { dump_graph( self.quads_for_pattern(None, None, None, Some(from_graph_name)) .map(|q| Ok(q.into())), writer, - syntax, + format, ) } @@ -375,26 +379,27 @@ impl MemoryStore { /// /// Usage example: /// ``` - /// use oxigraph::{MemoryStore, DatasetSyntax}; + /// use oxigraph::MemoryStore; + /// use oxigraph::io::DatasetFormat; /// /// let file = " .\n".as_bytes(); /// /// let store = MemoryStore::new(); - /// store.load_dataset(file, DatasetSyntax::NQuads, None)?; + /// store.load_dataset(file, DatasetFormat::NQuads, None)?; /// /// let mut buffer = Vec::new(); - /// store.dump_dataset(&mut buffer, DatasetSyntax::NQuads)?; + /// store.dump_dataset(&mut buffer, DatasetFormat::NQuads)?; /// assert_eq!(file, buffer.as_slice()); /// # oxigraph::Result::Ok(()) /// ``` /// /// Errors related to parameter validation like the base IRI use the `INVALID_INPUT` error kind. /// Errors related to a bad syntax in the loaded file use the `INVALID_DATA` error kind. - pub fn dump_dataset(&self, writer: impl Write, syntax: DatasetSyntax) -> Result<(), io::Error> { + pub fn dump_dataset(&self, writer: impl Write, format: DatasetFormat) -> Result<(), io::Error> { dump_dataset( self.quads_for_pattern(None, None, None, None).map(Ok), writer, - syntax, + format, ) } @@ -971,7 +976,8 @@ impl<'a> MemoryTransaction<'a> { /// /// Usage example: /// ``` - /// use oxigraph::{MemoryStore, GraphSyntax}; + /// use oxigraph::MemoryStore; + /// use oxigraph::io::GraphFormat; /// use oxigraph::model::*; /// /// let store = MemoryStore::new(); @@ -979,7 +985,7 @@ impl<'a> MemoryTransaction<'a> { /// // insertion /// let file = b" ."; /// store.transaction(|transaction| { - /// transaction.load_graph(file.as_ref(), GraphSyntax::NTriples, &GraphName::DefaultGraph, None) + /// transaction.load_graph(file.as_ref(), GraphFormat::NTriples, &GraphName::DefaultGraph, None) /// })?; /// /// // quad filter @@ -991,25 +997,26 @@ impl<'a> MemoryTransaction<'a> { pub fn load_graph( &mut self, reader: impl BufRead, - syntax: GraphSyntax, + format: GraphFormat, to_graph_name: &GraphName, base_iri: Option<&str>, ) -> Result<(), io::Error> { - load_graph(self, reader, syntax, to_graph_name, base_iri) + load_graph(self, reader, format, to_graph_name, base_iri) } /// Loads a dataset file (i.e. quads) into the store during the transaction. /// /// Usage example: /// ``` - /// use oxigraph::{MemoryStore, DatasetSyntax}; + /// use oxigraph::MemoryStore; + /// use oxigraph::io::DatasetFormat; /// use oxigraph::model::*; /// /// let store = MemoryStore::new(); /// /// // insertion /// let file = b" ."; - /// store.load_dataset(file.as_ref(), DatasetSyntax::NQuads, None)?; + /// store.load_dataset(file.as_ref(), DatasetFormat::NQuads, None)?; /// /// // quad filter /// let results: Vec = store.quads_for_pattern(None, None, None, None).collect(); @@ -1020,10 +1027,10 @@ impl<'a> MemoryTransaction<'a> { pub fn load_dataset( &mut self, reader: impl BufRead, - syntax: DatasetSyntax, + format: DatasetFormat, base_iri: Option<&str>, ) -> Result<(), io::Error> { - load_dataset(self, reader, syntax, base_iri) + load_dataset(self, reader, format, base_iri) } /// Adds a quad to this store during the transaction. diff --git a/lib/src/store/mod.rs b/lib/src/store/mod.rs index 3663a25b..ba5377a2 100644 --- a/lib/src/store/mod.rs +++ b/lib/src/store/mod.rs @@ -16,9 +16,9 @@ pub use crate::store::rocksdb::RocksDbStore; #[cfg(feature = "sled")] pub use crate::store::sled::SledStore; +use crate::io::{DatasetFormat, GraphFormat}; use crate::model::*; use crate::store::numeric_encoder::*; -use crate::{DatasetSyntax, GraphSyntax}; use rio_api::formatter::{QuadsFormatter, TriplesFormatter}; use rio_api::parser::{QuadsParser, TriplesParser}; use rio_turtle::{ @@ -63,19 +63,19 @@ pub(crate) trait WritableEncodedStore: StrContainer { fn load_graph( store: &mut S, reader: impl BufRead, - syntax: GraphSyntax, + format: GraphFormat, to_graph_name: &GraphName, base_iri: Option<&str>, ) -> Result<(), io::Error> { let base_iri = base_iri.unwrap_or(""); - match syntax { - GraphSyntax::NTriples => { + match format { + GraphFormat::NTriples => { load_from_triple_parser(store, NTriplesParser::new(reader), to_graph_name) } - GraphSyntax::Turtle => { + GraphFormat::Turtle => { load_from_triple_parser(store, TurtleParser::new(reader, base_iri), to_graph_name) } - GraphSyntax::RdfXml => { + GraphFormat::RdfXml => { load_from_triple_parser(store, RdfXmlParser::new(reader, base_iri), to_graph_name) } } @@ -110,24 +110,24 @@ where fn dump_graph( triples: impl Iterator>, writer: impl Write, - syntax: GraphSyntax, + format: GraphFormat, ) -> Result<(), io::Error> { - match syntax { - GraphSyntax::NTriples => { + match format { + GraphFormat::NTriples => { let mut formatter = NTriplesFormatter::new(writer); for triple in triples { formatter.format(&(&triple?).into())?; } formatter.finish(); } - GraphSyntax::Turtle => { + GraphFormat::Turtle => { let mut formatter = TurtleFormatter::new(writer); for triple in triples { formatter.format(&(&triple?).into())?; } formatter.finish()?; } - GraphSyntax::RdfXml => { + GraphFormat::RdfXml => { let mut formatter = RdfXmlFormatter::new(writer).map_err(map_xml_err)?; for triple in triples { formatter.format(&(&triple?).into()).map_err(map_xml_err)?; @@ -145,13 +145,13 @@ fn map_xml_err(e: RdfXmlError) -> io::Error { fn load_dataset( store: &mut S, reader: impl BufRead, - syntax: DatasetSyntax, + format: DatasetFormat, base_iri: Option<&str>, ) -> Result<(), io::Error> { let base_iri = base_iri.unwrap_or(""); - match syntax { - DatasetSyntax::NQuads => load_from_quad_parser(store, NQuadsParser::new(reader)), - DatasetSyntax::TriG => load_from_quad_parser(store, TriGParser::new(reader, base_iri)), + match format { + DatasetFormat::NQuads => load_from_quad_parser(store, NQuadsParser::new(reader)), + DatasetFormat::TriG => load_from_quad_parser(store, TriGParser::new(reader, base_iri)), } } @@ -180,17 +180,17 @@ where fn dump_dataset( quads: impl Iterator>, writer: impl Write, - syntax: DatasetSyntax, + format: DatasetFormat, ) -> Result<(), io::Error> { - match syntax { - DatasetSyntax::NQuads => { + match format { + DatasetFormat::NQuads => { let mut formatter = NQuadsFormatter::new(writer); for quad in quads { formatter.format(&(&quad?).into())?; } formatter.finish(); } - DatasetSyntax::TriG => { + DatasetFormat::TriG => { let mut formatter = TriGFormatter::new(writer); for quad in quads { formatter.format(&(&quad?).into())?; diff --git a/lib/src/store/rocksdb.rs b/lib/src/store/rocksdb.rs index c4206ff7..bb86232c 100644 --- a/lib/src/store/rocksdb.rs +++ b/lib/src/store/rocksdb.rs @@ -1,13 +1,13 @@ //! Store based on the [RocksDB](https://rocksdb.org/) key-value database. use crate::error::{Infallible, UnwrapInfallible}; +use crate::io::{DatasetFormat, GraphFormat}; use crate::model::*; use crate::sparql::{Query, QueryOptions, QueryResult, SimplePreparedQuery}; use crate::store::numeric_encoder::*; use crate::store::{ dump_dataset, dump_graph, load_dataset, load_graph, ReadableEncodedStore, WritableEncodedStore, }; -use crate::{DatasetSyntax, GraphSyntax}; use rocksdb::*; use std::convert::TryInto; use std::io; @@ -191,12 +191,12 @@ impl RocksDbStore { pub fn load_graph( &self, reader: impl BufRead, - syntax: GraphSyntax, + format: GraphFormat, to_graph_name: &GraphName, base_iri: Option<&str>, ) -> Result<(), crate::Error> { let mut transaction = self.auto_batch_writer(); - load_graph(&mut transaction, reader, syntax, to_graph_name, base_iri)?; + load_graph(&mut transaction, reader, format, to_graph_name, base_iri)?; Ok(transaction.apply()?) } @@ -213,11 +213,11 @@ impl RocksDbStore { pub fn load_dataset( &self, reader: impl BufRead, - syntax: DatasetSyntax, + format: DatasetFormat, base_iri: Option<&str>, ) -> Result<(), crate::Error> { let mut transaction = self.auto_batch_writer(); - load_dataset(&mut transaction, reader, syntax, base_iri)?; + load_dataset(&mut transaction, reader, format, base_iri)?; Ok(transaction.apply()?) } @@ -243,21 +243,21 @@ impl RocksDbStore { pub fn dump_graph( &self, writer: impl Write, - syntax: GraphSyntax, + format: GraphFormat, from_graph_name: &GraphName, ) -> Result<(), io::Error> { dump_graph( self.quads_for_pattern(None, None, None, Some(from_graph_name)) .map(|q| Ok(q?.into())), writer, - syntax, + format, ) } /// Dumps the store dataset into a file. /// /// See `MemoryStore` for a usage example. - pub fn dump_dataset(&self, writer: impl Write, syntax: DatasetSyntax) -> Result<(), io::Error> { + pub fn dump_dataset(&self, writer: impl Write, syntax: DatasetFormat) -> Result<(), io::Error> { dump_dataset( self.quads_for_pattern(None, None, None, None), writer, @@ -577,7 +577,7 @@ impl RocksDbTransaction<'_> { pub fn load_graph( &mut self, reader: impl BufRead, - syntax: GraphSyntax, + syntax: GraphFormat, to_graph_name: &GraphName, base_iri: Option<&str>, ) -> Result<(), io::Error> { @@ -597,10 +597,10 @@ impl RocksDbTransaction<'_> { pub fn load_dataset( &mut self, reader: impl BufRead, - syntax: DatasetSyntax, + format: DatasetFormat, base_iri: Option<&str>, ) -> Result<(), io::Error> { - load_dataset(&mut self.inner, reader, syntax, base_iri) + load_dataset(&mut self.inner, reader, format, base_iri) } /// Adds a quad to this store during the transaction. diff --git a/lib/src/store/sled.rs b/lib/src/store/sled.rs index f560a489..43949cff 100644 --- a/lib/src/store/sled.rs +++ b/lib/src/store/sled.rs @@ -1,13 +1,13 @@ //! Store based on the [Sled](https://sled.rs/) key-value database. use crate::error::{Infallible, UnwrapInfallible}; +use crate::io::{DatasetFormat, GraphFormat}; use crate::model::*; use crate::sparql::{Query, QueryOptions, QueryResult, SimplePreparedQuery}; use crate::store::numeric_encoder::*; use crate::store::{ dump_dataset, dump_graph, load_dataset, load_graph, ReadableEncodedStore, WritableEncodedStore, }; -use crate::{DatasetSyntax, GraphSyntax}; use sled::{Batch, Config, Iter, Tree}; use std::convert::TryInto; use std::io::{BufRead, Cursor, Write}; @@ -176,14 +176,14 @@ impl SledStore { pub fn load_graph( &self, reader: impl BufRead, - syntax: GraphSyntax, + format: GraphFormat, to_graph_name: &GraphName, base_iri: Option<&str>, ) -> Result<(), io::Error> { load_graph( &mut DirectWriter::new(self), reader, - syntax, + format, to_graph_name, base_iri, ) @@ -202,10 +202,10 @@ impl SledStore { pub fn load_dataset( &self, reader: impl BufRead, - syntax: DatasetSyntax, + format: DatasetFormat, base_iri: Option<&str>, ) -> Result<(), io::Error> { - load_dataset(&mut DirectWriter::new(self), reader, syntax, base_iri) + load_dataset(&mut DirectWriter::new(self), reader, format, base_iri) } /// Adds a quad to this store. @@ -227,25 +227,25 @@ impl SledStore { pub fn dump_graph( &self, writer: impl Write, - syntax: GraphSyntax, + format: GraphFormat, from_graph_name: &GraphName, ) -> Result<(), io::Error> { dump_graph( self.quads_for_pattern(None, None, None, Some(from_graph_name)) .map(|q| Ok(q?.into())), writer, - syntax, + format, ) } /// Dumps the store dataset into a file. /// /// See `MemoryStore` for a usage example. - pub fn dump_dataset(&self, writer: impl Write, syntax: DatasetSyntax) -> Result<(), io::Error> { + pub fn dump_dataset(&self, writer: impl Write, format: DatasetFormat) -> Result<(), io::Error> { dump_dataset( self.quads_for_pattern(None, None, None, None), writer, - syntax, + format, ) } @@ -668,11 +668,11 @@ impl SledTransaction<'_> { pub fn load_graph( &mut self, reader: impl BufRead, - syntax: GraphSyntax, + format: GraphFormat, to_graph_name: &GraphName, base_iri: Option<&str>, ) -> Result<(), io::Error> { - load_graph(&mut self.inner, reader, syntax, to_graph_name, base_iri) + load_graph(&mut self.inner, reader, format, to_graph_name, base_iri) } /// Loads a dataset file (i.e. quads) into the store. into the store during the transaction. @@ -688,10 +688,10 @@ impl SledTransaction<'_> { pub fn load_dataset( &mut self, reader: impl BufRead, - syntax: DatasetSyntax, + format: DatasetFormat, base_iri: Option<&str>, ) -> Result<(), io::Error> { - load_dataset(&mut self.inner, reader, syntax, base_iri) + load_dataset(&mut self.inner, reader, format, base_iri) } /// Adds a quad to this store during the transaction. diff --git a/python/src/memory_store.rs b/python/src/memory_store.rs index cae216b1..5e9f88fa 100644 --- a/python/src/memory_store.rs +++ b/python/src/memory_store.rs @@ -1,8 +1,9 @@ use crate::model::*; use crate::store_utils::*; +use oxigraph::io::{DatasetFormat, GraphFormat}; use oxigraph::model::*; use oxigraph::sparql::QueryOptions; -use oxigraph::{DatasetSyntax, GraphSyntax, MemoryStore}; +use oxigraph::MemoryStore; use pyo3::basic::CompareOp; use pyo3::exceptions::{NotImplementedError, ValueError}; use pyo3::prelude::*; @@ -81,23 +82,23 @@ impl PyMemoryStore { Some(base_iri) }; - if let Some(graph_syntax) = GraphSyntax::from_media_type(mime_type) { + if let Some(graph_format) = GraphFormat::from_media_type(mime_type) { self.inner .load_graph( Cursor::new(data), - graph_syntax, + graph_format, &to_graph_name.unwrap_or(GraphName::DefaultGraph), base_iri, ) .map_err(map_io_err) - } else if let Some(dataset_syntax) = DatasetSyntax::from_media_type(mime_type) { + } else if let Some(dataset_format) = DatasetFormat::from_media_type(mime_type) { if to_graph_name.is_some() { return Err(ValueError::py_err( "The target graph name parameter is not available for dataset formats", )); } self.inner - .load_dataset(Cursor::new(data), dataset_syntax, base_iri) + .load_dataset(Cursor::new(data), dataset_format, base_iri) .map_err(map_io_err) } else { Err(ValueError::py_err(format!( diff --git a/python/src/sled_store.rs b/python/src/sled_store.rs index a4061b44..d3b3fc5d 100644 --- a/python/src/sled_store.rs +++ b/python/src/sled_store.rs @@ -1,8 +1,9 @@ use crate::model::*; use crate::store_utils::*; +use oxigraph::io::{DatasetFormat, GraphFormat}; use oxigraph::model::*; use oxigraph::sparql::QueryOptions; -use oxigraph::{DatasetSyntax, GraphSyntax, SledStore}; +use oxigraph::SledStore; use pyo3::exceptions::ValueError; use pyo3::prelude::*; use pyo3::types::PyTuple; @@ -83,23 +84,23 @@ impl PySledStore { Some(base_iri) }; - if let Some(graph_syntax) = GraphSyntax::from_media_type(mime_type) { + if let Some(graph_format) = GraphFormat::from_media_type(mime_type) { self.inner .load_graph( Cursor::new(data), - graph_syntax, + graph_format, &to_graph_name.unwrap_or(GraphName::DefaultGraph), base_iri, ) .map_err(map_io_err) - } else if let Some(dataset_syntax) = DatasetSyntax::from_media_type(mime_type) { + } else if let Some(dataset_format) = DatasetFormat::from_media_type(mime_type) { if to_graph_name.is_some() { return Err(ValueError::py_err( "The target graph name parameter is not available for dataset formats", )); } self.inner - .load_dataset(Cursor::new(data), dataset_syntax, base_iri) + .load_dataset(Cursor::new(data), dataset_format, base_iri) .map_err(map_io_err) } else { Err(ValueError::py_err(format!( diff --git a/server/src/main.rs b/server/src/main.rs index c1227989..8fca4ade 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -16,9 +16,10 @@ use async_std::net::{TcpListener, TcpStream}; use async_std::prelude::*; use async_std::task::{block_on, spawn, spawn_blocking}; use http_types::{headers, Body, Error, Method, Mime, Request, Response, Result, StatusCode}; +use oxigraph::io::{DatasetFormat, GraphFormat}; use oxigraph::model::GraphName; -use oxigraph::sparql::{Query, QueryOptions, QueryResult, QueryResultSyntax}; -use oxigraph::{DatasetSyntax, GraphSyntax, RocksDbStore}; +use oxigraph::sparql::{Query, QueryOptions, QueryResult, QueryResultFormat}; +use oxigraph::RocksDbStore; use std::str::FromStr; use url::form_urlencoded; @@ -60,7 +61,7 @@ async fn handle_request(request: Request, store: RocksDbStore) -> Result { if let Some(content_type) = request.content_type() { - match if let Some(format) = GraphSyntax::from_media_type(content_type.essence()) { + match if let Some(format) = GraphFormat::from_media_type(content_type.essence()) { spawn_blocking(move || { store.load_graph( SyncAsyncBufReader::from(request), @@ -69,7 +70,7 @@ async fn handle_request(request: Request, store: RocksDbStore) -> Result Result<()> { fn load_sparql_query_result(url: &str) -> Result { if url.ends_with(".srx") { StaticQueryResults::from_query_results( - QueryResult::read(read_file(url)?, QueryResultSyntax::Xml)?, + QueryResult::read(read_file(url)?, QueryResultFormat::Xml)?, false, ) } else if url.ends_with(".srj") { StaticQueryResults::from_query_results( - QueryResult::read(read_file(url)?, QueryResultSyntax::Json)?, + QueryResult::read(read_file(url)?, QueryResultFormat::Json)?, false, ) } else { diff --git a/wikibase/src/loader.rs b/wikibase/src/loader.rs index 1eef4c36..b6d3cd44 100644 --- a/wikibase/src/loader.rs +++ b/wikibase/src/loader.rs @@ -4,8 +4,9 @@ use async_std::prelude::*; use async_std::task::block_on; use chrono::{DateTime, Datelike, Utc}; use http_types::{headers, Error, Method, Request, Response, Result, StatusCode}; +use oxigraph::io::GraphFormat; use oxigraph::model::NamedNode; -use oxigraph::{GraphSyntax, RocksDbStore}; +use oxigraph::RocksDbStore; use serde_json::Value; use std::collections::{HashMap, HashSet}; use std::io::{BufReader, Cursor, Read}; @@ -271,7 +272,7 @@ impl WikibaseLoader { transaction.load_graph( BufReader::new(data), - GraphSyntax::NTriples, + GraphFormat::NTriples, &NamedNode::new(uri)?.into(), None, )?; diff --git a/wikibase/src/main.rs b/wikibase/src/main.rs index 0aa2ada8..19cfb217 100644 --- a/wikibase/src/main.rs +++ b/wikibase/src/main.rs @@ -16,8 +16,9 @@ use async_std::net::{TcpListener, TcpStream}; use async_std::prelude::*; use async_std::task::{spawn, spawn_blocking}; use http_types::{headers, Body, Error, Method, Mime, Request, Response, Result, StatusCode}; -use oxigraph::sparql::{Query, QueryOptions, QueryResult, QueryResultSyntax}; -use oxigraph::{GraphSyntax, RocksDbStore}; +use oxigraph::io::GraphFormat; +use oxigraph::sparql::{Query, QueryOptions, QueryResult, QueryResultFormat}; +use oxigraph::RocksDbStore; use std::str::FromStr; use std::time::Duration; use url::form_urlencoded; @@ -184,11 +185,11 @@ async fn evaluate_sparql_query( let format = content_negotiation( request, &[ - GraphSyntax::NTriples.media_type(), - GraphSyntax::Turtle.media_type(), - GraphSyntax::RdfXml.media_type(), + GraphFormat::NTriples.media_type(), + GraphFormat::Turtle.media_type(), + GraphFormat::RdfXml.media_type(), ], - GraphSyntax::from_media_type, + GraphFormat::from_media_type, )?; let mut body = Vec::default(); results.write_graph(&mut body, format)?; @@ -199,10 +200,10 @@ async fn evaluate_sparql_query( let format = content_negotiation( request, &[ - QueryResultSyntax::Xml.media_type(), - QueryResultSyntax::Json.media_type(), + QueryResultFormat::Xml.media_type(), + QueryResultFormat::Json.media_type(), ], - QueryResultSyntax::from_media_type, + QueryResultFormat::from_media_type, )?; let mut body = Vec::default(); results.write(&mut body, format)?;