oxigraph/io/
format.rs

1#![allow(deprecated)]
2
3use oxrdfio::{RdfFormat, RdfParser, RdfSerializer};
4
5/// [RDF graph](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-graph) serialization formats.
6///
7/// This enumeration is non exhaustive. New formats like JSON-LD will be added in the future.
8#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
9#[non_exhaustive]
10#[deprecated(note = "use RdfFormat instead", since = "0.4.0")]
11pub enum GraphFormat {
12    /// [N-Triples](https://www.w3.org/TR/n-triples/)
13    NTriples,
14    /// [Turtle](https://www.w3.org/TR/turtle/)
15    Turtle,
16    /// [RDF/XML](https://www.w3.org/TR/rdf-syntax-grammar/)
17    RdfXml,
18}
19
20impl GraphFormat {
21    /// The format canonical IRI according to the [Unique URIs for file formats registry](https://www.w3.org/ns/formats/).
22    ///
23    /// ```
24    /// use oxigraph::io::GraphFormat;
25    ///
26    /// assert_eq!(
27    ///     GraphFormat::NTriples.iri(),
28    ///     "http://www.w3.org/ns/formats/N-Triples"
29    /// )
30    /// ```
31    #[inline]
32    pub fn iri(self) -> &'static str {
33        match self {
34            Self::NTriples => "http://www.w3.org/ns/formats/N-Triples",
35            Self::Turtle => "http://www.w3.org/ns/formats/Turtle",
36            Self::RdfXml => "http://www.w3.org/ns/formats/RDF_XML",
37        }
38    }
39
40    /// The format [IANA media type](https://tools.ietf.org/html/rfc2046).
41    ///
42    /// ```
43    /// use oxigraph::io::GraphFormat;
44    ///
45    /// assert_eq!(GraphFormat::NTriples.media_type(), "application/n-triples")
46    /// ```
47    #[inline]
48    pub fn media_type(self) -> &'static str {
49        match self {
50            Self::NTriples => "application/n-triples",
51            Self::Turtle => "text/turtle",
52            Self::RdfXml => "application/rdf+xml",
53        }
54    }
55
56    /// The format [IANA-registered](https://tools.ietf.org/html/rfc2046) file extension.
57    ///
58    /// ```
59    /// use oxigraph::io::GraphFormat;
60    ///
61    /// assert_eq!(GraphFormat::NTriples.file_extension(), "nt")
62    /// ```
63    #[inline]
64    pub fn file_extension(self) -> &'static str {
65        match self {
66            Self::NTriples => "nt",
67            Self::Turtle => "ttl",
68            Self::RdfXml => "rdf",
69        }
70    }
71
72    /// Looks for a known format from a media type.
73    ///
74    /// It supports some media type aliases.
75    /// For example, "application/xml" is going to return `GraphFormat::RdfXml` even if it is not its canonical media type.
76    ///
77    /// Example:
78    /// ```
79    /// use oxigraph::io::GraphFormat;
80    ///
81    /// assert_eq!(
82    ///     GraphFormat::from_media_type("text/turtle; charset=utf-8"),
83    ///     Some(GraphFormat::Turtle)
84    /// )
85    /// ```
86    #[inline]
87    pub fn from_media_type(media_type: &str) -> Option<Self> {
88        match media_type.split(';').next()?.trim() {
89            "application/n-triples" | "text/plain" => Some(Self::NTriples),
90            "text/turtle" | "application/turtle" | "application/x-turtle" => Some(Self::Turtle),
91            "application/rdf+xml" | "application/xml" | "text/xml" => Some(Self::RdfXml),
92            _ => None,
93        }
94    }
95
96    /// Looks for a known format from an extension.
97    ///
98    /// It supports some aliases.
99    ///
100    /// Example:
101    /// ```
102    /// use oxigraph::io::GraphFormat;
103    ///
104    /// assert_eq!(
105    ///     GraphFormat::from_extension("nt"),
106    ///     Some(GraphFormat::NTriples)
107    /// )
108    /// ```
109    #[inline]
110    pub fn from_extension(extension: &str) -> Option<Self> {
111        match extension {
112            "nt" | "txt" => Some(Self::NTriples),
113            "ttl" => Some(Self::Turtle),
114            "rdf" | "xml" => Some(Self::RdfXml),
115            _ => None,
116        }
117    }
118}
119
120impl From<GraphFormat> for RdfFormat {
121    #[inline]
122    fn from(format: GraphFormat) -> Self {
123        match format {
124            GraphFormat::NTriples => Self::NTriples,
125            GraphFormat::Turtle => Self::Turtle,
126            GraphFormat::RdfXml => Self::RdfXml,
127        }
128    }
129}
130
131impl From<GraphFormat> for RdfParser {
132    #[inline]
133    fn from(format: GraphFormat) -> Self {
134        RdfFormat::from(format).into()
135    }
136}
137
138impl From<GraphFormat> for RdfSerializer {
139    #[inline]
140    fn from(format: GraphFormat) -> Self {
141        RdfFormat::from(format).into()
142    }
143}
144
145/// [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) serialization formats.
146///
147/// This enumeration is non exhaustive. New formats like JSON-LD will be added in the future.
148#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
149#[non_exhaustive]
150#[deprecated(note = "use RdfFormat instead", since = "0.4.0")]
151pub enum DatasetFormat {
152    /// [N-Quads](https://www.w3.org/TR/n-quads/)
153    NQuads,
154    /// [TriG](https://www.w3.org/TR/trig/)
155    TriG,
156}
157
158impl DatasetFormat {
159    /// The format canonical IRI according to the [Unique URIs for file formats registry](https://www.w3.org/ns/formats/).
160    ///
161    /// ```
162    /// use oxigraph::io::DatasetFormat;
163    ///
164    /// assert_eq!(
165    ///     DatasetFormat::NQuads.iri(),
166    ///     "http://www.w3.org/ns/formats/N-Quads"
167    /// )
168    /// ```
169    #[inline]
170    pub fn iri(self) -> &'static str {
171        match self {
172            Self::NQuads => "http://www.w3.org/ns/formats/N-Quads",
173            Self::TriG => "http://www.w3.org/ns/formats/TriG",
174        }
175    }
176
177    /// The format [IANA media type](https://tools.ietf.org/html/rfc2046).
178    ///
179    /// ```
180    /// use oxigraph::io::DatasetFormat;
181    ///
182    /// assert_eq!(DatasetFormat::NQuads.media_type(), "application/n-quads")
183    /// ```
184    #[inline]
185    pub fn media_type(self) -> &'static str {
186        match self {
187            Self::NQuads => "application/n-quads",
188            Self::TriG => "application/trig",
189        }
190    }
191
192    /// The format [IANA-registered](https://tools.ietf.org/html/rfc2046) file extension.
193    ///
194    /// ```
195    /// use oxigraph::io::DatasetFormat;
196    ///
197    /// assert_eq!(DatasetFormat::NQuads.file_extension(), "nq")
198    /// ```
199    #[inline]
200    pub fn file_extension(self) -> &'static str {
201        match self {
202            Self::NQuads => "nq",
203            Self::TriG => "trig",
204        }
205    }
206
207    /// Looks for a known format from a media type.
208    ///
209    /// It supports some media type aliases.
210    ///
211    /// Example:
212    /// ```
213    /// use oxigraph::io::DatasetFormat;
214    ///
215    /// assert_eq!(
216    ///     DatasetFormat::from_media_type("application/n-quads; charset=utf-8"),
217    ///     Some(DatasetFormat::NQuads)
218    /// )
219    /// ```
220    #[inline]
221    pub fn from_media_type(media_type: &str) -> Option<Self> {
222        match media_type.split(';').next()?.trim() {
223            "application/n-quads" | "text/x-nquads" | "text/nquads" => Some(Self::NQuads),
224            "application/trig" | "application/x-trig" => Some(Self::TriG),
225            _ => None,
226        }
227    }
228
229    /// Looks for a known format from an extension.
230    ///
231    /// It supports some aliases.
232    ///
233    /// Example:
234    /// ```
235    /// use oxigraph::io::DatasetFormat;
236    ///
237    /// assert_eq!(
238    ///     DatasetFormat::from_extension("nq"),
239    ///     Some(DatasetFormat::NQuads)
240    /// )
241    /// ```
242    #[inline]
243    pub fn from_extension(extension: &str) -> Option<Self> {
244        match extension {
245            "nq" | "txt" => Some(Self::NQuads),
246            "trig" => Some(Self::TriG),
247            _ => None,
248        }
249    }
250}
251
252impl From<DatasetFormat> for RdfFormat {
253    #[inline]
254    fn from(format: DatasetFormat) -> Self {
255        match format {
256            DatasetFormat::NQuads => Self::NQuads,
257            DatasetFormat::TriG => Self::TriG,
258        }
259    }
260}
261
262impl From<DatasetFormat> for RdfParser {
263    #[inline]
264    fn from(format: DatasetFormat) -> Self {
265        RdfFormat::from(format).into()
266    }
267}
268
269impl From<DatasetFormat> for RdfSerializer {
270    #[inline]
271    fn from(format: DatasetFormat) -> Self {
272        RdfFormat::from(format).into()
273    }
274}
275
276impl TryFrom<DatasetFormat> for GraphFormat {
277    type Error = ();
278
279    /// Attempts to find a graph format that is a subset of this [`DatasetFormat`].
280    #[inline]
281    fn try_from(value: DatasetFormat) -> Result<Self, Self::Error> {
282        match value {
283            DatasetFormat::NQuads => Ok(Self::NTriples),
284            DatasetFormat::TriG => Ok(Self::Turtle),
285        }
286    }
287}
288
289impl TryFrom<GraphFormat> for DatasetFormat {
290    type Error = ();
291
292    /// Attempts to find a dataset format that is a superset of this [`GraphFormat`].
293    #[inline]
294    fn try_from(value: GraphFormat) -> Result<Self, Self::Error> {
295        match value {
296            GraphFormat::NTriples => Ok(Self::NQuads),
297            GraphFormat::Turtle => Ok(Self::TriG),
298            GraphFormat::RdfXml => Err(()),
299        }
300    }
301}