Linked data core
Core library for building Linked Data derive macros.
linked-data-core parses Rust structs and enums annotated with #[ld(...)] attributes into an
RDF-oriented model (RdfType, RdfStruct, RdfEnum, RdfField, RdfVariant). It resolves
prefixed names into full IRIs and validates the annotations with spanned compile errors. Code
generation is left to you: implement the TokenGenerator trait to turn that model into whatever
your derive macro needs to emit (serialization, deserialization, SPARQL queries, etc.).
Installation
This crate is meant to be used from a proc-macro crate:
[]
= true
[]
= "0.2"
= "3"
= "1"
= "1"
= "3"
Writing a derive macro
use ;
use TokenStream;
use proc_macro_error;
use quote;
use ;
;
RdfType::from_derive reports invalid annotations with proc_macro_error3::abort!, so the
derive entry point must be annotated with #[proc_macro_error].
Every model type implements quote::ToTokens by delegating to the matching TokenGenerator
method, so they can be interpolated directly in quote!.
Attributes
All annotations live in #[ld(...)] attributes. IRIs can be written in full
("http://schema.org/name") or as prefixed names ("schema:name") once a prefix is declared on
the type.
Structs
| Attribute | Description |
|---|---|
prefix("ex" = "http://ex.org/") |
Declares a prefix usable in this type's IRIs. |
type = "ex:Person" |
The rdf:type of the resource (at most once). |
Struct fields
| Attribute | Description |
|---|---|
"ex:name" |
The predicate IRI linking the resource to the field value. |
id |
The field holds the subject IRI of the resource. |
flatten |
The field's own properties are merged into the parent. |
graph |
The field holds a named graph. |
ignore |
The field is skipped. |
Enums
Enums accept prefix(...) like structs. An enum must contain either only single-field variants
or only unit variants; mixing both is rejected.
Tagged unions
Single-field (tuple) variants wrap a value. The IRI can be set on the variant or on its field:
RdfVariant::predicate_path returns the resulting PredicatePath:
-
An IRI on either the variant or its field gives a direct predicate (
PredicatePath::Predicate)::s ex:email :o . -
An IRI on both gives a path through an intermediate blank node (
PredicatePath::ChainedPath), whereto_blankis the field's IRI andfrom_blankthe variant's::s ex:address _:b . _:b ex:value :o .
Closed lists
When every variant is a unit variant, the enum is a closed list of RDF resources, such as a
controlled vocabulary. Each variant is identified by its own IRI, and RdfEnum::is_closed_list
returns true:
Errors
Invalid annotations are reported as compile errors pointing at the offending code, including:
- unions, and enum variants with more than one field;
- enums mixing unit and single-field variants;
- a variant without an IRI;
- more than one
typeon a struct, or more than one IRI on a field or variant; - malformed IRIs and invalid prefixes.
Development
CI runs on Rust 1.86.0.
License
Licensed under the MIT license.