Skip to main content

ng_oxigraph/oxigraph/io/
write.rs

1#![allow(deprecated)]
2
3//! Utilities to write RDF graphs and datasets.
4
5use crate::oxigraph::io::{DatasetFormat, GraphFormat};
6use crate::oxigraph::model::*;
7use crate::oxrdfio::{RdfSerializer, ToWriteQuadWriter};
8use std::io::{self, Write};
9
10/// A serializer for RDF graph serialization formats.
11///
12/// It currently supports the following formats:
13/// * [N-Triples](https://www.w3.org/TR/n-triples/) ([`GraphFormat::NTriples`])
14/// * [Turtle](https://www.w3.org/TR/turtle/) ([`GraphFormat::Turtle`])
15/// * [RDF/XML](https://www.w3.org/TR/rdf-syntax-grammar/) ([`GraphFormat::RdfXml`])
16///
17/// ```
18/// use oxigraph::io::{GraphFormat, GraphSerializer};
19/// use oxigraph::model::*;
20///
21/// let mut buffer = Vec::new();
22/// let mut writer = GraphSerializer::from_format(GraphFormat::NTriples).triple_writer(&mut buffer);
23/// writer.write(&Triple {
24///     subject: NamedNode::new("http://example.com/s")?.into(),
25///     predicate: NamedNode::new("http://example.com/p")?,
26///     object: NamedNode::new("http://example.com/o")?.into(),
27/// })?;
28/// writer.finish()?;
29///
30/// assert_eq!(
31///     buffer.as_slice(),
32///     "<http://example.com/s> <http://example.com/p> <http://example.com/o> .\n".as_bytes()
33/// );
34/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
35/// ```
36#[deprecated(note = "use RdfSerializer instead", since = "0.4.0")]
37pub struct GraphSerializer {
38    inner: RdfSerializer,
39}
40
41impl GraphSerializer {
42    /// Builds a serializer for the given format
43    #[inline]
44    pub fn from_format(format: GraphFormat) -> Self {
45        Self {
46            inner: RdfSerializer::from_format(format.into()),
47        }
48    }
49
50    /// Returns a [`TripleWriter`] allowing writing triples into the given [`Write`] implementation
51    pub fn triple_writer<W: Write>(self, write: W) -> TripleWriter<W> {
52        TripleWriter {
53            writer: self.inner.serialize_to_write(write),
54        }
55    }
56}
57
58/// Allows writing triples.
59/// Could be built using a [`GraphSerializer`].
60///
61/// <div class="warning">
62///
63/// Do not forget to run the [`finish`](TripleWriter::finish()) method to properly write the last bytes of the file.</div>
64///
65/// ```
66/// use oxigraph::io::{GraphFormat, GraphSerializer};
67/// use oxigraph::model::*;
68///
69/// let mut buffer = Vec::new();
70/// let mut writer = GraphSerializer::from_format(GraphFormat::NTriples).triple_writer(&mut buffer);
71/// writer.write(&Triple {
72///     subject: NamedNode::new("http://example.com/s")?.into(),
73///     predicate: NamedNode::new("http://example.com/p")?,
74///     object: NamedNode::new("http://example.com/o")?.into(),
75/// })?;
76/// writer.finish()?;
77///
78/// assert_eq!(
79///     buffer.as_slice(),
80///     "<http://example.com/s> <http://example.com/p> <http://example.com/o> .\n".as_bytes()
81/// );
82/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
83/// ```
84#[must_use]
85pub struct TripleWriter<W: Write> {
86    writer: ToWriteQuadWriter<W>,
87}
88
89impl<W: Write> TripleWriter<W> {
90    /// Writes a triple
91    pub fn write<'a>(&mut self, triple: impl Into<TripleRef<'a>>) -> io::Result<()> {
92        self.writer.write_triple(triple)
93    }
94
95    /// Writes the last bytes of the file
96    pub fn finish(self) -> io::Result<()> {
97        self.writer.finish()?.flush()
98    }
99}
100
101/// A serializer for RDF graph serialization formats.
102///
103/// It currently supports the following formats:
104/// * [N-Quads](https://www.w3.org/TR/n-quads/) ([`DatasetFormat::NQuads`])
105/// * [TriG](https://www.w3.org/TR/trig/) ([`DatasetFormat::TriG`])
106///
107/// ```
108/// use oxigraph::io::{DatasetFormat, DatasetSerializer};
109/// use oxigraph::model::*;
110///
111/// let mut buffer = Vec::new();
112/// let mut writer = DatasetSerializer::from_format(DatasetFormat::NQuads).quad_writer(&mut buffer);
113/// writer.write(&Quad {
114///    subject: NamedNode::new("http://example.com/s")?.into(),
115///    predicate: NamedNode::new("http://example.com/p")?,
116///    object: NamedNode::new("http://example.com/o")?.into(),
117///    graph_name: NamedNode::new("http://example.com/g")?.into(),
118/// })?;
119/// writer.finish()?;
120///
121/// assert_eq!(buffer.as_slice(), "<http://example.com/s> <http://example.com/p> <http://example.com/o> <http://example.com/g> .\n".as_bytes());
122/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
123/// ```
124#[deprecated(note = "use RdfSerializer instead", since = "0.4.0")]
125pub struct DatasetSerializer {
126    inner: RdfSerializer,
127}
128
129impl DatasetSerializer {
130    /// Builds a serializer for the given format
131    #[inline]
132    pub fn from_format(format: DatasetFormat) -> Self {
133        Self {
134            inner: RdfSerializer::from_format(format.into()),
135        }
136    }
137
138    /// Returns a [`QuadWriter`] allowing writing triples into the given [`Write`] implementation
139    pub fn quad_writer<W: Write>(self, write: W) -> QuadWriter<W> {
140        QuadWriter {
141            writer: self.inner.serialize_to_write(write),
142        }
143    }
144}
145
146/// Allows writing triples.
147/// Could be built using a [`DatasetSerializer`].
148///
149/// <div class="warning">
150///
151/// Do not forget to run the [`finish`](QuadWriter::finish()) method to properly write the last bytes of the file.</div>
152///
153/// ```
154/// use oxigraph::io::{DatasetFormat, DatasetSerializer};
155/// use oxigraph::model::*;
156///
157/// let mut buffer = Vec::new();
158/// let mut writer = DatasetSerializer::from_format(DatasetFormat::NQuads).quad_writer(&mut buffer);
159/// writer.write(&Quad {
160///    subject: NamedNode::new("http://example.com/s")?.into(),
161///    predicate: NamedNode::new("http://example.com/p")?,
162///    object: NamedNode::new("http://example.com/o")?.into(),
163///    graph_name: NamedNode::new("http://example.com/g")?.into(),
164/// })?;
165/// writer.finish()?;
166///
167/// assert_eq!(buffer.as_slice(), "<http://example.com/s> <http://example.com/p> <http://example.com/o> <http://example.com/g> .\n".as_bytes());
168/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
169/// ```
170#[must_use]
171pub struct QuadWriter<W: Write> {
172    writer: ToWriteQuadWriter<W>,
173}
174
175impl<W: Write> QuadWriter<W> {
176    /// Writes a quad
177    pub fn write<'a>(&mut self, quad: impl Into<QuadRef<'a>>) -> io::Result<()> {
178        self.writer.write_quad(quad)
179    }
180
181    /// Writes the last bytes of the file
182    pub fn finish(self) -> io::Result<()> {
183        self.writer.finish()?.flush()
184    }
185}