1#[cfg(feature = "serde")]
2use serde::Serialize;
3use {num_enum::TryFromPrimitive, phf::phf_map};
4
5static DATA_TYPE_MAP: phf::Map<&'static str, DataType> = phf_map! {
6 "Unbound Value" => DataType::UnboundValue,
7 "Blank Node" => DataType::BlankNode,
8 "IRI Reference" => DataType::IriReference,
9 "http://www.w3.org/2000/01/rdf-schema#Literal" => DataType::Literal,
10 "http://www.w3.org/2001/XMLSchema#anyURI" => DataType::AnyUri,
11 "http://www.w3.org/2001/XMLSchema#boolean" => DataType::Boolean,
12 "http://www.w3.org/2001/XMLSchema#byte" => DataType::Byte,
13 "http://www.w3.org/2001/XMLSchema#date" => DataType::Date,
14 "http://www.w3.org/2001/XMLSchema#dateTime" => DataType::DateTime,
15 "http://www.w3.org/2001/XMLSchema#dateTimeStamp" => DataType::DateTimeStamp,
16 "http://www.w3.org/2001/XMLSchema#gDay" => DataType::Day,
17 "http://www.w3.org/2001/XMLSchema#dayTimeDuration" => DataType::DayTimeDuration,
18 "http://www.w3.org/2001/XMLSchema#decimal" => DataType::Decimal,
19 "http://www.w3.org/2001/XMLSchema#double" => DataType::Double,
20 "http://www.w3.org/2001/XMLSchema#duration" => DataType::Duration,
21 "http://www.w3.org/2001/XMLSchema#float" => DataType::Float,
22 "http://www.w3.org/2001/XMLSchema#int" => DataType::Int,
23 "http://www.w3.org/2001/XMLSchema#integer" => DataType::Integer,
24 "http://www.w3.org/2001/XMLSchema#long" => DataType::Long,
25 "http://www.w3.org/2001/XMLSchema#gMonth" => DataType::Month,
26 "http://www.w3.org/2001/XMLSchema#gMonthDay" => DataType::MonthDay,
27 "http://www.w3.org/2001/XMLSchema#negativeInteger" => DataType::NegativeInteger,
28 "http://www.w3.org/2001/XMLSchema#nonNegativeInteger" => DataType::NonNegativeInteger,
29 "http://www.w3.org/2001/XMLSchema#nonPositiveInteger" => DataType::NonPositiveInteger,
30 "http://www.w3.org/2001/XMLSchema#short" => DataType::Short,
31 "http://www.w3.org/2001/XMLSchema#string" => DataType::String,
32 "http://www.w3.org/2001/XMLSchema#time" => DataType::Time,
33 "http://www.w3.org/2001/XMLSchema#unsignedByte" => DataType::UnsignedByte,
34 "http://www.w3.org/2001/XMLSchema#unsignedInt" => DataType::UnsignedInt,
35 "http://www.w3.org/2001/XMLSchema#unsignedLong" => DataType::UnsignedLong,
36 "http://www.w3.org/2001/XMLSchema#unsignedShort" => DataType::UnsignedShort,
37 "http://www.w3.org/2001/XMLSchema#gYear" => DataType::Year,
38 "http://www.w3.org/2001/XMLSchema#gYearMonth" => DataType::YearMonth,
39 "http://www.w3.org/2001/XMLSchema#yearMonthDuration" => DataType::YearMonthDuration,
40};
41
42#[derive(Debug, Eq, PartialEq, Hash, Copy, Clone, TryFromPrimitive)]
46#[cfg_attr(feature = "serde", derive(Serialize))]
47#[repr(u8)]
48pub enum DataType {
49 UnboundValue = 0,
51 BlankNode = 1,
53 IriReference = 2,
55 Literal = 3,
57 AnyUri = 4,
59 String = 5,
61 PlainLiteral = 6,
63 Boolean = 7,
65 DateTime = 8,
67 DateTimeStamp = 9,
69 Time = 10,
71 Date = 11,
73 YearMonth = 12,
75 Year = 13,
77 MonthDay = 14,
79 Day = 15,
81 Month = 16,
83 Duration = 17,
85 YearMonthDuration = 18,
87 DayTimeDuration = 19,
89 Double = 20,
91 Float = 21,
93 Decimal = 22,
95 Integer = 23,
97 NonNegativeInteger = 24,
99 NonPositiveInteger = 25,
101 NegativeInteger = 26,
103 PositiveInteger = 27,
105 Long = 28,
107 Int = 29,
109 Short = 30,
111 Byte = 31,
113 UnsignedLong = 32,
115 UnsignedInt = 33,
117 UnsignedShort = 34,
119 UnsignedByte = 35,
121}
122
123impl Default for DataType {
124 fn default() -> Self { DataType::Boolean }
127}
128
129impl DataType {
130 pub fn from_datatype_id(data_type_id: u8) -> Result<DataType, ekg_error::Error> {
131 DataType::try_from(data_type_id)
132 .map_err(|_err| ekg_error::Error::UnknownDataType { data_type_id })
133 }
134
135 pub fn from_xsd_iri(iri: &str) -> Result<Self, ekg_error::Error> {
136 if let Some(data_type) = DATA_TYPE_MAP.get(iri) {
137 Ok(*data_type)
138 } else {
139 Err(ekg_error::Error::UnknownXsdDataType { data_type_iri: iri.to_string() })
140 }
141 }
142
143 pub fn as_xsd_iri_str(&self) -> &'static str {
144 DATA_TYPE_MAP
145 .entries()
146 .find_map(
147 |(key, val)| {
148 if val == self { Some(key) } else { None }
149 },
150 )
151 .unwrap_or_else(|| panic!("You've managed to create an unknown DataType instance"))
152 }
153
154 #[inline]
155 pub fn is_string(&self) -> bool { matches!(self, DataType::String | DataType::PlainLiteral) }
156
157 #[inline]
158 pub fn is_iri(&self) -> bool { matches!(self, DataType::AnyUri | DataType::IriReference) }
159
160 #[inline]
161 pub fn is_boolean(&self) -> bool { matches!(self, DataType::Boolean) }
162
163 #[inline]
164 pub fn is_date(&self) -> bool { matches!(self, DataType::Date) }
165
166 #[inline]
167 pub fn is_date_time(&self) -> bool { matches!(self, DataType::DateTime) }
168
169 #[inline]
170 pub fn is_decimal(&self) -> bool { matches!(self, DataType::Decimal) }
171
172 #[inline]
173 pub fn is_date_time_stamp(&self) -> bool { matches!(self, DataType::DateTimeStamp) }
174
175 #[inline]
176 pub fn is_duration(&self) -> bool { matches!(self, DataType::Duration) }
177
178 #[inline]
179 pub fn is_signed_integer(&self) -> bool {
180 matches!(
182 self,
183 DataType::Int |
184 DataType::Integer |
185 DataType::NegativeInteger |
186 DataType::NonPositiveInteger |
187 DataType::Long |
188 DataType::Short
189 )
190 }
191
192 #[inline]
193 pub fn is_unsigned_integer(&self) -> bool {
194 matches!(
196 self,
197 DataType::PositiveInteger |
198 DataType::NonNegativeInteger |
199 DataType::UnsignedByte |
200 DataType::UnsignedInt |
201 DataType::UnsignedShort |
202 DataType::UnsignedLong
203 )
204 }
205
206 #[inline]
207 pub fn is_integer(&self) -> bool { self.is_unsigned_integer() || self.is_signed_integer() }
208
209 #[inline]
210 pub fn is_blank_node(&self) -> bool {
211 matches!(self, DataType::BlankNode)
213 }
214}