Simple "Basic Post" Schema
This example shows how to create a Rust application with rebase that uses TreeLDR to define a simple self-signed basic blog post credential schema. The program will consist of:
a TreeLDR layout definition for
schema.org
'sBlogPosting
type,a Rust type definition automatically derived from this layout, and
an implementation of Rebase's
SchemaType
trait for this type written with the help of TreeLDR.
Project Creation
First, create a new Rust project using cargo:
The src
directory will contain the sources of our application. For now, it only contains a main.rs
file with a dummy main function definition.
TreeLDR Schema Definitions
In the src
directory, create a new basic_post.tldr
file containing the following BasicPost
layout definition for https://schema.org/BlogPosting
:
Instead of defining the schema:BlogPosting
type (and the schema:Text
type) ourselves in this file, we will rely on an example file provided in the TreeLDR repository that contains schema.org
type definitions. In the terminal, use the following command to download this file as src/schema.org.tldr
:
Import the Schema in Rust
We will embed the previous layout definition into the Rust program as a type definition using the #[tldr]
procedural macro attribute provided by the treeldr-rust-macros
crate. This crate relies on the treeldr-rust-prelude
crate. First, add those two crates to the dependencies of the Rust program by adding the following lines to the Cargo.toml
file under the [dependencies]
section:
Then add the following module definition annotated with the #[tldr]
procedural macro attribute to the src/main.rs
file:
The arguments to the #[tldr]
macro lists all the files we want to include in the Rust program in the schema
module. The submodules annotated with the #[prefix]
macro specify where to put the types. Here every layout prefixed by https://schema.org/
will be put inside the schema::org
module, while the layouts prefixed by https://example.com/
will be put inside the schema::example
module.
At compile time, this macro call will expand to the following module:
The schema::example::BasicPost
type corresponds to our blog post schema layout.
Rebase's SchemaType Trait Implementation
We now need to implement rebase's SchemaType
for BasicPost
. In the src/main.rs
file, add the following implementation:
The implementation is almost complete. The only missing piece is the context
method implementation that should return the JSON-LD context of the final verifiable credential (VC). This VC is a VerifiableBasicPost
as stated in the types
method, whose subject is the blog post itself as stated in the subject
method. We can generate this JSON-LD context using TreeLDR by first defining what the final VC type will be. Create a new src/vc.tldr
file with the following content:
We define the VerifiableBasicPost
as a vc:VerifiableCredential
where the credential subject is a schema:BlogPosting
. The with
keyword is used to specify that we will use the BasicPost
layout to represent a schema:BlogPosting
in the VC. Once again, we will use a TreeLDR example file to define the vc:VerifiableCredential
type. Use the following command to download the file as src/vc.tldr
:
We can now generate the JSON-LD context using tldrc
and the following command:
The https://www.w3.org/2018/credentials/v1
context defines the VerifiableCredential
type. The result can be used to define the context
method. Replace the todo!()
in the previous implementation with the following piece of code that includes the JSON-LD context generated thanks to TreeLDR.
Last updated