Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oneOf types and value enumerations #86

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,14 @@ impl<M: Clone> Context<M> {
pub fn require_layout_field_id(
&self,
id: Id,
) -> Result<treeldr::TId<treeldr::layout::Field>, RequireError<M>> {
) -> Result<treeldr::TId<treeldr::layout::structure::Field>, RequireError<M>> {
Ok(self.require(id)?.require_layout_field_id(self)?)
}

pub fn require_layout_variant_id(
&self,
id: Id,
) -> Result<treeldr::TId<treeldr::layout::Variant>, RequireError<M>> {
) -> Result<treeldr::TId<treeldr::layout::enumeration::Variant>, RequireError<M>> {
Ok(self.require(id)?.require_layout_variant_id(self)?)
}

Expand Down
6 changes: 3 additions & 3 deletions build/src/layout/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
use super::Error;
use locspan::Meta;
use rdf_types::{Generator, Vocabulary, VocabularyMut};
pub use treeldr::layout::field::Property;
pub use treeldr::layout::structure::field::Property;
use treeldr::{metadata::Merge, BlankIdIndex, Id, IriIndex, Name};

/// Layout field definition.
Expand Down Expand Up @@ -146,7 +146,7 @@ impl<M> Definition<M> {
_as_component: &treeldr::component::Data<M>,
as_formatted: &treeldr::component::formatted::Data<M>,
meta: M,
) -> Result<Meta<treeldr::layout::field::Definition<M>, M>, Error<M>>
) -> Result<Meta<treeldr::layout::structure::field::Definition<M>, M>, Error<M>>
where
M: Clone,
{
Expand All @@ -158,7 +158,7 @@ impl<M> Definition<M> {
Property::For,
)?;

Ok(Meta(treeldr::layout::field::Definition::new(prop), meta))
Ok(Meta(treeldr::layout::structure::field::Definition::new(prop), meta))
}
}

Expand Down
4 changes: 2 additions & 2 deletions build/src/layout/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ impl Definition {
as_component: &treeldr::component::Data<M>,
_as_formatted: &treeldr::component::formatted::Data<M>,
meta: M,
) -> Result<Meta<treeldr::layout::variant::Definition, M>, Error<M>>
) -> Result<Meta<treeldr::layout::enumeration::variant::Definition, M>, Error<M>>
where
M: Clone,
{
as_component.assert_named(as_resource, &meta)?;

Ok(Meta(treeldr::layout::variant::Definition, meta))
Ok(Meta(treeldr::layout::enumeration::variant::Definition, meta))
}
}

