Skip to main content

json_ld/syntax/
expandable.rs

1use std::fmt;
2
3use crate::syntax::context::JsonLdKeyOrKeyword;
4
5use super::JsonLdKeyword;
6
7pub enum JsonLdExpandable {
8	Keyword(JsonLdKeyword),
9	String(String),
10}
11
12pub enum JsonLdExpandableRef<'a> {
13	/// [`JsonLdKeyword`].
14	Keyword(JsonLdKeyword),
15
16	/// Other term.
17	String(&'a str),
18}
19
20impl<'a> From<&'a JsonLdKeyOrKeyword> for JsonLdExpandableRef<'a> {
21	fn from(k: &'a JsonLdKeyOrKeyword) -> Self {
22		match k {
23			JsonLdKeyOrKeyword::Keyword(k) => Self::Keyword(*k),
24			JsonLdKeyOrKeyword::Key(k) => Self::String(k.as_str()),
25		}
26	}
27}
28
29impl<'a> From<&'a str> for JsonLdExpandableRef<'a> {
30	fn from(s: &'a str) -> Self {
31		match JsonLdKeyword::try_from(s) {
32			Ok(k) => Self::Keyword(k),
33			Err(_) => Self::String(s),
34		}
35	}
36}
37
38impl<'a> fmt::Display for JsonLdExpandableRef<'a> {
39	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40		match self {
41			Self::Keyword(k) => k.fmt(f),
42			Self::String(s) => s.fmt(f),
43		}
44	}
45}