json_ld_syntax_next/context/definition/
vocab.rs

1use crate::{CompactIri, ExpandableRef};
2use iref::Iri;
3use rdf_types::BlankId;
4
5#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7#[cfg_attr(feature = "serde", serde(transparent))]
8pub struct Vocab(String);
9
10impl Vocab {
11	pub fn as_iri(&self) -> Option<&Iri> {
12		Iri::new(&self.0).ok()
13	}
14
15	pub fn as_compact_iri(&self) -> Option<&CompactIri> {
16		CompactIri::new(&self.0).ok()
17	}
18
19	pub fn as_blank_id(&self) -> Option<&BlankId> {
20		BlankId::new(&self.0).ok()
21	}
22
23	pub fn as_str(&self) -> &str {
24		&self.0
25	}
26
27	pub fn into_string(self) -> String {
28		self.0
29	}
30}
31
32impl From<String> for Vocab {
33	fn from(s: String) -> Self {
34		Self(s)
35	}
36}
37
38impl<'a> From<&'a Vocab> for ExpandableRef<'a> {
39	fn from(v: &'a Vocab) -> Self {
40		ExpandableRef::String(&v.0)
41	}
42}