diff options
-rw-r--r-- | src/lib.rs | 401 | ||||
-rw-r--r-- | src/schema_schema.rs | 24 | ||||
-rw-r--r-- | tests/helpers/mod.rs | 54 | ||||
-rw-r--r-- | tests/schema-via-knuffel.rs | 56 |
4 files changed, 259 insertions, 276 deletions
@@ -1,5 +1,5 @@ #[cfg(feature = "knuffel")] -use knuffel::Decode; +use knuffel::{Decode, DecodeScalar}; pub trait BuildFromRef { fn ref_to(query: impl Into<String>) -> Self; @@ -21,239 +21,220 @@ pub struct Document { pub nodes: Vec<Node>, } -pub use info::Info; - -pub mod info { - use super::*; - - #[derive(Debug, PartialEq, Eq, Default)] - #[cfg_attr(feature = "knuffel", derive(Decode))] - pub struct Info { - #[cfg_attr(feature = "knuffel", knuffel(children(name = "title")))] - pub title: Vec<TextValue>, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "description")))] - pub description: Vec<TextValue>, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "author")))] - pub authors: Vec<Person>, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "contributor")))] - pub contributors: Vec<Person>, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "link")))] - pub links: Vec<Link>, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "license")))] - pub licenses: Vec<License>, - #[cfg_attr(feature = "knuffel", knuffel(child))] - pub published: Option<Date>, - #[cfg_attr(feature = "knuffel", knuffel(child))] - pub modified: Option<Date>, - } - - #[derive(Debug, PartialEq, Eq)] - #[cfg_attr(feature = "knuffel", derive(Decode))] - pub struct TextValue { - #[cfg_attr(feature = "knuffel", knuffel(argument))] - pub text: String, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub lang: Option<String>, - } - - #[derive(Debug, PartialEq, Eq)] - #[cfg_attr(feature = "knuffel", derive(Decode))] - pub struct Person { - #[cfg_attr(feature = "knuffel", knuffel(argument))] - pub name: String, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub orcid: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "link")))] - pub links: Vec<Link>, - } +#[derive(Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub struct Info { + #[cfg_attr(feature = "knuffel", knuffel(children(name = "title")))] + pub title: Vec<TextValue>, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "description")))] + pub description: Vec<TextValue>, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "author")))] + pub authors: Vec<Person>, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "contributor")))] + pub contributors: Vec<Person>, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "link")))] + pub links: Vec<Link>, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "license")))] + pub licenses: Vec<License>, + #[cfg_attr(feature = "knuffel", knuffel(child))] + pub published: Option<Date>, + #[cfg_attr(feature = "knuffel", knuffel(child))] + pub modified: Option<Date>, +} - #[derive(Debug, PartialEq, Eq)] - #[cfg_attr(feature = "knuffel", derive(Decode))] - pub struct Link { - #[cfg_attr(feature = "knuffel", knuffel(argument))] - pub iri: String, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub rel: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub lang: Option<String>, - } +#[derive(Debug, PartialEq, Eq)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub struct TextValue { + #[cfg_attr(feature = "knuffel", knuffel(argument))] + pub text: String, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub lang: Option<String>, +} - #[derive(Debug, PartialEq, Eq)] - #[cfg_attr(feature = "knuffel", derive(Decode))] - pub struct License { - #[cfg_attr(feature = "knuffel", knuffel(argument))] - pub name: String, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub spdx: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "link")))] - pub link: Vec<Link>, - } +#[derive(Debug, PartialEq, Eq)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub struct Person { + #[cfg_attr(feature = "knuffel", knuffel(argument))] + pub name: String, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub orcid: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "link")))] + pub links: Vec<Link>, +} - #[derive(Debug, PartialEq, Eq)] - #[cfg_attr(feature = "knuffel", derive(Decode))] - pub struct Date { - #[cfg_attr(feature = "knuffel", knuffel(argument))] - pub date: String, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub time: Option<String>, - } +#[derive(Debug, PartialEq, Eq)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub struct Link { + #[cfg_attr(feature = "knuffel", knuffel(argument))] + pub iri: String, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub rel: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub lang: Option<String>, } -pub use node::Node; +#[derive(Debug, PartialEq, Eq)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub struct License { + #[cfg_attr(feature = "knuffel", knuffel(argument))] + pub name: String, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub spdx: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "link")))] + pub link: Vec<Link>, +} -pub mod node { - use super::*; +#[derive(Debug, PartialEq, Eq)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub struct Date { + #[cfg_attr(feature = "knuffel", knuffel(argument))] + pub date: String, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub time: Option<String>, +} - #[derive(Debug, PartialEq, Eq, Default)] - #[cfg_attr(feature = "knuffel", derive(Decode))] - pub struct Node { - #[cfg_attr(feature = "knuffel", knuffel(argument))] - pub name: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub id: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub description: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub ref_: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] - pub min: Option<usize>, - #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] - pub max: Option<usize>, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "prop")))] - pub props: Vec<Prop>, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "value")))] - pub values: Vec<Value>, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "children")))] - pub children: Vec<Children>, - } +#[derive(Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub struct Node { + #[cfg_attr(feature = "knuffel", knuffel(argument))] + pub name: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub id: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub description: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub ref_: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] + pub min: Option<usize>, + #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] + pub max: Option<usize>, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "prop")))] + pub props: Vec<Prop>, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "value")))] + pub values: Vec<Value>, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "children")))] + pub children: Vec<Children>, +} - impl BuildFromRef for Node { - fn ref_to(query: impl Into<String>) -> Self { - Self { - ref_: Some(query.into()), - ..Self::default() - } +impl BuildFromRef for Node { + fn ref_to(query: impl Into<String>) -> Self { + Self { + ref_: Some(query.into()), + ..Self::default() } } +} - #[derive(Debug, PartialEq, Eq, Default)] - #[cfg_attr(feature = "knuffel", derive(Decode))] - pub struct Prop { - #[cfg_attr(feature = "knuffel", knuffel(argument))] - pub key: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub id: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub description: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub ref_: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(child))] - pub required: bool, - #[cfg_attr(feature = "knuffel", knuffel(children))] - pub validations: Vec<Validation>, - } +#[derive(Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub struct Prop { + #[cfg_attr(feature = "knuffel", knuffel(argument))] + pub key: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub id: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub description: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub ref_: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(child))] + pub required: bool, + #[cfg_attr(feature = "knuffel", knuffel(children))] + pub validations: Vec<Validation>, +} - impl BuildFromRef for Prop { - fn ref_to(query: impl Into<String>) -> Self { - Self { - ref_: Some(query.into()), - ..Self::default() - } +impl BuildFromRef for Prop { + fn ref_to(query: impl Into<String>) -> Self { + Self { + ref_: Some(query.into()), + ..Self::default() } } +} - #[derive(Debug, PartialEq, Eq, Default)] - #[cfg_attr(feature = "knuffel", derive(Decode))] - pub struct Value { - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub id: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub description: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub ref_: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] - pub min: Option<usize>, - #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] - pub max: Option<usize>, - #[cfg_attr(feature = "knuffel", knuffel(children))] - pub validations: Vec<Validation>, - } +#[derive(Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub struct Value { + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub id: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub description: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub ref_: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] + pub min: Option<usize>, + #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] + pub max: Option<usize>, + #[cfg_attr(feature = "knuffel", knuffel(children))] + pub validations: Vec<Validation>, +} - impl BuildFromRef for Value { - fn ref_to(query: impl Into<String>) -> Self { - Self { - ref_: Some(query.into()), - ..Self::default() - } +impl BuildFromRef for Value { + fn ref_to(query: impl Into<String>) -> Self { + Self { + ref_: Some(query.into()), + ..Self::default() } } +} - #[derive(Debug, PartialEq, Eq, Default)] - #[cfg_attr(feature = "knuffel", derive(Decode))] - pub struct Children { - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub id: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub description: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub ref_: Option<String>, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "node")))] - pub nodes: Vec<Node>, - } +#[derive(Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub struct Children { + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub id: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub description: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub ref_: Option<String>, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "node")))] + pub nodes: Vec<Node>, +} - impl BuildFromRef for Children { - fn ref_to(query: impl Into<String>) -> Self { - Self { - ref_: Some(query.into()), - ..Self::default() - } +impl BuildFromRef for Children { + fn ref_to(query: impl Into<String>) -> Self { + Self { + ref_: Some(query.into()), + ..Self::default() } } +} - pub use validation::Validation; - - pub mod validation { - #[cfg(feature = "knuffel")] - use knuffel::{Decode, DecodeScalar}; - - #[derive(Debug, PartialEq, Eq)] - #[cfg_attr(feature = "knuffel", derive(Decode))] - pub enum Validation { - Type(#[cfg_attr(feature = "knuffel", knuffel(argument))] String), - Enum(#[cfg_attr(feature = "knuffel", knuffel(arguments))] Vec<String>), - Pattern(#[cfg_attr(feature = "knuffel", knuffel(argument))] String), - Format(#[cfg_attr(feature = "knuffel", knuffel(arguments))] Vec<Format>), - } +#[derive(Debug, PartialEq, Eq)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub enum Validation { + Type(#[cfg_attr(feature = "knuffel", knuffel(argument))] String), + Enum(#[cfg_attr(feature = "knuffel", knuffel(arguments))] Vec<String>), + Pattern(#[cfg_attr(feature = "knuffel", knuffel(argument))] String), + Format(#[cfg_attr(feature = "knuffel", knuffel(arguments))] Vec<Format>), +} - #[derive(Debug, PartialEq, Eq)] - #[cfg_attr(feature = "knuffel", derive(DecodeScalar))] - pub enum Format { - DateTime, - Date, - Time, - Duration, - Decimal, - Currency, - Country2, - Country3, - CountrySubdivision, - Email, - IdnEmail, - Hostname, - IdnHostname, - Ipv4, - Ipv6, - Url, - UrlReference, - Irl, - IrlReference, - UrlTemplate, - Uuid, - Regex, - Base64, - KdlQuery, - } - } +#[derive(Debug, PartialEq, Eq)] +#[cfg_attr(feature = "knuffel", derive(DecodeScalar))] +pub enum Format { + DateTime, + Date, + Time, + Duration, + Decimal, + Currency, + Country2, + Country3, + CountrySubdivision, + Email, + IdnEmail, + Hostname, + IdnHostname, + Ipv4, + Ipv6, + Url, + UrlReference, + Irl, + IrlReference, + UrlTemplate, + Uuid, + Regex, + Base64, + KdlQuery, } mod schema_schema; diff --git a/src/schema_schema.rs b/src/schema_schema.rs index d548f5a..c7cc2ec 100644 --- a/src/schema_schema.rs +++ b/src/schema_schema.rs @@ -1,7 +1,5 @@ use lazy_static::lazy_static; -use super::node::validation::*; -use super::node::*; use super::*; lazy_static! { @@ -742,51 +740,51 @@ fn make_info_node() -> Node { fn make_schema_info() -> Info { Info { - title: vec![info::TextValue { + title: vec![TextValue { text: s("KDL Schema"), lang: s("en"), }], - description: vec![info::TextValue { + description: vec![TextValue { text: s("KDL Schema KDL schema in KDL"), lang: s("en"), }], - authors: vec![info::Person { + authors: vec![Person { name: s("Kat Marchán"), orcid: None, - links: vec![info::Link { + links: vec![Link { iri: s("https://github.com/zkat"), rel: s("self"), lang: None, }], }], - contributors: vec![info::Person { + contributors: vec![Person { name: s("Lars Willighagen"), orcid: None, - links: vec![info::Link { + links: vec![Link { iri: s("https://github.com/larsgw"), rel: s("self"), lang: None, }], }], - links: vec![info::Link { + links: vec![Link { iri: s("https://github.com/zkat/kdl"), rel: s("documentation"), lang: None, }], - licenses: vec![info::License { + licenses: vec![License { name: s("Creative Commons Attribution-ShareAlike 4.0 International License"), spdx: s("CC-BY-SA-4.0"), - link: vec![info::Link { + link: vec![Link { iri: s("https://creativecommons.org/licenses/by-sa/4.0/"), rel: None, lang: s("en"), }], }], - published: Some(info::Date { + published: Some(Date { date: s("2021-08-31"), time: None, }), - modified: Some(info::Date { + modified: Some(Date { date: s("2021-09-01"), time: None, }), diff --git a/tests/helpers/mod.rs b/tests/helpers/mod.rs new file mode 100644 index 0000000..09499ad --- /dev/null +++ b/tests/helpers/mod.rs @@ -0,0 +1,54 @@ +use std::fmt::Debug; + +use kdl_schema::*; + +pub trait DeepAssertEq +where + Self: PartialEq + Debug, +{ + fn deep_assert_eq(left: &Self, right: &Self) { + assert_eq!(left, right); + } +} + +impl DeepAssertEq for Schema { + fn deep_assert_eq(left: &Self, right: &Self) { + DeepAssertEq::deep_assert_eq(&left.document, &right.document) + } +} + +impl DeepAssertEq for Document { + fn deep_assert_eq(left: &Self, right: &Self) { + DeepAssertEq::deep_assert_eq(&left.info, &right.info); + DeepAssertEq::deep_assert_eq(&left.nodes, &right.nodes); + } +} + +impl<T: DeepAssertEq> DeepAssertEq for Vec<T> { + fn deep_assert_eq(left: &Self, right: &Self) { + for (left, right) in left.iter().zip(right.iter()) { + DeepAssertEq::deep_assert_eq(left, right); + } + assert_eq!(left, right); + } +} + +impl DeepAssertEq for Info {} + +impl DeepAssertEq for Node { + fn deep_assert_eq(left: &Self, right: &Self) { + DeepAssertEq::deep_assert_eq(&left.props, &right.props); + DeepAssertEq::deep_assert_eq(&left.values, &right.values); + DeepAssertEq::deep_assert_eq(&left.children, &right.children); + assert_eq!(left, right); + } +} + +impl DeepAssertEq for Prop {} +impl DeepAssertEq for Value {} +impl DeepAssertEq for Children { + fn deep_assert_eq(left: &Self, right: &Self) { + DeepAssertEq::deep_assert_eq(&left.nodes, &right.nodes); + assert_eq!(left, right); + } +} diff --git a/tests/schema-via-knuffel.rs b/tests/schema-via-knuffel.rs index b0d1a1c..147c905 100644 --- a/tests/schema-via-knuffel.rs +++ b/tests/schema-via-knuffel.rs @@ -1,59 +1,9 @@ #![cfg(feature = "knuffel")] -use std::fmt::Debug; +use kdl_schema::{Schema, SCHEMA_SCHEMA}; -use kdl_schema::*; - -trait DeepAssertEq -where - Self: PartialEq + Debug, -{ - fn deep_assert_eq(left: &Self, right: &Self) { - assert_eq!(left, right); - } -} - -impl DeepAssertEq for Schema { - fn deep_assert_eq(left: &Self, right: &Self) { - DeepAssertEq::deep_assert_eq(&left.document, &right.document) - } -} - -impl DeepAssertEq for Document { - fn deep_assert_eq(left: &Self, right: &Self) { - DeepAssertEq::deep_assert_eq(&left.info, &right.info); - DeepAssertEq::deep_assert_eq(&left.nodes, &right.nodes); - } -} - -impl<T: DeepAssertEq> DeepAssertEq for Vec<T> { - fn deep_assert_eq(left: &Self, right: &Self) { - for (left, right) in left.iter().zip(right.iter()) { - DeepAssertEq::deep_assert_eq(left, right); - } - assert_eq!(left, right); - } -} - -impl DeepAssertEq for Info {} - -impl DeepAssertEq for Node { - fn deep_assert_eq(left: &Self, right: &Self) { - DeepAssertEq::deep_assert_eq(&left.props, &right.props); - DeepAssertEq::deep_assert_eq(&left.values, &right.values); - DeepAssertEq::deep_assert_eq(&left.children, &right.children); - assert_eq!(left, right); - } -} - -impl DeepAssertEq for node::Prop {} -impl DeepAssertEq for node::Value {} -impl DeepAssertEq for node::Children { - fn deep_assert_eq(left: &Self, right: &Self) { - DeepAssertEq::deep_assert_eq(&left.nodes, &right.nodes); - assert_eq!(left, right); - } -} +mod helpers; +use helpers::DeepAssertEq; #[test] fn schema_loads() -> miette::Result<()> { |