Skip to main content

json_ld/core/
processing_mode.rs

1use linked_data::{DeserializeLinkedData, SerializeLinkedData};
2use std::convert::TryFrom;
3use std::fmt;
4
5/// Processing mode.
6///
7/// This is a property of the context processing and compaction options.
8/// New features defined in JSON-LD 1.1 are available unless the processing mode is set to [`JsonLdProcessingMode::JsonLd1_0`].
9#[derive(
10	Default, Clone, Copy, PartialEq, Eq, Hash, Debug, SerializeLinkedData, DeserializeLinkedData,
11)]
12pub enum JsonLdProcessingMode {
13	/// JSON-LD 1.0.
14	#[ld(literal = "json-ld-1.0")]
15	JsonLd1_0,
16
17	/// JSON-LD 1.1.
18	#[default]
19	#[ld(literal = "json-ld-1.1")]
20	JsonLd1_1,
21}
22
23impl JsonLdProcessingMode {
24	/// Returns the name of the processing mode.
25	#[inline(always)]
26	pub fn as_str(&self) -> &str {
27		match self {
28			JsonLdProcessingMode::JsonLd1_0 => "json-ld-1.0",
29			JsonLdProcessingMode::JsonLd1_1 => "json-ld-1.1",
30		}
31	}
32}
33
34impl<'a> TryFrom<&'a str> for JsonLdProcessingMode {
35	type Error = ();
36
37	fn try_from(name: &'a str) -> Result<JsonLdProcessingMode, ()> {
38		match name {
39			"json-ld-1.0" => Ok(JsonLdProcessingMode::JsonLd1_0),
40			"json-ld-1.1" => Ok(JsonLdProcessingMode::JsonLd1_1),
41			_ => Err(()),
42		}
43	}
44}
45
46impl fmt::Display for JsonLdProcessingMode {
47	#[inline(always)]
48	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49		write!(f, "{}", self.as_str())
50	}
51}