Skip to main content

jsonld_core/rdf/
mod.rs

1//! Conversion of expanded JSON-LD documents to RDF, and the well-known IRI
2//! constants the conversion needs.
3//!
4//! [`quad`] walks an expanded document and yields its quads; the
5//! triple-level machinery that turns individual objects into RDF terms —
6//! minting blank node identifiers through a generator and interning IRIs in a
7//! vocabulary — is re-exported alongside it.
8use iri_rs::{Iri, iri};
9
10/// RDF quads produced from an expanded document.
11pub mod quad;
12mod triples;
13
14pub use quad::*;
15pub use triples::*;
16
17/// The `rdf:type` IRI.
18pub const RDF_TYPE: Iri<&'static str> = iri!("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
19/// The `rdf:first` IRI, holding the head of a list.
20pub const RDF_FIRST: Iri<&'static str> = iri!("http://www.w3.org/1999/02/22-rdf-syntax-ns#first");
21/// The `rdf:rest` IRI, holding the tail of a list.
22pub const RDF_REST: Iri<&'static str> = iri!("http://www.w3.org/1999/02/22-rdf-syntax-ns#rest");
23/// The `rdf:value` IRI.
24pub const RDF_VALUE: Iri<&'static str> = iri!("http://www.w3.org/1999/02/22-rdf-syntax-ns#value");
25/// The `rdf:direction` IRI, carrying the base direction of a string.
26pub const RDF_DIRECTION: Iri<&'static str> = iri!("http://www.w3.org/1999/02/22-rdf-syntax-ns#direction");
27/// The `rdf:JSON` datatype IRI.
28pub const RDF_JSON: Iri<&'static str> = iri!("http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON");
29/// IRI of the `http://www.w3.org/1999/02/22-rdf-syntax-ns#nil` value.
30pub const RDF_NIL: Iri<&'static str> = iri!("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil");
31
32/// The `xsd:boolean` datatype IRI.
33pub const XSD_BOOLEAN: Iri<&'static str> = iri!("http://www.w3.org/2001/XMLSchema#boolean");
34/// The `xsd:integer` datatype IRI.
35pub const XSD_INTEGER: Iri<&'static str> = iri!("http://www.w3.org/2001/XMLSchema#integer");
36/// The `xsd:double` datatype IRI.
37pub const XSD_DOUBLE: Iri<&'static str> = iri!("http://www.w3.org/2001/XMLSchema#double");
38/// The `xsd:string` datatype IRI.
39pub const XSD_STRING: Iri<&'static str> = iri!("http://www.w3.org/2001/XMLSchema#string");
40
41/// Direction representation method.
42///
43/// Used by the RDF serializer to decide how to encode
44/// [`Direction`](crate::Direction)s.
45#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
46pub enum RdfDirection {
47    /// Encode direction in the string value type IRI using the
48    /// `https://www.w3.org/ns/i18n#` prefix.
49    I18nDatatype,
50
51    /// Encode the direction using a compound literal value.
52    CompoundLiteral,
53}
54
55#[derive(Debug, Clone, thiserror::Error)]
56#[error("invalid RDF direction: {0}")]
57/// Error raised when a string is not a valid base direction.
58pub struct InvalidRdfDirection(pub String);
59
60impl std::str::FromStr for RdfDirection {
61    type Err = InvalidRdfDirection;
62
63    fn from_str(s: &str) -> Result<Self, Self::Err> {
64        match s {
65            "i18n-datatype" => Ok(Self::I18nDatatype),
66            "compound-literal" => Ok(Self::CompoundLiteral),
67            _ => Err(InvalidRdfDirection(s.to_string())),
68        }
69    }
70}
71
72impl<'a> TryFrom<&'a str> for RdfDirection {
73    type Error = InvalidRdfDirection;
74
75    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
76        value.parse()
77    }
78}