json_ld_syntax_next/context/term_definition/
id.rs1use crate::context::definition::KeyOrKeywordRef;
2use crate::{CompactIri, ExpandableRef, Keyword};
3use iref::Iri;
4use rdf_types::BlankId;
5use std::fmt;
6use std::hash::Hash;
7
8#[derive(Clone, PartialOrd, Ord, Debug)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[cfg_attr(feature = "serde", serde(untagged))]
11pub enum Id {
12 Keyword(Keyword),
13 Term(String),
14}
15
16impl Id {
17 pub fn as_iri(&self) -> Option<&Iri> {
18 match self {
19 Self::Term(t) => Iri::new(t).ok(),
20 Self::Keyword(_) => None,
21 }
22 }
23
24 pub fn as_blank_id(&self) -> Option<&BlankId> {
25 match self {
26 Self::Term(t) => BlankId::new(t).ok(),
27 Self::Keyword(_) => None,
28 }
29 }
30
31 pub fn as_compact_iri(&self) -> Option<&CompactIri> {
32 match self {
33 Self::Term(t) => CompactIri::new(t).ok(),
34 Self::Keyword(_) => None,
35 }
36 }
37
38 pub fn as_keyword(&self) -> Option<Keyword> {
39 match self {
40 Self::Keyword(k) => Some(*k),
41 Self::Term(_) => None,
42 }
43 }
44
45 pub fn as_str(&self) -> &str {
46 match self {
47 Self::Term(t) => t.as_str(),
48 Self::Keyword(k) => k.into_str(),
49 }
50 }
51
52 pub fn into_string(self) -> String {
53 match self {
54 Self::Term(t) => t,
55 Self::Keyword(k) => k.to_string(),
56 }
57 }
58
59 pub fn is_keyword(&self) -> bool {
60 matches!(self, Self::Keyword(_))
61 }
62
63 pub fn is_keyword_like(&self) -> bool {
64 crate::is_keyword_like(self.as_str())
65 }
66
67 pub fn as_id_ref(&self) -> IdRef {
68 match self {
69 Self::Term(t) => IdRef::Term(t),
70 Self::Keyword(k) => IdRef::Keyword(*k),
71 }
72 }
73}
74
75impl PartialEq for Id {
76 fn eq(&self, other: &Self) -> bool {
77 match (self, other) {
78 (Self::Term(a), Self::Term(b)) => a == b,
79 (Self::Keyword(a), Self::Keyword(b)) => a == b,
80 _ => false,
81 }
82 }
83}
84
85impl Eq for Id {}
86
87impl Hash for Id {
88 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
89 self.as_str().hash(state)
90 }
91}
92
93impl From<String> for Id {
94 fn from(s: String) -> Self {
95 match Keyword::try_from(s.as_str()) {
96 Ok(k) => Self::Keyword(k),
97 Err(_) => Self::Term(s),
98 }
99 }
100}
101
102impl<'a> From<&'a Id> for ExpandableRef<'a> {
103 fn from(i: &'a Id) -> Self {
104 match i {
105 Id::Term(t) => Self::String(t),
106 Id::Keyword(k) => Self::Keyword(*k),
107 }
108 }
109}
110
111impl fmt::Display for Id {
112 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
113 match self {
114 Self::Term(t) => t.fmt(f),
115 Self::Keyword(k) => k.fmt(f),
116 }
117 }
118}
119
120#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
121pub enum IdRef<'a> {
122 Term(&'a str),
123 Keyword(Keyword),
124}
125
126impl IdRef<'_> {
127 pub fn as_str(&self) -> &str {
128 match self {
129 Self::Term(t) => t,
130 Self::Keyword(k) => k.into_str(),
131 }
132 }
133
134 pub fn is_keyword(&self) -> bool {
135 matches!(self, Self::Keyword(_))
136 }
137
138 pub fn is_keyword_like(&self) -> bool {
139 crate::is_keyword_like(self.as_str())
140 }
141}
142
143impl<'a> From<&'a str> for IdRef<'a> {
144 fn from(s: &'a str) -> Self {
145 match Keyword::try_from(s) {
146 Ok(k) => Self::Keyword(k),
147 Err(_) => Self::Term(s),
148 }
149 }
150}
151
152impl<'a> From<IdRef<'a>> for ExpandableRef<'a> {
153 fn from(i: IdRef<'a>) -> Self {
154 match i {
155 IdRef::Term(t) => Self::String(t),
156 IdRef::Keyword(k) => Self::Keyword(k),
157 }
158 }
159}
160
161impl fmt::Display for IdRef<'_> {
162 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163 self.as_str().fmt(f)
164 }
165}
166
167impl<'a> From<IdRef<'a>> for KeyOrKeywordRef<'a> {
168 fn from(i: IdRef<'a>) -> Self {
169 match i {
170 IdRef::Term(t) => Self::Key(t.into()),
171 IdRef::Keyword(k) => Self::Keyword(k),
172 }
173 }
174}