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 ++- 2 files changed, 202 insertions(+), 223 deletions(-) (limited to 'src') 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, }), -- cgit v1.2.3