rdfx/lib.rs
1//! The [Resource Description Framework (RDF)][rdf] is a very simple graph data
2//! model defined by the [World Wide Web Consortium (W3C)][w3c] to represent
3//! arbitrary pieces of information, primarily intended for the web. Nodes of
4//! the graph are called *resources*, and resources are connected together using
5//! *relations*, which are resources themselves.
6//!
7//! This is a utility library providing common types, data-structures, traits,
8//! constants and macro definitions to deal with RDF data:
9//! - IRIs (through the [`iri-rs`](iri_rs) crate), blank node identifiers and
10//! literals to represent resources in their lexical form as *terms*;
11//! - Triples and quads;
12//! - Interpretations projecting resources from the lexical domain to the value
13//! domain;
14//! - Graphs and datasets representing collections of interpreted triples/quads.
15//!
16//! # Getting started
17//!
18//! Terms, triples and quads are built with the [`triple!`] and [`quad!`]
19//! macros, which accept the same lexical syntax as N-Triples/N-Quads:
20//!
21//! ```
22//! use rdfx::{LocalTerm, triple};
23//!
24//! // RDF 1.1: a language-tagged literal in object position.
25//! let t = triple!(<"http://example.org/alice"> <"http://xmlns.com/foaf/0.1/name"> "Alice" @ "en");
26//!
27//! // RDF 1.2: a triple term as the object.
28//! let annotated = triple!(
29//! <"http://example.org/bob"> <"http://example.org/says">
30//! <<( <"http://example.org/s"> <"http://example.org/p"> <"http://example.org/o"> )>>
31//! );
32//! assert!(matches!(annotated.into_object(), LocalTerm::Triple(_)));
33//! ```
34//!
35//! Storage is uniform over a single resource type:
36//! [`BTreeDataset<R>`](dataset::BTreeDataset),
37//! [`HashDataset<R>`](dataset::HashDataset) and their indexed variants hold
38//! the same `R: Resource` in every position. [`LocalTerm`] is deliberately *not* a [`Resource`] —
39//! a literal cannot be a subject — so store interned handles from a
40//! [`vocabulary`], or wrap the lexical term in a newtype opted in with
41//! [`impl_resource!`]:
42//!
43//! ```
44//! use rdfx::{Quad, dataset::{BTreeDataset, TraversableDataset}};
45//!
46//! let mut dataset: BTreeDataset<usize> = BTreeDataset::new();
47//! dataset.insert(Quad(1, 0, 2, None));
48//! dataset.insert(Quad(2, 0, 3, Some(9)));
49//!
50//! assert_eq!(dataset.quads_count(), 2);
51//! ```
52//!
53//! Blank node isomorphism lives in [`dataset::isomorphism`]: the plain
54//! entry points answer yes or no, while
55//! [`find_bijection_with`](dataset::isomorphism::find_bijection_with) returns
56//! the blank node mapping. The `_with` variants take an
57//! [`Interpretation`](interpretation::Interpretation), which is what makes
58//! blank nodes and triple-term interiors visible to the matcher; without one
59//! resources are opaque and equivalence degrades to set equality.
60//!
61//! ```
62//! use rdfx::{Quad, dataset::{BTreeDataset, isomorphism::dataset_equivalent}};
63//!
64//! let mut a: BTreeDataset<usize> = BTreeDataset::new();
65//! a.insert(Quad(1, 0, 2, None));
66//!
67//! let mut b: BTreeDataset<usize> = BTreeDataset::new();
68//! b.insert(Quad(1, 0, 2, None));
69//!
70//! assert!(dataset_equivalent(&a, &b));
71//! ```
72//!
73//! # RDF 1.2
74//!
75//! This crate targets [RDF 1.2 Concepts][rdf12] alongside RDF 1.1 — RDF 1.1
76//! graphs are a syntactic subset (they never construct the new variants):
77//!
78//! - **Triple terms** ([RDF 1.2 §4][s32-tt]) — a triple appearing as an RDF
79//! term, restricted to object position in strict RDF (any position via the
80//! [`GeneralizedTriple`] / [`GeneralizedLocalTerm`] types). Carried by
81//! [`LocalTerm::Triple`]; aliased as [`TripleTerm`]. Macro syntax
82//! `<<( s p o )>>`.
83//! - **Directional language strings** ([RDF 1.2 §3.4][s32-dls]) — language
84//! tag plus base direction (`ltr` / `rtl`), datatype `rdf:dirLangString`.
85//! Carried by [`LiteralType::DirLangString`]; constructor
86//! [`Literal::dir_lang_string`]; helper enum [`Direction`]. Macro syntax
87//! `"v" @ "lang" / "ltr"`.
88//! - **`rdf:reifies` annotation model** ([RDF 1.2 §6][s32-ann]) — a
89//! round-trippable mapping between triple-term-bearing graphs and their
90//! reified RDF 1.1-compatible form, exposed as
91//! [`star::unstar_graph`] / [`star::restar_graph`] (and the dataset
92//! variants).
93//!
94//! ```
95//! use rdfx::{Direction, LiteralType, LocalTerm, Term, triple};
96//!
97//! let t = triple!(<"http://example.org/doc"> <"http://example.org/title"> "hello" @ "ar" / "rtl");
98//! let LocalTerm::Named(Term::Literal(literal)) = t.into_object() else { unreachable!() };
99//!
100//! assert!(matches!(literal.into_type(), LiteralType::DirLangString { direction: Direction::Rtl, .. }));
101//! ```
102//!
103//! # Feature flags
104//!
105//! All features are off by default.
106//!
107//! - `serde` — `Serialize` / `Deserialize` for terms, literals, triples and
108//! quads.
109//! - `meta` — the [`meta`] module: [`locspan`]-tagged values and the
110//! [`Strip`](meta::Strip) trait.
111//! - `contextual` — `Display` through a vocabulary via the
112//! [`contextual`](https://crates.io/crates/contextual) crate.
113//! - `uuid-generator`, `uuid-generator-v3`, `uuid-generator-v4`,
114//! `uuid-generator-v5` — UUID-based blank node generators.
115//!
116//! [rdf]: <https://w3c.github.io/rdf-primer/spec/>
117//! [w3c]: <https://www.w3.org/>
118//! [rdf12]: <https://www.w3.org/TR/rdf12-concepts/>
119//! [s32-tt]: <https://www.w3.org/TR/rdf12-concepts/#section-triples>
120//! [s32-dls]: <https://www.w3.org/TR/rdf12-concepts/#section-Graph-Literal>
121//! [s32-ann]: <https://www.w3.org/TR/rdf12-concepts/#section-reification>
122#![recursion_limit = "1024"]
123
124#[doc(hidden)]
125pub use iri_rs;
126
127#[doc(hidden)]
128pub use langtag;
129
130mod blankid;
131mod display;
132mod generalized;
133mod id;
134mod literal;
135mod r#macro;
136mod positions;
137mod quad;
138mod schema;
139mod swar;
140mod term;
141mod triple;
142
143pub use blankid::*;
144pub use display::*;
145pub use generalized::*;
146pub use id::*;
147pub use literal::*;
148#[doc(hidden)]
149pub use positions::__seal;
150pub use positions::{IsGraph, IsObject, IsPredicate, IsSubject, Resource};
151pub use quad::*;
152pub use schema::*;
153pub use term::*;
154pub use triple::*;
155
156pub mod dataset;
157pub mod interpretation;
158pub mod pattern;
159pub mod star;
160pub mod utils;
161pub mod vocabulary;
162
163#[cfg(feature = "meta")]
164pub mod meta;
165
166pub use dataset::Dataset;
167pub use interpretation::{Interpretation, InterpretationMut};
168pub use iri_rs::{Iri, IriBuf, iri};
169
170pub const XSD_STRING: Iri<&'static str> = iri!("http://www.w3.org/2001/XMLSchema#string");