Skip to content

Commit

Permalink
Use common id generator when building multiple modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothee-haudebourg committed Nov 18, 2024
1 parent ec47d61 commit f243bb7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
10 changes: 10 additions & 0 deletions layouts/tests/distill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,13 @@ negative_test! {
/// Missing required field.
e01 (Term::blank(BlankIdBuf::new("_:john_smith".to_string()).unwrap()))
}

#[test]
fn dehydrate_t16() {
dehydrate(
"t16",
[Term::blank(
BlankIdBuf::new("_:subject".to_string()).unwrap(),
)],
)
}
7 changes: 7 additions & 0 deletions layouts/tests/distill/t16-in.nq
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
_:subject <https://schema.org/name> "John Smith" .
_:subject <https://schema.org/email> "[email protected]" .
_:subject <https://purl.imsglobal.org/spec/vc/ob/vocab.html#achievement> <http://example.com/#projectExecution> .
<http://example.com/#projectExecution> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://purl.imsglobal.org/spec/vc/ob/vocab.html#Achievement> .
<http://example.com/#projectExecution> <https://schema.org/name> "Project execution"@en .
<http://example.com/#projectExecution> <https://purl.imsglobal.org/spec/vc/ob/vocab.html#Criteria> <http://example.com/#projectExecutionCriteria> .
<http://example.com/#projectExecutionCriteria> <https://purl.imsglobal.org/spec/vc/ob/vocab.html#narrative> "Contribute to solution design and implementation."@en .
35 changes: 35 additions & 0 deletions layouts/tests/distill/t16-layout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"type": "record",
"fields": {
"name": { "property": "https://schema.org/name", "value": { "type": "string" } },
"email": { "property": "https://schema.org/email", "value": { "type": "string" } },
"achievement": {
"intro": [],
"value": {
"input": ["_:self"],
"layout": {
"type": "sum",
"variants": {
"projectExecution": {
"intro": [],
"value": {
"input": ["_:self"],
"layout": {
"type": "unit",
"const": "projectExecution",
"dataset": [
["_:self", "https://purl.imsglobal.org/spec/vc/ob/vocab.html#achievement", "http://example.com/#projectExecution"],
["http://example.com/#projectExecution", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "https://purl.imsglobal.org/spec/vc/ob/vocab.html#Achievement"],
["http://example.com/#projectExecution", "https://schema.org/name", { "value": "Project execution", "language": "en" }],
["http://example.com/#projectExecution", "https://purl.imsglobal.org/spec/vc/ob/vocab.html#Criteria", "http://example.com/#projectExecutionCriteria"],
["http://example.com/#projectExecutionCriteria", "https://purl.imsglobal.org/spec/vc/ob/vocab.html#narrative", { "value": "Contribute to solution design and implementation.", "language": "en" }]
]
}
}
}
}
}
}
}
}
}
5 changes: 5 additions & 0 deletions layouts/tests/distill/t16-out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "John Smith",
"email": "[email protected]",
"achievement": "projectExecution"
}
12 changes: 8 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use codespan_reporting::{
termcolor::{ColorChoice, StandardStream},
},
};
use rdf_types::Term;
use rdf_types::{generator, Generator, Term};
use std::{
fs,
io::{self, BufReader},
Expand Down Expand Up @@ -190,19 +190,20 @@ impl DefaultLayoutRef {

fn run(files: &mut SimpleFiles<String, String>, args: Args) -> Result<(), Error> {
let mut layouts = Layouts::new();
let mut generator = generator::Blank::new();
let prelude = args.prelude();
let mut default_layout = DefaultLayoutRef::None;
for filename in args.layouts {
let content = fs::read_to_string(&filename).map_err(Error::IO)?;
let file_id = files.add(filename.to_string_lossy().into_owned(), content);
let layout_ref = load_layout(files, file_id, &mut layouts)?;
let layout_ref = load_layout(files, file_id, &mut layouts, &mut generator)?;
default_layout.set(layout_ref);
}

for filename in args.include {
let content = fs::read_to_string(&filename).map_err(Error::IO)?;
let file_id = files.add(filename.to_string_lossy().into_owned(), content);
load_layout(files, file_id, &mut layouts)?;
load_layout(files, file_id, &mut layouts, &mut generator)?;
}

let layouts = layouts.with(prelude);
Expand Down Expand Up @@ -357,14 +358,17 @@ fn load_layout(
files: &SimpleFiles<String, String>,
file_id: usize,
layouts: &mut Layouts,
generator: &mut impl Generator,
) -> Result<Ref<LayoutType>, Error> {
use json_syntax::{Parse, TryFromJson};

let mut builder = treeldr_layouts::abs::Builder::new();
let mut context = builder.with_generator_mut(generator);

match json_syntax::Value::parse_str(files.get(file_id).unwrap().source().as_str()) {
Ok((json, code_map)) => {
match treeldr_layouts::abs::syntax::Layout::try_from_json(&json, &code_map) {
Ok(layout) => match layout.build(&mut builder) {
Ok(layout) => match layout.build_with_context(&mut context) {
Ok(layout_ref) => {
let new_layouts = builder.build();

Expand Down

0 comments on commit f243bb7

Please sign in to comment.