1use alloc::{borrow::Cow, vec, vec::Vec};
4use dogma::traits::{Labeled, Named};
5
6pub const FORMATS: [(&'static str, Format); 14] = [
7 ("cottas", Format::Cottas),
8 ("csvw", Format::Csvw),
9 ("hdt", Format::Hdt),
10 ("jelly", Format::Jelly),
11 ("jsonld", Format::JsonLd),
12 ("n3", Format::Notation3),
13 ("nq", Format::NQuads),
14 ("nt", Format::NTriples),
15 ("rj", Format::RdfJson),
16 ("rdf", Format::RdfXml),
17 ("trig", Format::TriG),
18 ("trix", Format::TriX),
19 ("ttl", Format::Turtle),
20 ("yamlld", Format::YamlLd),
21];
22
23#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25#[non_exhaustive]
26pub enum Format {
27 Cottas,
28 Csvw,
29 Hdt,
30 Jelly,
31 JsonLd,
32 Notation3,
33 NQuads,
34 NTriples,
35 RdfJson,
36 RdfXml,
37 TriG,
38 TriX,
39 Turtle,
40 YamlLd,
41}
42
43impl Named for Format {
44 fn name(&self) -> Cow<'_, str> {
45 use Format::*;
46 Cow::Borrowed(match self {
47 Cottas => "cottas",
48 Csvw => "csvw",
49 Hdt => "hdt",
50 Jelly => "jelly",
51 JsonLd => "json-ld",
52 Notation3 => "n3",
53 NQuads => "n-quads",
54 NTriples => "n-triples",
55 RdfJson => "rdfjson",
56 RdfXml => "rdfxml",
57 TriG => "trig",
58 TriX => "trix",
59 Turtle => "turtle",
60 YamlLd => "yaml-ld",
61 })
62 }
63}
64
65impl Labeled for Format {
66 fn label(&self) -> Cow<'_, str> {
67 use Format::*;
68 Cow::Borrowed(match self {
69 Cottas => "Cottas",
70 Csvw => "CSVW",
71 Hdt => "HDT",
72 Jelly => "Jelly",
73 JsonLd => "JSON-LD",
74 Notation3 => "Notation3",
75 NQuads => "N-Quads",
76 NTriples => "N-Triples",
77 RdfJson => "RDF/JSON",
78 RdfXml => "RDF/XML",
79 TriG => "TriG",
80 TriX => "TriX",
81 Turtle => "Turtle",
82 YamlLd => "YAML-LD",
83 })
84 }
85}
86
87impl Format {
88 pub fn from_extension(extension: &str) -> Option<Self> {
89 for (format_extension, format) in FORMATS {
90 if extension.eq_ignore_ascii_case(format_extension) {
91 return Some(format);
92 }
93 }
94 None
95 }
96
97 pub fn extension(&self) -> &str {
98 use Format::*;
99 match self {
100 Cottas => "cottas",
101 Csvw => "csv",
102 Hdt => "hdt",
103 Jelly => "jelly",
104 JsonLd => "jsonld",
105 Notation3 => "n3",
106 NQuads => "nq",
107 NTriples => "nt",
108 RdfJson => "rj",
109 RdfXml => "rdf",
110 TriG => "trig",
111 TriX => "trix",
112 Turtle => "ttl",
113 YamlLd => "yamlld",
114 }
115 }
116
117 pub fn extensions(&self) -> Vec<&str> {
118 use Format::*;
119 match self {
120 YamlLd => vec!["yamlld", "yaml", "yml"],
121 _ => vec![self.extension()],
122 }
123 }
124
125 pub fn is_binary(&self) -> bool {
126 use Format::*;
127 matches!(self, Cottas | Hdt | Jelly)
128 }
129
130 pub fn is_text(&self) -> bool {
131 !self.is_binary()
132 }
133}
134
135impl core::fmt::Display for Format {
136 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
137 f.write_str(&self.label())
138 }
139}
140
141#[cfg(feature = "oxrdf")]
142impl TryFrom<&oxrdfio::RdfFormat> for Format {
143 type Error = ();
144
145 fn try_from(format: &oxrdfio::RdfFormat) -> Result<Self, Self::Error> {
146 use oxrdfio::RdfFormat;
147 Ok(match format {
148 RdfFormat::N3 => Format::Notation3,
149 RdfFormat::NQuads => Format::NQuads,
150 RdfFormat::NTriples => Format::NTriples,
151 RdfFormat::RdfXml => Format::RdfXml,
152 RdfFormat::TriG => Format::TriG,
153 RdfFormat::Turtle => Format::Turtle,
154 RdfFormat::JsonLd { .. } => Format::JsonLd,
155 _ => return Err(()),
156 })
157 }
158}
159
160#[cfg(feature = "oxrdf")]
161impl TryInto<oxrdfio::RdfFormat> for Format {
162 type Error = ();
163
164 fn try_into(self) -> Result<oxrdfio::RdfFormat, Self::Error> {
165 TryInto::<oxrdfio::RdfFormat>::try_into(&self)
166 }
167}
168
169#[cfg(feature = "oxrdf")]
170impl TryInto<oxrdfio::RdfFormat> for &Format {
171 type Error = ();
172
173 fn try_into(self) -> Result<oxrdfio::RdfFormat, Self::Error> {
174 use oxrdfio::RdfFormat;
175 Ok(match self {
176 Format::Notation3 => RdfFormat::N3,
177 Format::NQuads => RdfFormat::NQuads,
178 Format::NTriples => RdfFormat::NTriples,
179 Format::RdfXml => RdfFormat::RdfXml,
180 Format::TriG => RdfFormat::TriG,
181 Format::Turtle => RdfFormat::Turtle,
182 _ => return Err(()),
183 })
184 }
185}