Expand Down
4 changes: 2 additions & 2 deletions build/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ impl<M: Clone> Definition<M> {
pub fn require_layout_field_id(
&self,
context: &crate::Context<M>,
) -> Result<treeldr::TId<treeldr::layout::Field>, NodeTypeInvalid<M>> {
) -> Result<treeldr::TId<treeldr::layout::structure::Field>, NodeTypeInvalid<M>> {
if self.has_type(context, component::formatted::Type::LayoutField) {
Ok(treeldr::TId::new(self.data.id))
} else {
Expand Down Expand Up @@ -462,7 +462,7 @@ impl<M: Clone> Definition<M> {
pub fn require_layout_variant_id(
&self,
context: &crate::Context<M>,
) -> Result<treeldr::TId<treeldr::layout::Variant>, NodeTypeInvalid<M>> {
) -> Result<treeldr::TId<treeldr::layout::enumeration::Variant>, NodeTypeInvalid<M>> {
if self.has_type(context, component::formatted::Type::LayoutVariant) {
Ok(treeldr::TId::new(self.data.id))
} else {
Expand Down
4 changes: 2 additions & 2 deletions core/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ impl<M> Definition<M> {
self.formatted.as_ref()
}

pub fn as_layout_field(&self) -> Option<&Meta<layout::field::Definition<M>, M>> {
pub fn as_layout_field(&self) -> Option<&Meta<layout::structure::field::Definition<M>, M>> {
self.formatted
.value()
.and_then(formatted::Definition::as_layout_field)
}

pub fn as_layout_variant(&self) -> Option<&Meta<layout::variant::Definition, M>> {
pub fn as_layout_variant(&self) -> Option<&Meta<layout::enumeration::variant::Definition, M>> {
self.formatted
.value()
.and_then(formatted::Definition::as_layout_variant)
Expand Down
18 changes: 9 additions & 9 deletions core/src/component/formatted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ impl<M> Data<M> {
#[derive(Debug)]
pub struct Definition<M> {
data: Data<M>,
layout_field: MetaOption<layout::field::Definition<M>, M>,
layout_variant: MetaOption<layout::variant::Definition, M>,
layout_field: MetaOption<layout::structure::field::Definition<M>, M>,
layout_variant: MetaOption<layout::enumeration::variant::Definition, M>,
}

impl<M> Definition<M> {
pub fn new(
data: Data<M>,
layout_field: MetaOption<layout::field::Definition<M>, M>,
layout_variant: MetaOption<layout::variant::Definition, M>,
layout_field: MetaOption<layout::structure::field::Definition<M>, M>,
layout_variant: MetaOption<layout::enumeration::variant::Definition, M>,
) -> Self {
Self {
data,
Expand All @@ -55,11 +55,11 @@ impl<M> Definition<M> {
self.layout_variant.is_some()
}

pub fn as_layout_field(&self) -> Option<&Meta<layout::field::Definition<M>, M>> {
pub fn as_layout_field(&self) -> Option<&Meta<layout::structure::field::Definition<M>, M>> {
self.layout_field.as_ref()
}

pub fn as_layout_variant(&self) -> Option<&Meta<layout::variant::Definition, M>> {
pub fn as_layout_variant(&self) -> Option<&Meta<layout::enumeration::variant::Definition, M>> {
self.layout_variant.as_ref()
}

Expand Down Expand Up @@ -98,7 +98,7 @@ impl Type {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Property {
Format,
LayoutField(layout::field::Property),
LayoutField(layout::structure::field::Property),
}

impl Property {
Expand Down Expand Up @@ -146,7 +146,7 @@ impl ClassBinding {

pub enum Binding {
Format(TId<Layout>),
LayoutField(layout::field::ClassBinding),
LayoutField(layout::structure::field::ClassBinding),
}

impl Binding {
Expand Down Expand Up @@ -185,7 +185,7 @@ impl<'a, M> Iterator for ClassBindings<'a, M> {
#[derivative(Default(bound = ""))]
pub struct Bindings<'a, M> {
data: ClassBindings<'a, M>,
layout_field: crate::layout::field::ClassBindings<'a, M>,
layout_field: crate::layout::structure::field::ClassBindings<'a, M>,
}

impl<'a, M> Iterator for Bindings<'a, M> {
Expand Down
14 changes: 6 additions & 8 deletions core/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@ use locspan::Meta;

pub mod array;
pub mod enumeration;
pub mod field;
pub mod value_enumeration;
mod one_or_many;
mod optional;
pub mod primitive;
mod reference;
mod required;
pub mod restriction;
mod set;
mod structure;
pub mod variant;
pub mod structure;

mod strongly_connected;
mod usages;

pub use array::Array;
pub use enumeration::Enum;
pub use field::Field;
pub use value_enumeration::ValueEnum;
pub use one_or_many::OneOrMany;
pub use optional::Optional;
pub use primitive::{restriction::Restricted as RestrictedPrimitive, Primitive};
Expand All @@ -35,7 +34,6 @@ pub use required::Required;
pub use restriction::{ContainerRestriction, ContainerRestrictions, Restrictions};
pub use set::Set;
pub use structure::Struct;
pub use variant::Variant;

pub use strongly_connected::StronglyConnectedLayouts;
pub use usages::Usages;
Expand Down Expand Up @@ -300,7 +298,7 @@ impl<M> Definition<M> {
pub enum ComposingLayouts<'a, M> {
Fields(
&'a crate::Model<M>,
std::slice::Iter<'a, Meta<TId<Field>, M>>,
std::slice::Iter<'a, Meta<TId<structure::Field>, M>>,
),
Enum(enumeration::ComposingLayouts<'a, M>),
One(Option<&'a Meta<TId<Layout>, M>>),
Expand Down Expand Up @@ -440,8 +438,8 @@ impl DescriptionProperty {
pub enum DescriptionBindingRef<'a, M> {
DerivedFrom(Primitive),
Reference(TId<Layout>),
Struct(&'a [Meta<TId<Field>, M>]),
Enum(&'a [Meta<TId<Variant>, M>]),
Struct(&'a [Meta<TId<structure::Field>, M>]),
Enum(&'a [Meta<TId<enumeration::Variant>, M>]),
Required(TId<Layout>),
Option(TId<Layout>),
Array(TId<Layout>),
Expand Down
4 changes: 3 additions & 1 deletion core/src/layout/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::collections::HashMap;
use crate::{Layout, TId};
use locspan::Meta;

use super::Variant;
pub mod variant;

pub use variant::Variant;

/// Enum layout.
#[derive(Debug, Clone)]
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion core/src/layout/structure.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::TId;
use locspan::Meta;

use super::field::Field;
pub mod field;

pub use field::Field;

/// Structure layout.
#[derive(Debug, Clone)]
Expand Down
File renamed without changes.
26 changes: 26 additions & 0 deletions core/src/layout/value_enumeration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::TId;
use locspan::Meta;

pub mod variant;

pub use variant::ValueVariant;

/// Enumeration.
#[derive(Debug, Clone)]
pub struct ValueEnum<M> {
members: Vec<Meta<TId<ValueVariant>, M>>,
}

pub struct Parts<M> {
pub members: Vec<Meta<TId<ValueVariant>, M>>,
}

impl<M> ValueEnum<M> {
pub fn new(members: Vec<Meta<TId<ValueVariant>, M>>) -> Self {
Self { members }
}

pub fn members(&self) -> &[Meta<TId<ValueVariant>, M>] {
&self.members
}
}
38 changes: 38 additions & 0 deletions core/src/layout/value_enumeration/variant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use locspan::Meta;

use crate::{MetaOption, Id};

pub struct ValueVariant;

pub struct Definition<M> {
/// Layout representation.
///
/// For now, only the `tldr:String` layout is supported.
representation: Meta<String, M>,

/// Optional value.
///
/// If the value enumeration layout is not orphan, then this value is not
/// `None`.
value: MetaOption<Id, M>
}

impl<M> Definition<M> {
pub fn new(
representation: Meta<String, M>,
value: MetaOption<Id, M>
) -> Self {
Self {
representation,
value
}
}

pub fn representation(&self) -> &Meta<String, M> {
&self.representation
}

pub fn value(&self) -> Option<&Meta<Id, M>> {
self.value.as_ref()
}
}
12 changes: 6 additions & 6 deletions core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ impl<M> Definition<M> {
.and_then(component::Definition::as_formatted)
}

pub fn as_layout_field(&self) -> Option<&Meta<layout::field::Definition<M>, M>> {
pub fn as_layout_field(&self) -> Option<&Meta<layout::structure::field::Definition<M>, M>> {
self.component
.value()
.and_then(component::Definition::as_layout_field)
}

pub fn as_layout_variant(&self) -> Option<&Meta<layout::variant::Definition, M>> {
pub fn as_layout_variant(&self) -> Option<&Meta<layout::enumeration::variant::Definition, M>> {
self.component
.value()
.and_then(component::Definition::as_layout_variant)
Expand Down Expand Up @@ -356,8 +356,8 @@ pub enum BindingValueRef<'a, M> {
DataType(TId<crate::ty::DataType<M>>),
Layout(TId<crate::Layout>),
Layouts(&'a Multiple<TId<crate::Layout>, M>),
Fields(&'a [Meta<TId<layout::Field>, M>]),
Variants(&'a [Meta<TId<layout::Variant>, M>]),
Fields(&'a [Meta<TId<layout::structure::Field>, M>]),
Variants(&'a [Meta<TId<layout::enumeration::Variant>, M>]),
Property(TId<crate::Property>),
DatatypeRestrictions(ty::data::Restrictions<'a>),
LayoutRestrictions(layout::Restrictions<'a, M>),
Expand Down Expand Up @@ -388,8 +388,8 @@ pub enum BindingValueIds<'a, M> {
DataType(Option<TId<crate::ty::DataType<M>>>),
Layout(Option<TId<crate::Layout>>),
Layouts(multiple::Iter<'a, TId<crate::Layout>, M>),
Fields(std::slice::Iter<'a, Meta<TId<layout::Field>, M>>),
Variants(std::slice::Iter<'a, Meta<TId<layout::Variant>, M>>),
Fields(std::slice::Iter<'a, Meta<TId<layout::structure::Field>, M>>),
Variants(std::slice::Iter<'a, Meta<TId<layout::enumeration::Variant>, M>>),
Property(Option<TId<crate::Property>>),
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ impl From<component::formatted::Property> for Property {
}
}

impl From<layout::field::Property> for Property {
fn from(p: layout::field::Property) -> Self {
impl From<layout::structure::field::Property> for Property {
fn from(p: layout::structure::field::Property) -> Self {
Self::Resource(node::Property::Component(component::Property::Formatted(
component::formatted::Property::LayoutField(p),
)))
Expand Down
4 changes: 2 additions & 2 deletions modules/json-ld-context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl unresolved::Bindings {
local_contexts: &mut unresolved::LocalContexts,
layout_contexts: &mut LayoutLocalContexts,
parent: Ref<unresolved::LocalContext>,
fid: TId<treeldr::layout::Field>,
fid: TId<treeldr::layout::structure::Field>,
) {
let f = builder.model.get(fid).unwrap();

Expand Down Expand Up @@ -432,7 +432,7 @@ impl<'a, V: IriVocabulary<Iri = IriIndex>, M> Builder<'a, V, M> {
}
}

pub fn is_type_field(&self, field_id: TId<treeldr::layout::Field>) -> bool {
pub fn is_type_field(&self, field_id: TId<treeldr::layout::structure::Field>) -> bool {
let field = self.model.get(field_id).unwrap();
match field.as_layout_field().property() {
Some(property_ref) => self.is_type_property(**property_ref),
Expand Down