Skip to main content

json_ld/syntax/context/definition/
vocab.rs

1use crate::syntax::{JsonLdCompactIri, JsonLdExpandableRef};
2use rdf_syntax::BlankId;
3use rdf_syntax::Iri;
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 JsonLdVocab(String);
9
10impl JsonLdVocab {
11	pub fn as_iri(&self) -> Option<&Iri> {
12		Iri::new(&self.0).ok()
13	}
14
15	pub fn as_compact_iri(&self) -> Option<&JsonLdCompactIri> {
16		JsonLdCompactIri::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 JsonLdVocab {
33	fn from(s: String) -> Self {
34		Self(s)
35	}
36}
37
38impl<'a> From<&'a JsonLdVocab> for JsonLdExpandableRef<'a> {
39	fn from(v: &'a JsonLdVocab) -> Self {
40		JsonLdExpandableRef::String(&v.0)
41	}
42}