Skip to content

Commit

Permalink
Detect unexpected (unused) layout entries.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothee-haudebourg committed Apr 2, 2024
1 parent 7a37d08 commit 16ff563
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 142 deletions.
24 changes: 19 additions & 5 deletions layouts/src/abs/syntax/layout/intersection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use json_syntax::TryFromJsonObject;
use serde::{Deserialize, Serialize};

use crate::{
abs::syntax::{check_type, Build, BuildError, Context, Error, Scope},
abs::syntax::{check_type, Build, BuildError, Context, Error, ObjectUnusedEntries, Scope},
layout::LayoutType,
Ref,
};
Expand All @@ -27,11 +27,25 @@ impl TryFromJsonObject for IntersectionLayout {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Self::Error> {
check_type(object, IntersectionLayoutType::NAME, code_map, offset)?;
Ok(Self {
let mut unused_entries = ObjectUnusedEntries::new(object, code_map, offset);
check_type(
object,
IntersectionLayoutType::NAME,
&mut unused_entries,
code_map,
offset,
)?;
let result = Self {
type_: IntersectionLayoutType,
header: LayoutHeader::try_from_json_object_at(object, code_map, offset)?,
})
header: LayoutHeader::try_from_json_object_at(
object,
&mut unused_entries,
code_map,
offset,
)?,
};
unused_entries.check()?;
Ok(result)
}
}

Expand Down
120 changes: 87 additions & 33 deletions layouts/src/abs/syntax/layout/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::abs::{
self,
syntax::{
check_type, expect_object, get_entry, require_entry, require_type, Build, BuildError,
CompactIri, Context, Dataset, Error, ExpectedType, Pattern, Scope, ValueFormatOrLayout,
ValueIntro,
CompactIri, Context, Dataset, Error, ExpectedType, ObjectUnusedEntries, Pattern, Scope,
ValueFormatOrLayout, ValueIntro,
},
};

