Skip to main content

json_ld/core/document/
profile.rs

1use rdf_syntax::{Iri, IriBuf, iri};
2
3pub const PROFILE_EXPANDED_IRI: &Iri = iri!("http://www.w3.org/ns/json-ld#expanded");
4pub const PROFILE_COMPACTED_IRI: &Iri = iri!("http://www.w3.org/ns/json-ld#compacted");
5pub const PROFILE_CONTEXT_IRI: &Iri = iri!("http://www.w3.org/ns/json-ld#context");
6pub const PROFILE_FLATTENED_IRI: &Iri = iri!("http://www.w3.org/ns/json-ld#flattened");
7pub const PROFILE_FRAMED_IRI: &Iri = iri!("http://www.w3.org/ns/json-ld#framed");
8
9/// Standard `profile` parameter values defined for the `application/ld+json`.
10///
11/// See: <https://www.w3.org/TR/json-ld11/#iana-considerations>
12#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub enum JsonLdStandardProfile {
14	/// To request or specify expanded JSON-LD document form.
15	Expanded,
16
17	/// To request or specify compacted JSON-LD document form.
18	Compacted,
19
20	/// To request or specify a JSON-LD context document.
21	Context,
22
23	/// To request or specify flattened JSON-LD document form.
24	Flattened,
25
26	// /// To request or specify a JSON-LD frame document.
27	// Frame,
28	/// To request or specify a JSON-LD framed document.
29	Framed,
30}
31
32impl JsonLdStandardProfile {
33	pub fn from_iri(iri: &Iri) -> Option<Self> {
34		if iri == PROFILE_EXPANDED_IRI {
35			Some(Self::Expanded)
36		} else if iri == PROFILE_COMPACTED_IRI {
37			Some(Self::Compacted)
38		} else if iri == PROFILE_CONTEXT_IRI {
39			Some(Self::Context)
40		} else if iri == PROFILE_FLATTENED_IRI {
41			Some(Self::Flattened)
42		} else if iri == PROFILE_FRAMED_IRI {
43			Some(Self::Framed)
44		} else {
45			None
46		}
47	}
48
49	pub fn iri(&self) -> &'static Iri {
50		match self {
51			Self::Expanded => PROFILE_CONTEXT_IRI,
52			Self::Compacted => PROFILE_COMPACTED_IRI,
53			Self::Context => PROFILE_CONTEXT_IRI,
54			Self::Flattened => PROFILE_FLATTENED_IRI,
55			Self::Framed => PROFILE_FRAMED_IRI,
56		}
57	}
58}
59
60/// Value for the `profile` parameter defined for the `application/ld+json`.
61///
62/// Standard values defined by the JSON-LD specification are defined by the
63/// [`JsonLdStandardProfile`] type.
64///
65/// See: <https://www.w3.org/TR/json-ld11/#iana-considerations>
66#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
67pub struct JsonLdProfile(Inner);
68
69#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
70enum Inner {
71	Standard(JsonLdStandardProfile),
72	Custom(IriBuf),
73}
74
75impl JsonLdProfile {
76	pub const EXPANDED: Self = Self(Inner::Standard(JsonLdStandardProfile::Expanded));
77	pub const COMPACTED: Self = Self(Inner::Standard(JsonLdStandardProfile::Compacted));
78	pub const CONTEXT: Self = Self(Inner::Standard(JsonLdStandardProfile::Context));
79	pub const FLATTENED: Self = Self(Inner::Standard(JsonLdStandardProfile::Flattened));
80	pub const FRAMED: Self = Self(Inner::Standard(JsonLdStandardProfile::Framed));
81
82	pub fn new(iri: &Iri) -> Self {
83		match JsonLdStandardProfile::from_iri(iri) {
84			Some(p) => Self(Inner::Standard(p)),
85			None => Self(Inner::Custom(iri.to_owned())),
86		}
87	}
88
89	pub fn iri(&self) -> &Iri {
90		match &self.0 {
91			Inner::Standard(s) => s.iri(),
92			Inner::Custom(c) => c,
93		}
94	}
95
96	pub fn as_standard(&self) -> Option<JsonLdStandardProfile> {
97		match &self.0 {
98			Inner::Standard(s) => Some(*s),
99			Inner::Custom(_) => None,
100		}
101	}
102
103	pub fn into_standard(self) -> Result<JsonLdStandardProfile, IriBuf> {
104		match self.0 {
105			Inner::Standard(s) => Ok(s),
106			Inner::Custom(e) => Err(e),
107		}
108	}
109}