json_ld_syntax/context/definition/
version.rs

1use std::{hash::Hash, str::FromStr};
2
3#[derive(Debug, thiserror::Error)]
4#[error("unknown JSON-LD version `{0}`")]
5pub struct UnknownVersion(pub String);
6
7/// Version number.
8///
9/// The only allowed value is a number with the value `1.1`.
10#[derive(Clone, Copy, PartialOrd, Ord, Debug)]
11pub enum Version {
12	V1_1,
13}
14
15impl Version {
16	pub fn into_bytes(self) -> &'static [u8] {
17		match self {
18			Self::V1_1 => b"1.1",
19		}
20	}
21
22	pub fn into_str(self) -> &'static str {
23		match self {
24			Self::V1_1 => "1.1",
25		}
26	}
27
28	pub fn into_json_number(self) -> &'static json_syntax::Number {
29		unsafe { json_syntax::Number::new_unchecked(self.into_bytes()) }
30	}
31
32	pub fn into_json_number_buf(self) -> json_syntax::NumberBuf {
33		unsafe { json_syntax::NumberBuf::new_unchecked(self.into_bytes().into()) }
34	}
35}
36
37impl PartialEq for Version {
38	fn eq(&self, _other: &Self) -> bool {
39		true
40	}
41}
42
43impl Eq for Version {}
44
45impl Hash for Version {
46	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
47		self.into_str().hash(state)
48	}
49}
50
51impl<'a> From<Version> for &'a json_syntax::Number {
52	fn from(v: Version) -> Self {
53		v.into_json_number()
54	}
55}
56
57impl From<Version> for json_syntax::NumberBuf {
58	fn from(v: Version) -> Self {
59		v.into_json_number_buf()
60	}
61}
62
63impl TryFrom<json_syntax::NumberBuf> for Version {
64	type Error = UnknownVersion;
65
66	fn try_from(value: json_syntax::NumberBuf) -> Result<Self, Self::Error> {
67		if value.trimmed().as_str() == "1.1" {
68			Ok(Self::V1_1)
69		} else {
70			Err(UnknownVersion(value.to_string()))
71		}
72	}
73}
74
75impl FromStr for Version {
76	type Err = UnknownVersion;
77
78	fn from_str(s: &str) -> Result<Self, Self::Err> {
79		if s == "1.1" {
80			Ok(Version::V1_1)
81		} else {
82			Err(UnknownVersion(s.to_owned()))
83		}
84	}
85}
86
87impl TryFrom<f32> for Version {
88	type Error = UnknownVersion;
89
90	fn try_from(value: f32) -> Result<Self, Self::Error> {
91		if value == 1.1 {
92			Ok(Version::V1_1)
93		} else {
94			Err(UnknownVersion(value.to_string()))
95		}
96	}
97}
98
99impl TryFrom<f64> for Version {
100	type Error = UnknownVersion;
101
102	fn try_from(value: f64) -> Result<Self, Self::Error> {
103		if value == 1.1 {
104			Ok(Version::V1_1)
105		} else {
106			Err(UnknownVersion(value.to_string()))
107		}
108	}
109}
110
111#[cfg(feature = "serde")]
112impl serde::Serialize for Version {
113	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
114	where
115		S: serde::Serializer,
116	{
117		self.into_json_number().serialize(serializer)
118	}
119}
120
121#[cfg(feature = "serde")]
122impl<'de> serde::Deserialize<'de> for Version {
123	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
124	where
125		D: serde::Deserializer<'de>,
126	{
127		json_syntax::NumberBuf::deserialize(deserializer)?
128			.try_into()
129			.map_err(serde::de::Error::custom)
130	}
131}