Skip to main content

jsonld_syntax/context/term_definition/
nest.rs

1use std::hash::Hash;
2
3use crate::is_keyword;
4
5#[derive(Clone, PartialOrd, Ord, Debug)]
6/// Value of the `@nest` entry of a term definition: the term under which the
7/// property is nested.
8pub enum Nest {
9    /// The `@nest` keyword itself, nesting the property under a plain `@nest`
10    /// entry.
11    Nest,
12
13    /// Another term of the context, which must not be a keyword.
14    Term(String),
15}
16
17impl Nest {
18    /// Returns this value as it is spelled in the context.
19    #[must_use]
20    pub fn as_str(&self) -> &str {
21        match self {
22            Self::Nest => "@nest",
23            Self::Term(t) => t,
24        }
25    }
26
27    /// Converts this value into an owned `String`, allocating for the `@nest`
28    /// keyword.
29    #[must_use]
30    pub fn into_string(self) -> String {
31        match self {
32            Self::Nest => "@nest".to_string(),
33            Self::Term(t) => t,
34        }
35    }
36}
37
38impl PartialEq for Nest {
39    fn eq(&self, other: &Self) -> bool {
40        match (self, other) {
41            (Self::Nest, Self::Nest) => true,
42            (Self::Term(a), Self::Term(b)) => a == b,
43            _ => false,
44        }
45    }
46}
47
48impl Eq for Nest {}
49
50impl Hash for Nest {
51    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
52        self.as_str().hash(state);
53    }
54}
55
56#[derive(Debug, thiserror::Error)]
57#[error("invalid `@nest` value")]
58/// Error raised when a `@nest` value is a keyword other than `@nest`.
59pub struct InvalidNest(pub String);
60
61impl TryFrom<String> for Nest {
62    type Error = InvalidNest;
63
64    fn try_from(s: String) -> Result<Self, InvalidNest> {
65        if s == "@nest" {
66            Ok(Self::Nest)
67        } else if is_keyword(&s) {
68            Err(InvalidNest(s))
69        } else {
70            Ok(Self::Term(s))
71        }
72    }
73}
74
75#[cfg(feature = "serde")]
76impl serde::Serialize for Nest {
77    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
78    where
79        S: serde::Serializer,
80    {
81        self.as_str().serialize(serializer)
82    }
83}
84
85#[cfg(feature = "serde")]
86impl<'de> serde::Deserialize<'de> for Nest {
87    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
88    where
89        D: serde::Deserializer<'de>,
90    {
91        struct Visitor;
92
93        impl serde::de::Visitor<'_> for Visitor {
94            type Value = Nest;
95
96            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
97                formatter.write_str("`@nest` value")
98            }
99
100            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
101            where
102                E: serde::de::Error,
103            {
104                self.visit_string(v.to_owned())
105            }
106
107            fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
108            where
109                E: serde::de::Error,
110            {
111                Nest::try_from(v).map_err(|e| E::custom(e))
112            }
113        }
114
115        deserializer.deserialize_string(Visitor)
116    }
117}