Skip to main content

jsonld_core/
mode.rs

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