Compiling to Rust
TreeLDR can generate a Rust type definition for each defined layout. For instance, consider the following layout for the schema:BlogPosting
type:
TreeLDR can generate the following Rust type:
Non required
properties are automatically translated into fields with an Option
type.
Embedding
We can embed TreeLDR-generated code inside a Rust program using the tldr
procedural macro attribute provided by the treeldr-rust-macros
crate. This attribute allows us to annotate a module that will host all the TreeLDR-generated definitions. It requires two inputs:
The list of all the TreeLDR files we need to import, given as parameters of the
tldr
attribute; andThe destination (path in the module) of each type definition. This is done by declaring a submodule for each IRI prefix of interest. The type definition of each layout will be generated in the submodule whose prefix matches the identifier of the layout.
This will expand into the following code:
Traits Implementations
In addition to the type definitions, TreeLDR will also provide some traits implementations for those types.
Core Traits
Each type will implement Clone
, PartialEq
, Eq
, PartialOrd
, Ord
and Hash
.
FromRdf
Each type will implement the FromRdf
trait provided by the treeldr_rust_prelude
crate. This trait provides the from_rdf
constructor function that can extract an instance of the given type from an RDF dataset defined with the grdf
crate.
IntoJsonLd
Each type will implement the IntoJsonLd
trait provided by the treeldr_rust_prelude
crate. This trait provides the into_json_ld
method that converts the value into a json_ld::syntax::Value
.
Last updated