Skip to content

Commit

Permalink
Add a prelude with common layout definitions.
Browse files Browse the repository at this point in the history
Fixes #177
  • Loading branch information
timothee-haudebourg committed Apr 4, 2024
1 parent 0be606a commit d98febc
Show file tree
Hide file tree
Showing 26 changed files with 313 additions and 50 deletions.
File renamed without changes.
5 changes: 5 additions & 0 deletions layouts/prelude/i16.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "https://treeldr.org/layouts#i16",
"type": "number",
"datatype": "http://www.w3.org/2001/XMLSchema#short"
}
5 changes: 5 additions & 0 deletions layouts/prelude/i32.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "https://treeldr.org/layouts#i32",
"type": "number",
"datatype": "http://www.w3.org/2001/XMLSchema#int"
}
5 changes: 5 additions & 0 deletions layouts/prelude/i64.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "https://treeldr.org/layouts#i64",
"type": "number",
"datatype": "http://www.w3.org/2001/XMLSchema#long"
}
5 changes: 5 additions & 0 deletions layouts/prelude/i8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "https://treeldr.org/layouts#i8",
"type": "number",
"datatype": "http://www.w3.org/2001/XMLSchema#byte"
}
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions layouts/prelude/u16.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "https://treeldr.org/layouts#u16",
"type": "number",
"datatype": "http://www.w3.org/2001/XMLSchema#unsignedShort"
}
5 changes: 5 additions & 0 deletions layouts/prelude/u32.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "https://treeldr.org/layouts#u32",
"type": "number",
"datatype": "http://www.w3.org/2001/XMLSchema#unsignedInt"
}
5 changes: 5 additions & 0 deletions layouts/prelude/u64.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "https://treeldr.org/layouts#u64",
"type": "number",
"datatype": "http://www.w3.org/2001/XMLSchema#unsignedLong"
}
5 changes: 5 additions & 0 deletions layouts/prelude/u8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "https://treeldr.org/layouts#u8",
"type": "number",
"datatype": "http://www.w3.org/2001/XMLSchema#unsignedByte"
}
File renamed without changes.
4 changes: 0 additions & 4 deletions layouts/preset/i16.json

This file was deleted.

4 changes: 0 additions & 4 deletions layouts/preset/i32.json

This file was deleted.

4 changes: 0 additions & 4 deletions layouts/preset/i64.json

This file was deleted.

4 changes: 0 additions & 4 deletions layouts/preset/i8.json

This file was deleted.

4 changes: 0 additions & 4 deletions layouts/preset/u16.json

This file was deleted.

4 changes: 0 additions & 4 deletions layouts/preset/u32.json

This file was deleted.

4 changes: 0 additions & 4 deletions layouts/preset/u64.json

This file was deleted.

4 changes: 0 additions & 4 deletions layouts/preset/u8.json

This file was deleted.

