-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathdatatype.rs
More file actions
79 lines (72 loc) · 2.71 KB
/
datatype.rs
File metadata and controls
79 lines (72 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// SPDX-FileCopyrightText: 2023 Joep Meindertsma <joep@ontola.io>
//
// SPDX-License-Identifier: MIT
//! [DataType]s constrain values of Atoms
use crate::urls;
use serde::{Deserialize, Serialize};
use std::{fmt, string::ParseError};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum DataType {
/// Either a full Resource, a link to a resource (subject) or a Nested Anonymous Resource
AtomicUrl,
Boolean,
Date,
Integer,
Float,
Markdown,
ResourceArray,
Slug,
String,
Timestamp,
Unsupported(String),
}
pub fn match_datatype(string: &str) -> DataType {
match string {
urls::ATOMIC_URL => DataType::AtomicUrl,
urls::BOOLEAN => DataType::Boolean,
urls::DATE => DataType::Date,
urls::INTEGER => DataType::Integer,
urls::FLOAT => DataType::Float,
urls::MARKDOWN => DataType::Markdown,
urls::RESOURCE_ARRAY => DataType::ResourceArray,
urls::SLUG => DataType::Slug,
urls::STRING => DataType::String,
urls::TIMESTAMP => DataType::Timestamp,
unsupported_datatype => DataType::Unsupported(unsupported_datatype.into()),
}
}
impl std::str::FromStr for DataType {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
urls::ATOMIC_URL => DataType::AtomicUrl,
urls::BOOLEAN => DataType::Boolean,
urls::DATE => DataType::Date,
urls::INTEGER => DataType::Integer,
urls::FLOAT => DataType::Float,
urls::MARKDOWN => DataType::Markdown,
urls::RESOURCE_ARRAY => DataType::ResourceArray,
urls::SLUG => DataType::Slug,
urls::STRING => DataType::String,
urls::TIMESTAMP => DataType::Timestamp,
unsupported_datatype => DataType::Unsupported(unsupported_datatype.into()),
})
}
}
impl fmt::Display for DataType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DataType::AtomicUrl => write!(f, "{}", urls::ATOMIC_URL),
DataType::Boolean => write!(f, "{}", urls::BOOLEAN),
DataType::Date => write!(f, "{}", urls::DATE),
DataType::Integer => write!(f, "{}", urls::INTEGER),
DataType::Float => write!(f, "{}", urls::FLOAT),
DataType::Markdown => write!(f, "{}", urls::MARKDOWN),
DataType::ResourceArray => write!(f, "{}", urls::RESOURCE_ARRAY),
DataType::Slug => write!(f, "{}", urls::SLUG),
DataType::String => write!(f, "{}", urls::STRING),
DataType::Timestamp => write!(f, "{}", urls::TIMESTAMP),
DataType::Unsupported(url) => write!(f, "{}", url),
}
}
}