From 73740e6dd0723113e22a26aa389e545085f58857 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sat, 26 Mar 2022 20:13:37 -0600 Subject: refactor lib out of modules and test helper into a module --- src/lib.rs | 401 +++++++++++++++++++++----------------------- src/schema_schema.rs | 24 ++- tests/helpers/mod.rs | 54 ++++++ tests/schema-via-knuffel.rs | 56 +------ 4 files changed, 259 insertions(+), 276 deletions(-) create mode 100644 tests/helpers/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 7f0966b..695c973 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ #[cfg(feature = "knuffel")] -use knuffel::Decode; +use knuffel::{Decode, DecodeScalar}; pub trait BuildFromRef { fn ref_to(query: impl Into) -> Self; @@ -21,239 +21,220 @@ pub struct Document { pub nodes: Vec, } -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, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "description")))] - pub description: Vec, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "author")))] - pub authors: Vec, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "contributor")))] - pub contributors: Vec, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "link")))] - pub links: Vec, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "license")))] - pub licenses: Vec, - #[cfg_attr(feature = "knuffel", knuffel(child))] - pub published: Option, - #[cfg_attr(feature = "knuffel", knuffel(child))] - pub modified: Option, - } - - #[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, - } - - #[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, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "link")))] - pub links: Vec, - } +#[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, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "description")))] + pub description: Vec, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "author")))] + pub authors: Vec, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "contributor")))] + pub contributors: Vec, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "link")))] + pub links: Vec, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "license")))] + pub licenses: Vec, + #[cfg_attr(feature = "knuffel", knuffel(child))] + pub published: Option, + #[cfg_attr(feature = "knuffel", knuffel(child))] + pub modified: Option, +} - #[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, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub lang: Option, - } +#[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, +} - #[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, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "link")))] - pub link: Vec, - } +#[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, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "link")))] + pub links: Vec, +} - #[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, - } +#[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, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub lang: Option, } -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, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "link")))] + pub link: Vec, +} -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, +} - #[derive(Debug, PartialEq, Eq, Default)] - #[cfg_attr(feature = "knuffel", derive(Decode))] - pub struct Node { - #[cfg_attr(feature = "knuffel", knuffel(argument))] - pub name: Option, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub id: Option, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub description: Option, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub ref_: Option, - #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] - pub min: Option, - #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] - pub max: Option, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "prop")))] - pub props: Vec, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "value")))] - pub values: Vec, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "children")))] - pub children: Vec, - } +#[derive(Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub struct Node { + #[cfg_attr(feature = "knuffel", knuffel(argument))] + pub name: Option, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub id: Option, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub description: Option, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub ref_: Option, + #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] + pub min: Option, + #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] + pub max: Option, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "prop")))] + pub props: Vec, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "value")))] + pub values: Vec, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "children")))] + pub children: Vec, +} - impl BuildFromRef for Node { - fn ref_to(query: impl Into) -> Self { - Self { - ref_: Some(query.into()), - ..Self::default() - } +impl BuildFromRef for Node { + fn ref_to(query: impl Into) -> 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, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub id: Option, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub description: Option, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub ref_: Option, - #[cfg_attr(feature = "knuffel", knuffel(child))] - pub required: bool, - #[cfg_attr(feature = "knuffel", knuffel(children))] - pub validations: Vec, - } +#[derive(Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub struct Prop { + #[cfg_attr(feature = "knuffel", knuffel(argument))] + pub key: Option, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub id: Option, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub description: Option, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub ref_: Option, + #[cfg_attr(feature = "knuffel", knuffel(child))] + pub required: bool, + #[cfg_attr(feature = "knuffel", knuffel(children))] + pub validations: Vec, +} - impl BuildFromRef for Prop { - fn ref_to(query: impl Into) -> Self { - Self { - ref_: Some(query.into()), - ..Self::default() - } +impl BuildFromRef for Prop { + fn ref_to(query: impl Into) -> 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, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub description: Option, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub ref_: Option, - #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] - pub min: Option, - #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] - pub max: Option, - #[cfg_attr(feature = "knuffel", knuffel(children))] - pub validations: Vec, - } +#[derive(Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub struct Value { + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub id: Option, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub description: Option, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub ref_: Option, + #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] + pub min: Option, + #[cfg_attr(feature = "knuffel", knuffel(child, unwrap(argument)))] + pub max: Option, + #[cfg_attr(feature = "knuffel", knuffel(children))] + pub validations: Vec, +} - impl BuildFromRef for Value { - fn ref_to(query: impl Into) -> Self { - Self { - ref_: Some(query.into()), - ..Self::default() - } +impl BuildFromRef for Value { + fn ref_to(query: impl Into) -> 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, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub description: Option, - #[cfg_attr(feature = "knuffel", knuffel(property))] - pub ref_: Option, - #[cfg_attr(feature = "knuffel", knuffel(children(name = "node")))] - pub nodes: Vec, - } +#[derive(Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "knuffel", derive(Decode))] +pub struct Children { + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub id: Option, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub description: Option, + #[cfg_attr(feature = "knuffel", knuffel(property))] + pub ref_: Option, + #[cfg_attr(feature = "knuffel", knuffel(children(name = "node")))] + pub nodes: Vec, +} - impl BuildFromRef for Children { - fn ref_to(query: impl Into) -> Self { - Self { - ref_: Some(query.into()), - ..Self::default() - } +impl BuildFromRef for Children { + fn ref_to(query: impl Into) -> 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), - Pattern(#[cfg_attr(feature = "knuffel", knuffel(argument))] String), - Format(#[cfg_attr(feature = "knuffel", knuffel(arguments))] Vec), - } +#[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), + Pattern(#[cfg_attr(feature = "knuffel", knuffel(argument))] String), + Format(#[cfg_attr(feature = "knuffel", knuffel(arguments))] Vec), +} - #[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 DeepAssertEq for Vec { + 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 DeepAssertEq for Vec { - 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<()> { -- cgit v1.2.3