42 changes: 36 additions & 6 deletions layouts/src/distill/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use crate::{
layout::{DataLayout, LayoutType, ListLayout, LiteralLayout, ProductLayoutType},
Layout, Layouts, Literal, Pattern, Ref, Value, ValueFormat,
Layout, LayoutRegistry, Literal, Pattern, Ref, Value, ValueFormat,
};
use iref::IriBuf;
use rdf_types::{
Expand Down Expand Up @@ -43,7 +43,7 @@ pub enum Error<R = Term> {
#[error(transparent)]
TermAmbiguity(Box<TermAmbiguity>),

#[error("layout {0} not found")]
#[error("layout `{0}` is undefined")]
LayoutNotFound(Ref<LayoutType, R>),

#[error("missing required field `{field_name}`")]
Expand Down Expand Up @@ -267,7 +267,7 @@ impl<G> Options<G> {
/// resources to be collected during deserialization. The collected terms
/// are then returned along with the RDF dataset.
pub fn dehydrate<G: Generator>(
layouts: &Layouts,
layouts: impl LayoutRegistry,
value: &Value,
layout_ref: &Ref<LayoutType>,
mut options: Options<G>,
Expand Down Expand Up @@ -510,7 +510,37 @@ pub fn dehydrate<G: Generator>(
/// the deserialized RDF dataset.
pub fn dehydrate_with<V, I, Q, D>(
rdf: &mut RdfContextMut<V, I>,
layouts: &Layouts<Q>,
layouts: impl LayoutRegistry<Q>,
value: &Value,
current_graph: Option<&I::Resource>,
layout_ref: &Ref<LayoutType, Q>,
inputs: &[I::Resource],
output: &mut D,
) -> Result<(), Error<Q>>
where
V: VocabularyMut,
V::Iri: Clone,
I: InterpretationMut<V>
+ ReverseIriInterpretationMut<Iri = V::Iri>
+ ReverseLiteralInterpretationMut<Literal = V::Literal>,
I::Resource: Clone + Ord,
Q: Clone + Ord + Into<I::Resource>,
D: TraversableDataset<Resource = I::Resource> + DatasetMut,
{
dehydrate_with_ref(
rdf,
&layouts,
value,
current_graph,
layout_ref,
inputs,
output,
)
}

fn dehydrate_with_ref<V, I, Q, D>(
rdf: &mut RdfContextMut<V, I>,
layouts: &impl LayoutRegistry<Q>,
value: &Value,
current_graph: Option<&I::Resource>,
layout_ref: &Ref<LayoutType, Q>,
Expand Down Expand Up @@ -804,7 +834,7 @@ where

fn dehydrate_sub_value<V, I, Q, D>(
rdf: &mut RdfContextMut<V, I>,
layouts: &Layouts<Q>,
layouts: &impl LayoutRegistry<Q>,
value: &Value,
current_graph: Option<&I::Resource>,
format: &ValueFormat<Q>,
Expand All @@ -828,7 +858,7 @@ where
None => current_graph.cloned(),
};

dehydrate_with(
dehydrate_with_ref(
rdf,
layouts,
value,
Expand Down
45 changes: 36 additions & 9 deletions layouts/src/distill/hy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
matching,
pattern::Substitution,
utils::QuadsExt,
Layout, Layouts, Matching, Pattern, Ref, TypedLiteral, TypedValue, Value,
Layout, LayoutRegistry, Matching, Pattern, Ref, TypedLiteral, TypedValue, Value,
};
use iref::IriBuf;
use rdf_types::{
Expand Down Expand Up @@ -40,7 +40,7 @@ pub enum Error<R = Term> {
#[error("no matching literal representation found")]
NoMatchingLiteral,

#[error("layout `{0}` not found")]
#[error("layout `{0}` is undefined")]
LayoutNotFound(Ref<LayoutType, R>),
}

Expand Down Expand Up @@ -114,7 +114,7 @@ impl<T, R> MatchingForFragment<R> for Result<T, matching::Error> {
/// This function a tree value annotated (typed) with references to the
/// different layouts used to serialize each part of the tree.
pub fn hydrate<D>(
context: &Layouts,
context: impl LayoutRegistry,
dataset: &D,
layout_ref: &Ref<LayoutType>,
inputs: &[Term],
Expand All @@ -130,7 +130,34 @@ where
pub fn hydrate_with<V, I: Interpretation, D>(
vocabulary: &V,
interpretation: &I,
context: &Layouts<I::Resource>,
context: impl LayoutRegistry<I::Resource>,
dataset: &D,
current_graph: Option<&I::Resource>,
layout_ref: &Ref<LayoutType, I::Resource>,
inputs: &[I::Resource],
) -> Result<TypedValue<I::Resource>, Error<I::Resource>>
where
V: Vocabulary,
V::Iri: PartialEq,
I: ReverseIriInterpretation<Iri = V::Iri> + ReverseLiteralInterpretation<Literal = V::Literal>,
I::Resource: Clone + Ord,
D: PatternMatchingDataset<Resource = I::Resource>,
{
hydrate_with_ref(
vocabulary,
interpretation,
&context,
dataset,
current_graph,
layout_ref,
inputs,
)
}

fn hydrate_with_ref<V, I: Interpretation, D>(
vocabulary: &V,
interpretation: &I,
context: &impl LayoutRegistry<I::Resource>,
dataset: &D,
current_graph: Option<&I::Resource>,
layout_ref: &Ref<LayoutType, I::Resource>,
Expand Down Expand Up @@ -252,7 +279,7 @@ where
&variant_substitution,
);

let value = hydrate_with(
let value = hydrate_with_ref(
vocabulary,
interpretation,
context,
Expand Down Expand Up @@ -325,7 +352,7 @@ where
let item_graph =
select_graph(current_graph, &field.value.graph, &field_substitution);

let value = hydrate_with(
let value = hydrate_with_ref(
vocabulary,
interpretation,
context,
Expand Down Expand Up @@ -387,7 +414,7 @@ where
&item_substitution,
);

let item = hydrate_with(
let item = hydrate_with_ref(
vocabulary,
interpretation,
context,
Expand Down Expand Up @@ -451,7 +478,7 @@ where
&item_substitution,
);

let item = hydrate_with(
let item = hydrate_with_ref(
vocabulary,
interpretation,
context,
Expand Down Expand Up @@ -501,7 +528,7 @@ where
let item_graph =
select_graph(current_graph, &item.value.graph, &item_substitution);

let item = hydrate_with(
let item = hydrate_with_ref(
vocabulary,
interpretation,
context,
Expand Down
2 changes: 1 addition & 1 deletion layouts/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<R: Ord> DerefResource<LayoutType, R> for Layouts<R> {
Hash(bound = "R: Ord + Hash")
)]
#[serde(bound(deserialize = "R: Clone + Ord + serde::Deserialize<'de>"))]
pub enum Layout<R> {
pub enum Layout<R = Term> {
/// Bottom layout.
///
/// This layout does not match any tree value or RDF dataset.
Expand Down
Loading

0 comments on commit d98febc

Please sign in to comment.