Expand description
The Resource Description Framework (RDF) is a very simple graph data model defined by the World Wide Web Consortium (W3C) to represent arbitrary pieces of information, primarily intended for the web. Nodes of the graph are called resources, and resources are connected together using relations, which are resources themselves.
This is a utility library providing common types, data-structures, traits, constants and macro definitions to deal with RDF data:
- IRIs (through the
iri-rscrate), blank node identifiers and literals to represent resources in their lexical form as terms; - Triples and quads;
- Interpretations projecting resources from the lexical domain to the value domain;
- Graphs and datasets representing collections of interpreted triples/quads.
§Getting started
Terms, triples and quads are built with the triple! and quad!
macros, which accept the same lexical syntax as N-Triples/N-Quads:
use rdfx::{LocalTerm, triple};
// RDF 1.1: a language-tagged literal in object position.
let t = triple!(<"http://example.org/alice"> <"http://xmlns.com/foaf/0.1/name"> "Alice" @ "en");
// RDF 1.2: a triple term as the object.
let annotated = triple!(
<"http://example.org/bob"> <"http://example.org/says">
<<( <"http://example.org/s"> <"http://example.org/p"> <"http://example.org/o"> )>>
);
assert!(matches!(annotated.into_object(), LocalTerm::Triple(_)));Storage is uniform over a single resource type:
BTreeDataset<R>,
HashDataset<R> and their indexed variants hold
the same R: Resource in every position. LocalTerm is deliberately not a Resource —
a literal cannot be a subject — so store interned handles from a
vocabulary, or wrap the lexical term in a newtype opted in with
impl_resource!:
use rdfx::{Quad, dataset::{BTreeDataset, TraversableDataset}};
let mut dataset: BTreeDataset<usize> = BTreeDataset::new();
dataset.insert(Quad(1, 0, 2, None));
dataset.insert(Quad(2, 0, 3, Some(9)));
assert_eq!(dataset.quads_count(), 2);Blank node isomorphism lives in dataset::isomorphism: the plain
entry points answer yes or no, while
find_bijection_with returns
the blank node mapping. The _with variants take an
Interpretation, which is what makes
blank nodes and triple-term interiors visible to the matcher; without one
resources are opaque and equivalence degrades to set equality.
use rdfx::{Quad, dataset::{BTreeDataset, isomorphism::dataset_equivalent}};
let mut a: BTreeDataset<usize> = BTreeDataset::new();
a.insert(Quad(1, 0, 2, None));
let mut b: BTreeDataset<usize> = BTreeDataset::new();
b.insert(Quad(1, 0, 2, None));
assert!(dataset_equivalent(&a, &b));§RDF 1.2
This crate targets RDF 1.2 Concepts alongside RDF 1.1 — RDF 1.1 graphs are a syntactic subset (they never construct the new variants):
- Triple terms (RDF 1.2 §4) — a triple appearing as an RDF
term, restricted to object position in strict RDF (any position via the
GeneralizedTriple/GeneralizedLocalTermtypes). Carried byLocalTerm::Triple; aliased asTripleTerm. Macro syntax<<( s p o )>>. - Directional language strings (RDF 1.2 §3.4) — language
tag plus base direction (
ltr/rtl), datatyperdf:dirLangString. Carried byLiteralType::DirLangString; constructorLiteral::dir_lang_string; helper enumDirection. Macro syntax"v" @ "lang" / "ltr". rdf:reifiesannotation model (RDF 1.2 §6) — a round-trippable mapping between triple-term-bearing graphs and their reified RDF 1.1-compatible form, exposed asstar::unstar_graph/star::restar_graph(and the dataset variants).
use rdfx::{Direction, LiteralType, LocalTerm, Term, triple};
let t = triple!(<"http://example.org/doc"> <"http://example.org/title"> "hello" @ "ar" / "rtl");
let LocalTerm::Named(Term::Literal(literal)) = t.into_object() else { unreachable!() };
assert!(matches!(literal.into_type(), LiteralType::DirLangString { direction: Direction::Rtl, .. }));§Feature flags
All features are off by default.
serde—Serialize/Deserializefor terms, literals, triples and quads.meta— themetamodule:locspan-tagged values and theStriptrait.contextual—Displaythrough a vocabulary via thecontextualcrate.uuid-generator,uuid-generator-v3,uuid-generator-v4,uuid-generator-v5— UUID-based blank node generators.
Re-exports§
pub use dataset::Dataset;pub use interpretation::Interpretation;pub use interpretation::InterpretationMut;
Modules§
- dataset
- Dataset traits and implementations.
- generator
- interpretation
- Resource interpretations.
- iri
- Compatibility namespace.
- meta
- Metadata-tagged terms and triples/quads, gated on the
metafeature. - pattern
- star
- RDF 1.2 reification helpers — round-trippable mapping between
triple-term-bearing graphs/datasets and their RDF 1.1-compatible reified
form using the
rdf:reifiesannotation vocabulary. - utils
- vocabulary
- Lexical domain abstractions.
Macros§
- generalized_
quad - Creates a generalized RDF quad (
GeneralizedQuad<LocalTerm>). - generalized_
quad_ pattern - Creates a generalized RDF quad pattern.
- generalized_
quads - Creates an array of generalized quads.
- generalized_
triple - Creates a generalized RDF triple (
GeneralizedTriple<LocalTerm>). - generalized_
triples - Creates an array of generalized triples.
- impl_
resource - Implements
Resourceand the four position traits for a type, granting it eligibility in every position of a triple/quad. - iri
- quad
- Creates a strict RDF 1.1 / 1.2 quad (
Quad<Id, IriBuf, LocalTerm, Id>). - quad_
pattern - Creates a strict quad pattern.
- quads
- Creates an array of strict RDF 1.1 / 1.2 quads.
- triple
- Creates a strict RDF 1.1 / 1.2 triple (
Triple<Id, IriBuf, LocalTerm>). - triple_
pattern - Creates a strict triple pattern
Triple<ResourceOrVar<Id, _>, ResourceOrVar<IriBuf, _>, ResourceOrVar<LocalTerm, _>>. - triples
- Creates an array of strict RDF 1.1 / 1.2 triples.
Structs§
- BlankId
- Blank node identifier.
- Blank
IdBuf - Owned blank node identifier.
- CowLiteral
- RDF Literal reference.
- Datatype
- Validated typed-literal datatype IRI.
- Datatype
Ref - Borrowed counterpart of
Datatype. - Generalized
Quad - Generalized RDF quad — no position bounds on its components.
- Generalized
Triple - Generalized RDF triple — no position bounds on its components.
- Invalid
Blank Id - Invalid blank node identifier.
- Invalid
Direction - Returned by
Direction::parsefor an unknown direction string. - Iri
- Literal
- RDF Literal.
- Literal
Ref - RDF Literal reference.
- Quad
- RDF quad.
- RdfDisplayed
- Value ready to be formatted as an RDF syntax element.
- Triple
- RDF triple.
Enums§
- CowId
- CowLiteral
Type - Owned or referenced RDF literal type.
- CowLocal
Term - CowTerm
- Direction
- Base direction of a directional language-tagged string.
- Generalization
Error - Reasons a
GeneralizedTripleorGeneralizedQuadcannot be converted to a strictTriple/Quad. - Generalized
Local Term - Fully-generalized local term — like
LocalTermbut its triple-term bodies may themselves carry literal subjects, blank predicates, and nested triple terms in any position. - Id
- IdRef
- Literal
Type - RDF literal type.
- Literal
Type Ref - RDF literal type reference.
- Local
Term - Lexical representation of an RDF resource.
- Local
Term Ref - Lexical RDF term reference.
- Quad
Export Failed - Error returned when calling
try_extract_from_vocabularyon aQuad. - Reserved
Datatype Error - Error returned when attempting to construct a
Datatypefrom one of the RDF-reserved language-string datatype IRIs. - Term
- Lexical representation of an RDF resource.
- TermRef
- Lexical RDF term reference.
Constants§
- RDFS_
CLASS - The
rdfs:ClassIRI, http://www.w3.org/2000/01/rdf-schema#Class. - RDFS_
COMMENT - The
rdfs:commentIRI, http://www.w3.org/2000/01/rdf-schema#comment. - RDFS_
CONTAINER - The
rdfs:ContainerIRI, http://www.w3.org/2000/01/rdf-schema#Container. - RDFS_
CONTAINER_ MEMBERSHIP_ PROPERTY - The
rdfs:ContainerMembershipPropertyIRI, http://www.w3.org/2000/01/rdf-schema#ContainerMembershipProperty. - RDFS_
DATATYPE - The
rdfs:DatatypeIRI, http://www.w3.org/2000/01/rdf-schema#Datatype. - RDFS_
DOMAIN - The
rdfs:domainIRI, http://www.w3.org/2000/01/rdf-schema#domain. - RDFS_
IS_ DEFINED_ BY - The
rdfs:isDefinedByIRI, http://www.w3.org/2000/01/rdf-schema#isDefinedBy. - RDFS_
LABEL - The
rdfs:labelIRI, http://www.w3.org/2000/01/rdf-schema#label. - RDFS_
LITERAL - The
rdfs:LiteralIRI, http://www.w3.org/2000/01/rdf-schema#Literal. - RDFS_
MEMBER - The
rdfs:memberIRI, http://www.w3.org/2000/01/rdf-schema#member. - RDFS_
RANGE - The
rdfs:rangeIRI, http://www.w3.org/2000/01/rdf-schema#range. - RDFS_
RESOURCE - The
rdfs:ResourceIRI, http://www.w3.org/2000/01/rdf-schema#Resource. - RDFS_
SEE_ ALSO - The
rdfs:seeAlsoIRI, http://www.w3.org/2000/01/rdf-schema#seeAlso. - RDFS_
SUB_ CLASS_ OF - The
rdfs:subClassOfIRI, http://www.w3.org/2000/01/rdf-schema#subClassOf. - RDFS_
SUB_ PROPERTY_ OF - The
rdfs:subPropertyOfIRI, http://www.w3.org/2000/01/rdf-schema#subPropertyOf. - RDF_ALT
- The
rdf:AltIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#Alt. - RDF_BAG
- The
rdf:BagIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag. - RDF_
DIR_ LANG_ STRING rdf:dirLangString— RDF 1.2 directional language-tagged literal datatype.- RDF_
FIRST - The
rdf:firstIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#first. - RDF_
HTML - The
rdf:HTMLIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML. - RDF_
JSON - The
rdf:JSONIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON. - RDF_
LANG_ STRING - The
rdf:langStringIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#langString. - RDF_
LIST - The
rdf:ListIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#List. - RDF_NIL
- The
rdf:nilIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#nil. - RDF_
OBJECT - The
rdf:objectIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#object. - RDF_
PREDICATE - The
rdf:predicateIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate. - RDF_
PROPERTY - The
rdf:PropertyIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#Property. - RDF_
REIFIES rdf:reifies— RDF 1.2 annotation predicate linking a reifier to its reified triple term.- RDF_
REST - The
rdf:restIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#rest. - RDF_SEQ
- The
rdf:SeqIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq. - RDF_
STATEMENT - The
rdf:StatementIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement. - RDF_
SUBJECT - The
rdf:subjectIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#subject. - RDF_
TRIPLE_ TERM rdf:TripleTerm— RDF 1.2 class of reified triple terms.- RDF_
TYPE - The
rdf:typeIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#type. - RDF_
VALUE - The
rdf:valueIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#value. - RDF_
XML_ LITERAL - The
rdf:XMLLiteralIRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral. - XSD_
STRING
Traits§
- From
Blank Id - Types that can be constructed from a blank node identifier.
- FromIri
- Types that can be constructed from an iri.
- From
Literal - Types that can be constructed from a literal.
- Generator
- Subject identifier generator.
- IsGraph
- Types that may appear in the graph-name position of an RDF quad.
- IsObject
- Types that may appear in the object position of an RDF triple/quad.
- IsPredicate
- Types that may appear in the predicate position of an RDF triple/quad.
- IsSubject
- Types that may appear in the subject position of an RDF triple/quad.
- Local
Generator - Maybe
Blank Id - Types that may represent a blank node identifier.
- Maybe
Iri - Types that may represent an iri.
- Maybe
Literal - Types that may represent a literal value.
- RdfDisplay
- Display method for RDF syntax elements.
- RdfDisplay
With Context - Resource
- Marker trait for opaque resource handles (e.g. interpretation indices).
- TryAs
Blank Id - Types that may have a blank node identifier representation that can be borrowed.
- TryAs
Iri - Types that may have an iri representation that can be borrowed.
- TryAs
Literal - Types that may have a literal representation that can be borrowed.
- TryExport
Quad - Type that can turn a
Quad<S, P, O, G>into aQuad. - TryInto
Blank Id - Types that can be turned into a blank node identifier.
- TryInto
Iri - Types that can be turned into an iri.
- TryInto
Literal - Types that can be turned into a literal.
Type Aliases§
- CowDatatype
- Owned-or-borrowed
Datatype. - Fully
Generalized Quad - Fully-generalized quad — body components are
GeneralizedLocalTerm. - Fully
Generalized Triple - Fully-generalized triple — body components are
GeneralizedLocalTerm. - IriBuf
- Lexical
Quad - Lexical RDF quad.
- Lexical
Quad Ref - Lexical RDF quad reference.
- Lexical
Triple - Lexical RDF triple.
- Lexical
Triple Ref - Lexical RDF triple reference.
- Triple
Term - RDF 1.2 triple term — a triple appearing as a term inside object position.
- Triple
Term Ref - Reference counterpart of
TripleTerm.