Expand Down Expand Up @@ -41,7 +41,7 @@ impl TryFromJsonObject for ListLayout {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Self::Error> {
let ty = require_type(object, code_map, offset)?;
let ty = require_type(object, None, code_map, offset)?;
match ty.value {
OrderedListLayoutType::NAME => {
OrderedListLayout::try_from_json_object_at(object, code_map, offset)
Expand Down Expand Up @@ -118,16 +118,30 @@ impl TryFromJsonObject for OrderedListLayout {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Self::Error> {
check_type(object, OrderedListLayoutType::NAME, code_map, offset)?;
Ok(Self {
let mut unused_entries = ObjectUnusedEntries::new(object, code_map, offset);
check_type(
object,
OrderedListLayoutType::NAME,
&mut unused_entries,
code_map,
offset,
)?;
let result = Self {
type_: OrderedListLayoutType,
header: LayoutHeader::try_from_json_object_at(object, code_map, offset)?,
node: require_entry(object, "node", code_map, offset)?,
head: get_entry(object, "head", code_map, offset)?
header: LayoutHeader::try_from_json_object_at(
object,
&mut unused_entries,
code_map,
offset,
)?,
node: require_entry(object, "node", &mut unused_entries, code_map, offset)?,
head: get_entry(object, "head", &mut unused_entries, code_map, offset)?
.unwrap_or_else(Pattern::default_head),
tail: get_entry(object, "tail", code_map, offset)?
tail: get_entry(object, "tail", &mut unused_entries, code_map, offset)?
.unwrap_or_else(Pattern::default_tail),
})
};
unused_entries.check()?;
Ok(result)
}
}

Expand Down Expand Up @@ -271,13 +285,19 @@ impl TryFromJsonObject for ListNode {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Self::Error> {
Ok(Self {
head: get_entry(object, "head", code_map, offset)?.unwrap_or_else(Self::default_head),
rest: get_entry(object, "rest", code_map, offset)?.unwrap_or_else(Self::default_rest),
intro: get_entry(object, "intro", code_map, offset)?.unwrap_or_default(),
value: require_entry(object, "value", code_map, offset)?,
dataset: get_entry(object, "dataset", code_map, offset)?,
})
let mut unused_entries = ObjectUnusedEntries::new(object, code_map, offset);
let result = Self {
head: get_entry(object, "head", &mut unused_entries, code_map, offset)?
.unwrap_or_else(Self::default_head),
rest: get_entry(object, "rest", &mut unused_entries, code_map, offset)?
.unwrap_or_else(Self::default_rest),
intro: get_entry(object, "intro", &mut unused_entries, code_map, offset)?
.unwrap_or_default(),
value: require_entry(object, "value", &mut unused_entries, code_map, offset)?,
dataset: get_entry(object, "dataset", &mut unused_entries, code_map, offset)?,
};
unused_entries.check()?;
Ok(result)
}
}

Expand Down Expand Up @@ -359,12 +379,26 @@ impl TryFromJsonObject for UnorderedListLayout {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Self::Error> {
check_type(object, UnorderedListLayoutType::NAME, code_map, offset)?;
Ok(Self {
let mut unused_entries = ObjectUnusedEntries::new(object, code_map, offset);
check_type(
object,
UnorderedListLayoutType::NAME,
&mut unused_entries,
code_map,
offset,
)?;
let result = Self {
type_: UnorderedListLayoutType,
header: LayoutHeader::try_from_json_object_at(object, code_map, offset)?,
item: require_entry(object, "item", code_map, offset)?,
})
header: LayoutHeader::try_from_json_object_at(
object,
&mut unused_entries,
code_map,
offset,
)?,
item: require_entry(object, "item", &mut unused_entries, code_map, offset)?,
};
unused_entries.check()?;
Ok(result)
}
}

Expand Down Expand Up @@ -404,12 +438,17 @@ impl TryFromJsonObject for ListItem {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Self::Error> {
Ok(Self {
intro: get_entry(object, "intro", code_map, offset)?.unwrap_or_default(),
value: require_entry(object, "value", code_map, offset)?,
dataset: get_entry(object, "dataset", code_map, offset)?.unwrap_or_default(),
property: get_entry(object, "property", code_map, offset)?,
})
let mut unused_entries = ObjectUnusedEntries::new(object, code_map, offset);
let result = Self {
intro: get_entry(object, "intro", &mut unused_entries, code_map, offset)?
.unwrap_or_default(),
value: require_entry(object, "value", &mut unused_entries, code_map, offset)?,
dataset: get_entry(object, "dataset", &mut unused_entries, code_map, offset)?
.unwrap_or_default(),
property: get_entry(object, "property", &mut unused_entries, code_map, offset)?,
};
unused_entries.check()?;
Ok(result)
}
}

Expand Down Expand Up @@ -504,12 +543,27 @@ impl TryFromJsonObject for SizedListLayout {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Self::Error> {
check_type(object, SizedListLayoutType::NAME, code_map, offset)?;
Ok(Self {
let mut unused_entries = ObjectUnusedEntries::new(object, code_map, offset);
check_type(
object,
SizedListLayoutType::NAME,
&mut unused_entries,
code_map,
offset,
)?;
let result = Self {
type_: SizedListLayoutType,
header: LayoutHeader::try_from_json_object_at(object, code_map, offset)?,
items: get_entry(object, "items", code_map, offset)?.unwrap_or_default(),
})
header: LayoutHeader::try_from_json_object_at(
object,
&mut unused_entries,
code_map,
offset,
)?,
items: get_entry(object, "items", &mut unused_entries, code_map, offset)?
.unwrap_or_default(),
};
unused_entries.check()?;
Ok(result)
}
}

Expand Down
115 changes: 82 additions & 33 deletions layouts/src/abs/syntax/layout/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
self,
syntax::{
check_type, expect_object, get_entry, require_entry, require_type, Build, BuildError,
CompactIri, Context, Error, ExpectedType, Pattern, Scope,
CompactIri, Context, Error, ExpectedType, ObjectUnusedEntries, Pattern, Scope,
},
RegExp,
},
Expand Down Expand Up @@ -42,7 +42,7 @@ impl TryFromJsonObject for LiteralLayout {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Self::Error> {
let ty = require_type(object, code_map, offset)?;
let ty = require_type(object, None, code_map, offset)?;
match ty.value {
IdLayoutType::NAME => {
IdLayout::try_from_json_object_at(object, code_map, offset).map(Self::Id)
Expand Down Expand Up @@ -127,7 +127,7 @@ impl TryFromJsonObject for DataLayout {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Error> {
let ty = require_type(object, code_map, offset)?;
let ty = require_type(object, None, code_map, offset)?;
match ty.value {
UnitLayoutType::NAME => {
UnitLayout::try_from_json_object_at(object, code_map, offset).map(Self::Unit)
Expand Down Expand Up @@ -205,10 +205,19 @@ impl TryFromJsonObject for UnitLayout {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Error> {
check_type(object, UnitLayoutType::NAME, code_map, offset)?;
let header = LayoutHeader::try_from_json_object_at(object, code_map, offset)?;
let const_ = get_entry(object, "const", code_map, offset)?.unwrap_or_default();

let mut unused_entries = ObjectUnusedEntries::new(object, code_map, offset);
check_type(
object,
UnitLayoutType::NAME,
&mut unused_entries,
code_map,
offset,
)?;
let header =
LayoutHeader::try_from_json_object_at(object, &mut unused_entries, code_map, offset)?;
let const_ =
get_entry(object, "const", &mut unused_entries, code_map, offset)?.unwrap_or_default();
unused_entries.check()?;
Ok(Self {
type_: UnitLayoutType,
header,
Expand Down Expand Up @@ -306,11 +315,19 @@ impl TryFromJsonObject for BooleanLayout {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Error> {
check_type(object, BooleanLayoutType::NAME, code_map, offset)?;
let header = LayoutHeader::try_from_json_object_at(object, code_map, offset)?;
let resource = get_entry(object, "resource", code_map, offset)?;
let datatype = get_entry(object, "datatype", code_map, offset)?;

let mut unused_entries = ObjectUnusedEntries::new(object, code_map, offset);
check_type(
object,
BooleanLayoutType::NAME,
&mut unused_entries,
code_map,
offset,
)?;
let header =
LayoutHeader::try_from_json_object_at(object, &mut unused_entries, code_map, offset)?;
let resource = get_entry(object, "resource", &mut unused_entries, code_map, offset)?;
let datatype = get_entry(object, "datatype", &mut unused_entries, code_map, offset)?;
unused_entries.check()?;
Ok(Self {
type_: BooleanLayoutType,
header,
Expand Down Expand Up @@ -367,11 +384,19 @@ impl TryFromJsonObject for NumberLayout {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Error> {
check_type(object, NumberLayoutType::NAME, code_map, offset)?;
let header = LayoutHeader::try_from_json_object_at(object, code_map, offset)?;
let resource = get_entry(object, "resource", code_map, offset)?;
let datatype = require_entry(object, "datatype", code_map, offset)?;

let mut unused_entries = ObjectUnusedEntries::new(object, code_map, offset);
check_type(
object,
NumberLayoutType::NAME,
&mut unused_entries,
code_map,
offset,
)?;
let header =
LayoutHeader::try_from_json_object_at(object, &mut unused_entries, code_map, offset)?;
let resource = get_entry(object, "resource", &mut unused_entries, code_map, offset)?;
let datatype = require_entry(object, "datatype", &mut unused_entries, code_map, offset)?;
unused_entries.check()?;
Ok(Self {
type_: NumberLayoutType,
header,
Expand Down Expand Up @@ -428,11 +453,19 @@ impl TryFromJsonObject for ByteStringLayout {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Error> {
check_type(object, ByteStringLayoutType::NAME, code_map, offset)?;
let header = LayoutHeader::try_from_json_object_at(object, code_map, offset)?;
let resource = get_entry(object, "resource", code_map, offset)?;
let datatype = require_entry(object, "datatype", code_map, offset)?;

let mut unused_entries = ObjectUnusedEntries::new(object, code_map, offset);
check_type(
object,
ByteStringLayoutType::NAME,
&mut unused_entries,
code_map,
offset,
)?;
let header =
LayoutHeader::try_from_json_object_at(object, &mut unused_entries, code_map, offset)?;
let resource = get_entry(object, "resource", &mut unused_entries, code_map, offset)?;
let datatype = require_entry(object, "datatype", &mut unused_entries, code_map, offset)?;
unused_entries.check()?;
Ok(Self {
type_: ByteStringLayoutType,
header,
Expand Down Expand Up @@ -468,12 +501,20 @@ impl TryFromJsonObject for TextStringLayout {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Error> {
check_type(object, TextStringLayoutType::NAME, code_map, offset)?;
let header = LayoutHeader::try_from_json_object_at(object, code_map, offset)?;
let pattern = get_entry(object, "pattern", code_map, offset)?;
let resource = get_entry(object, "resource", code_map, offset)?;
let datatype = get_entry(object, "datatype", code_map, offset)?;

let mut unused_entries = ObjectUnusedEntries::new(object, code_map, offset);
check_type(
object,
TextStringLayoutType::NAME,
&mut unused_entries,
code_map,
offset,
)?;
let header =
LayoutHeader::try_from_json_object_at(object, &mut unused_entries, code_map, offset)?;
let pattern = get_entry(object, "pattern", &mut unused_entries, code_map, offset)?;
let resource = get_entry(object, "resource", &mut unused_entries, code_map, offset)?;
let datatype = get_entry(object, "datatype", &mut unused_entries, code_map, offset)?;
unused_entries.check()?;
Ok(Self {
type_: TextStringLayoutType,
header,
Expand Down Expand Up @@ -538,11 +579,19 @@ impl TryFromJsonObject for IdLayout {
code_map: &json_syntax::CodeMap,
offset: usize,
) -> Result<Self, Error> {
check_type(object, IdLayoutType::NAME, code_map, offset)?;
let header = LayoutHeader::try_from_json_object_at(object, code_map, offset)?;
let pattern = get_entry(object, "pattern", code_map, offset)?;
let resource = get_entry(object, "resource", code_map, offset)?;

let mut unused_entries = ObjectUnusedEntries::new(object, code_map, offset);
check_type(
object,
IdLayoutType::NAME,
&mut unused_entries,
code_map,
offset,
)?;
let header =
LayoutHeader::try_from_json_object_at(object, &mut unused_entries, code_map, offset)?;
let pattern = get_entry(object, "pattern", &mut unused_entries, code_map, offset)?;
let resource = get_entry(object, "resource", &mut unused_entries, code_map, offset)?;
unused_entries.check()?;
Ok(Self {
type_: IdLayoutType,
header,
Expand Down
Loading

0 comments on commit 16ff563

Please sign in to comment.