jsonld-core 0.23.2

JSON-LD core types and traits
Documentation
use std::{convert::TryFrom, fmt};

/// Processing mode.
///
/// This is a property of the context processing and compaction options.
/// New features defined in JSON-LD 1.1 are available unless the processing mode is set to [`ProcessingMode::JsonLd1_0`].
#[derive(Default, Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum ProcessingMode {
    /// JSON-LD 1.0.
    JsonLd1_0,

    /// JSON-LD 1.1.
    #[default]
    JsonLd1_1,
}

impl ProcessingMode {
    /// Returns the name of the processing mode.
    #[inline(always)]
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            ProcessingMode::JsonLd1_0 => "json-ld-1.0",
            ProcessingMode::JsonLd1_1 => "json-ld-1.1",
        }
    }
}

/// Error raised when parsing an unknown processing-mode name.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("unknown processing mode `{0}`")]
pub struct UnknownProcessingMode(pub String);

impl<'a> TryFrom<&'a str> for ProcessingMode {
    type Error = UnknownProcessingMode;

    fn try_from(name: &'a str) -> Result<ProcessingMode, UnknownProcessingMode> {
        match name {
            "json-ld-1.0" => Ok(ProcessingMode::JsonLd1_0),
            "json-ld-1.1" => Ok(ProcessingMode::JsonLd1_1),
            _ => Err(UnknownProcessingMode(name.to_owned())),
        }
    }
}

impl fmt::Display for ProcessingMode {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}