json_ld_core/
mode.rs

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