SpruceKit
SpruceID
  • 🌲SpruceKit Introduction
    • Decentralized Identity Overview
    • Glossary
  • ⚡Quickstart
  • SpruceKit Mobile
    • SpruceKit Showcase App
      • Installation
      • Getting Started
      • Issue a Showcase Credential
      • Present a Showcase Credential
    • SpruceKit Mobile SDK
      • Introduction
      • Core Components
        • StorageManager
        • KeyManager
        • CredentialPack
        • Card
        • IsoMdlPresentation
        • mDocReader/IsomDLReader
        • Document Scanner
      • SpruceKit iOS SDK
        • Installation
        • Build a Wallet
          • Accept a W3C VC
          • Present a W3C VC
          • Present mDL in-person/offline
          • Present an mDL over the internet
        • Build a Verifier
          • Verify a W3C VC
          • Verify an mDL in-person/offline
          • Verify an mDL over the internet
      • SpruceKit Android SDK
        • Installation
        • Build a Wallet
          • Accept a W3C VC
          • Present a W3C VC
          • Present an mDL in-person/offline
          • Present an mDL over the internet
        • Build a Verifier
          • Verify a W3C VC
          • Verify an mDL in-person/offline
          • Verify an mDL over the internet
  • Verifiable Digital Credentials
    • ⚙️DIDKit
      • Installation
      • Core Concepts
      • DID Methods
      • Runtime Configuration
      • Specifications and Dependencies
      • Quickstart
      • DIDKit Packages
        • Command Line Interface
        • HTTP Server
        • Rust Crate
        • C Interface
        • Java and Android
        • Python
        • Javascript
      • DIDKit Examples
        • Core Functions (CLI)
        • Core Functions (HTTP)
        • did-web in minutes
        • Batch Generation & Verification
    • 🪪ISO mDL
      • Quickstart
      • Core Concepts
      • User Guide
  • Schema Definition Language
    • 🔗TreeLDR
      • TreeLDR Quickstart
        • First Schema
        • Compilation into JSON Schema
        • Compilation into JSON-LD Context
        • Writing a Layout
        • Simple Rust Integration
      • Types
        • Literal Types
      • Layouts
        • Primitive Layouts
        • Literal Layouts
        • Enumeration
        • Array Layout
        • References
      • Compiling
        • Schema Definition Formats
          • JSON Schema
          • JSON-LD Context
          • Resource Description Framework (RDF)
        • Programming Languages
          • Compiling to Rust
      • RDF Vocabulary
      • 💡TreeLDR Basics
        • Types and Layouts
        • Properties
        • Syntax
  • Witness for Credential Claims
    • 🔭Rebase
      • Core Library
      • Rust Client/Witness SDK
      • WASM Client SDK
      • Simple "Basic Post" Schema
      • DNS Witness Flow Schema
  • References
    • Contributing
    • Code of Conduct
Powered by GitBook
On this page
  • Embedding
  • Traits Implementations

Was this helpful?

  1. Schema Definition Language
  2. TreeLDR
  3. Compiling
  4. Programming Languages

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:

// example/basic_post.tldr
base <https://example.com/>;
use <https://schema.org/> as schema;

layout BasicPost for schema:BlogPosting {
	/// Title of the post.
	schema:title: schema:Text,

	/// Content of the post.
	schema:body: schema:Text
}

TreeLDR can generate the following Rust type:

pub struct BasicPost {
	/// Title of the post.
	title: Option<schema::Text>,

	/// Content of the post.
	body: Option<schema:Text>
}

Non required properties are automatically translated into fields with an Option type.

Embedding

  • The list of all the TreeLDR files we need to import, given as parameters of the tldr attribute; and

  • The 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.

#[tldr(
	"examples/schema.org.tldr",
	"examples/basic_post.tldr"
)]
pub mod example {
	/// schema.org types.
	#[prefix("https://schema.org/")]
	pub mod schema {}

	/// Basic Post example types.
	#[prefix("https://example.com/example/")]
	pub mod basic_post {}
}

This will expand into the following code:

pub mod example {
	pub mod schema {
		pub type Text = String;
	}

	pub mod basic_post {
		pub struct BasicPost {
			/// Title of the post.
			title: Option<super::schema::Text>,

			/// Content of the post.
			body: Option<super::schema:Text>
		}
	}
}

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

use treeldr_rust_prelude::FromRdf;

let post = example::basic_post::BasicPost::from_rdf(
	id,
	dataset.default_graph(),
).expect("invalid post");

IntoJsonLd

use treeldr_rust_prelude::IntoJsonLd;

// Schema to JSON-LD.
let json_ld: json_ld::syntax::Value<json_ld::syntax::ContextEntry<()>, _> =
	vc.into_json_ld();
PreviousProgramming LanguagesNextRDF Vocabulary

Last updated 1 year ago

Was this helpful?

We can embed TreeLDR-generated code inside a Rust program using the tldr procedural macro attribute provided by the . This attribute allows us to annotate a module that will host all the TreeLDR-generated definitions. It requires two inputs:

Each type will implement the FromRdf trait provided by the . 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.

Each type will implement the IntoJsonLd trait provided by the . This trait provides the into_json_ld method that converts the value into a json_ld::syntax::Value.

🔗
treeldr-rust-macros crate
treeldr_rust_prelude crate
treeldr_rust_prelude crate