-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `SerializeLd` and `DeserializeLd` derive macros. * Add preset layouts. * Add `tldr` and `tldr_include` macros.
- Loading branch information
1 parent
5182482
commit 3d4e745
Showing
44 changed files
with
4,696 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "treeldr-gen-rust" | ||
description = "TreeLDR Layouts to Rust" | ||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
|
||
[dependencies] | ||
treeldr-layouts.workspace = true | ||
rdf-types.workspace = true | ||
iref.workspace = true | ||
grdf.workspace = true | ||
syn.workspace = true | ||
proc-macro2.workspace = true | ||
quote.workspace = true | ||
log.workspace = true | ||
thiserror.workspace = true | ||
|
||
clap = { workspace = true, features = ["derive"] } | ||
stderrlog.workspace = true | ||
serde_json.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use iref::IriBuf; | ||
use rdf_types::Term; | ||
use std::{fs, path::PathBuf, process::ExitCode}; | ||
use treeldr_layouts::{abs, distill::RdfContext, layout::LayoutType, Ref}; | ||
|
||
#[derive(clap::Parser)] | ||
#[clap(name="treeldr", author, version, about, long_about = None)] | ||
struct Args { | ||
/// Input files. | ||
filenames: Vec<PathBuf>, | ||
|
||
/// Layout to generate. | ||
#[clap(long, short)] | ||
layout: Option<IriBuf>, | ||
|
||
/// Sets the level of verbosity. | ||
#[clap(short, long = "verbose", action = clap::ArgAction::Count)] | ||
verbosity: u8, | ||
} | ||
|
||
enum DefaultLayoutRef { | ||
Unknown, | ||
Some(Ref<LayoutType>), | ||
None, | ||
} | ||
|
||
impl DefaultLayoutRef { | ||
pub fn set(&mut self, layout_ref: Ref<LayoutType>) { | ||
match self { | ||
Self::Unknown => *self = Self::Some(layout_ref), | ||
Self::Some(_) => *self = Self::None, | ||
Self::None => (), | ||
} | ||
} | ||
} | ||
|
||
fn main() -> ExitCode { | ||
// Parse options. | ||
let args: Args = clap::Parser::parse(); | ||
|
||
// Initialize logger. | ||
stderrlog::new() | ||
.verbosity(args.verbosity as usize) | ||
.init() | ||
.unwrap(); | ||
|
||
// Initialize the layout builder. | ||
let mut builder = abs::Builder::new(); | ||
|
||
let mut default_layout_ref = DefaultLayoutRef::Unknown; | ||
|
||
for filename in args.filenames { | ||
let content = fs::read_to_string(filename).unwrap(); | ||
|
||
match serde_json::from_str::<abs::syntax::Layout>(&content) { | ||
Ok(abstract_layout) => match abstract_layout.build(&mut builder) { | ||
Ok(layout_ref) => default_layout_ref.set(layout_ref), | ||
Err(e) => { | ||
log::error!("compile error: {e}"); | ||
return ExitCode::FAILURE; | ||
} | ||
}, | ||
Err(e) => { | ||
log::error!("parse error: {e}") | ||
} | ||
} | ||
} | ||
|
||
let layouts = builder.build(); | ||
|
||
let layout_ref = match args.layout { | ||
Some(iri) => { | ||
let term = Term::iri(iri); | ||
if layouts.layout(&term).is_some() { | ||
Ref::new(term) | ||
} else { | ||
log::error!("unknown layout {term}"); | ||
return ExitCode::FAILURE; | ||
} | ||
} | ||
None => match default_layout_ref { | ||
DefaultLayoutRef::Some(layout_ref) => layout_ref, | ||
_ => { | ||
log::error!("missing layout"); | ||
return ExitCode::FAILURE; | ||
} | ||
}, | ||
}; | ||
|
||
let gen_options = treeldr_gen_rust::Options::new(); | ||
|
||
let result = | ||
treeldr_gen_rust::generate(RdfContext::default(), &layouts, &layout_ref, &gen_options); | ||
|
||
match result { | ||
Ok(r) => { | ||
println!("{r}"); | ||
ExitCode::SUCCESS | ||
} | ||
Err(e) => { | ||
log::error!("parse error: {e}"); | ||
ExitCode::FAILURE | ||
} | ||
} | ||
} |
Oops, something went wrong.