Skip to content

Commit

Permalink
Add tldr and tldr_include macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothee-haudebourg committed Dec 8, 2023
1 parent 7f58b9e commit 91cfa18
Show file tree
Hide file tree
Showing 13 changed files with 403 additions and 61 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
members = [
"layouts",
"generators/rust/treeldr-rs",
"generators/rust/treeldr-rs-derive",
"generators/rust/treeldr-rs-macros",
"generators/rust/generator"
]
resolver = "2"
Expand All @@ -14,7 +14,8 @@ authors = ["Spruce Systems Inc."]

[workspace.dependencies]
treeldr-layouts = { path = "layouts", version = "0.2.0" }
treeldr-derive = { path = "generators/rust/treeldr-rs-derive", version = "0.2.0" }
treeldr-macros = { path = "generators/rust/treeldr-rs-macros", version = "0.2.0" }
treeldr-gen-rust = { path = "generators/rust/generator", version = "0.2.0" }

log = "0.4"
educe = "0.4.23"
Expand Down
62 changes: 53 additions & 9 deletions generators/rust/generator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::{collections::HashMap, hash::Hash};
use std::{
collections::{BTreeMap, HashMap},
hash::Hash,
};

use grdf::BTreeDataset;
use proc_macro2::TokenStream;
use proc_macro2::{Span, TokenStream};
use quote::quote;
use rdf_types::{BlankIdBuf, Id, IriVocabulary, ReverseIriInterpretation};
use rdf_types::{BlankIdBuf, Id, IriVocabulary, ReverseIriInterpretation, Term};
use syn::spanned::Spanned;
use treeldr_layouts::{
distill::RdfContext,
layout::{DataLayout, LayoutType, ListLayout, LiteralLayout},
Expand All @@ -14,7 +18,7 @@ use utils::ident_from_iri;
pub mod utils;

#[derive(Debug, thiserror::Error)]
pub enum Error<R> {
pub enum Error<R = Term> {
#[error("missing type identifier for layout {0}")]
MissingTypeIdentifier(R),

Expand All @@ -28,17 +32,41 @@ pub enum Error<R> {
NoIriRepresentation,
}

pub struct Options<R> {
#[derive(Debug, thiserror::Error)]
#[error("invalid module path")]
pub struct InvalidModulePath(Span);

impl InvalidModulePath {
pub fn span(&self) -> Span {
self.0
}
}

pub struct Options<R = Term> {
idents: HashMap<Ref<LayoutType, R>, syn::Ident>,
extern_modules: BTreeMap<String, syn::Path>,
}

impl<R> Options<R> {
pub fn new() -> Self {
Self {
idents: HashMap::new(),
extern_modules: BTreeMap::new(),
}
}

pub fn use_module(&mut self, prefix: String, path: syn::Path) -> Result<(), InvalidModulePath> {
for segment in &path.segments {
if !matches!(&segment.arguments, syn::PathArguments::None) {
return Err(InvalidModulePath(path.span()));
}
}

self.extern_modules.insert(prefix, path);

Ok(())
}

pub fn layout_ident<V, I>(
&self,
rdf: RdfContext<V, I>,
Expand Down Expand Up @@ -67,6 +95,8 @@ impl<R> Options<R> {
R: Clone + Eq + Hash,
{
use treeldr_layouts::PresetLayout;
let mut module_path = None;

for i in rdf.interpretation.iris_of(layout_ref.id()) {
let iri = rdf.vocabulary.iri(i).unwrap();
if let Some(p) = PresetLayout::from_iri(iri) {
Expand All @@ -87,13 +117,27 @@ impl<R> Options<R> {

return Ok(syn::parse2(ty).unwrap());
}

for (prefix, path) in &self.extern_modules {
if iri.starts_with(prefix) {
module_path = Some(path.clone())
}
}
}

let ident = self.layout_ident(rdf, layout_ref)?;
Ok(syn::Type::Path(syn::TypePath {
qself: None,
path: syn::Path::from(ident),
}))

let mut path = module_path.unwrap_or_else(|| syn::Path {
leading_colon: None,
segments: syn::punctuated::Punctuated::new(),
});

path.segments.push(syn::PathSegment {
ident,
arguments: syn::PathArguments::None,
});

Ok(syn::Type::Path(syn::TypePath { qself: None, path }))
}
}

Expand Down
41 changes: 0 additions & 41 deletions generators/rust/treeldr-rs-derive/src/lib.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "treeldr-derive"
description = "TreeLDR derive macros"
name = "treeldr-macros"
description = "TreeLDR macros"
version.workspace = true
authors.workspace = true
edition.workspace = true
Expand All @@ -10,11 +10,13 @@ proc-macro = true

[dependencies]
treeldr-layouts.workspace = true
treeldr-gen-rust.workspace = true
thiserror.workspace = true
rdf-types.workspace = true
iref.workspace = true
static-iref.workspace = true
syn.workspace = true
proc-macro2.workspace = true
quote.workspace = true
serde_json.workspace = true
proc-macro-error = "1.0.4"
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ pub fn generate(input: DeriveInput) -> Result<TokenStream, Error> {
#(#deserialize_fields)*

Ok(Self {
#(#unwrap_fields)*
#(#unwrap_fields),*
})
}
}
Expand Down
Loading

0 comments on commit 91cfa18

Please sign in to comment.