Skip to main content

jsonld_syntax/
expandable.rs

1use std::fmt;
2
3use crate::{Keyword, context::definition::KeyOrKeyword};
4
5/// Borrowed value that IRI expansion can be applied to.
6///
7/// Keywords are recognised while borrowing, so that expansion does not have to
8/// match against the keyword list again.
9pub enum ExpandableRef<'a> {
10    /// A JSON-LD keyword.
11    Keyword(Keyword),
12
13    /// Anything else: a term, a compact IRI, an IRI or a blank node
14    /// identifier.
15    String(&'a str),
16}
17
18impl<'a> From<&'a KeyOrKeyword> for ExpandableRef<'a> {
19    fn from(k: &'a KeyOrKeyword) -> Self {
20        match k {
21            KeyOrKeyword::Keyword(k) => Self::Keyword(*k),
22            KeyOrKeyword::Key(k) => Self::String(k.as_str()),
23        }
24    }
25}
26
27impl<'a> From<&'a str> for ExpandableRef<'a> {
28    fn from(s: &'a str) -> Self {
29        match Keyword::try_from(s) {
30            Ok(k) => Self::Keyword(k),
31            Err(_) => Self::String(s),
32        }
33    }
34}
35
36impl fmt::Display for ExpandableRef<'_> {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        match self {
39            Self::Keyword(k) => k.fmt(f),
40            Self::String(s) => s.fmt(f),
41        }
42    }
43}