From 8a9658b9febe98ab17b660b2780e85c89d49c6e0 Mon Sep 17 00:00:00 2001 From: Ivan Subotic <400790+subotic@users.noreply.github.com> Date: Wed, 6 Sep 2023 15:33:30 +0200 Subject: [PATCH] add conversion of project (ongoing) --- Cargo.lock | 68 ++++++++++++++++++++++++++ Cargo.toml | 2 + src/dsp_meta/domain/convert/project.rs | 17 ++++--- src/dsp_meta/domain/mod.rs | 44 ++++++++++++++++- src/dsp_meta/domain/project.rs | 18 +++++-- 5 files changed, 139 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 985f1614..e06baed4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,6 +130,7 @@ dependencies = [ "tracing", "tracing-subscriber", "tracing-test", + "url", ] [[package]] @@ -144,6 +145,15 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "form_urlencoded" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652" +dependencies = [ + "percent-encoding", +] + [[package]] name = "hashbrown" version = "0.14.0" @@ -195,6 +205,16 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "idna" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "indexmap" version = "2.0.0" @@ -271,6 +291,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" +[[package]] +name = "percent-encoding" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" + [[package]] name = "pin-project-lite" version = "0.2.13" @@ -435,6 +461,21 @@ dependencies = [ "once_cell", ] +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tracing" version = "0.1.37" @@ -520,12 +561,39 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "unicode-bidi" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" + [[package]] name = "unicode-ident" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" +[[package]] +name = "unicode-normalization" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "url" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "143b538f18257fac9cad154828a57c6bf5157e1aa604d4816b5995bf6de87ae5" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + [[package]] name = "utf8parse" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 49af8d3e..79f47f1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ serde_json = "1" # JSON serialization tracing = "0.1" tracing-subscriber = "0.3" tracing-test = "0.2" +url = { version = "2", features = ["serde"] } warp = "0.3" [package] @@ -47,6 +48,7 @@ serde_json.workspace = true tracing.workspace = true tracing-subscriber.workspace = true tracing-test.workspace = true +url.workspace = true [[bin]] name = "dsp_meta" diff --git a/src/dsp_meta/domain/convert/project.rs b/src/dsp_meta/domain/convert/project.rs index 405ff934..32c2f4a4 100644 --- a/src/dsp_meta/domain/convert/project.rs +++ b/src/dsp_meta/domain/convert/project.rs @@ -1,4 +1,4 @@ -use hcl::Expression; +use hcl::{Attribute, Expression}; use tracing::warn; use crate::domain::{ @@ -124,14 +124,11 @@ impl TryFrom> for ExtractedProjectBlocks { type Error = DspMetaError; fn try_from(blocks: Vec<&hcl::Block>) -> Result { - let mut _alternative_names: Vec<&AlternativeName> = vec![]; + let mut alternative_names: Vec<&AlternativeName> = vec![]; for block in blocks { match block.identifier.as_str() { - "alternative_name" => { - println!("alternative_name"); - dbg!(block); - } + "alternative_name" => alternative_names.push(&AlternativeName::try_from(&block)?), "description" => { println!("description"); dbg!(block); @@ -149,6 +146,14 @@ impl TryFrom> for ExtractedProjectBlocks { } } +impl TryFrom<&hcl::Block> for AlternativeName { + type Error = DspMetaError; + + fn try_from(block: &hcl::Block) -> Result { + AlternativeName::default() + } +} + #[cfg(test)] mod tests { use hcl::{Identifier, Number}; diff --git a/src/dsp_meta/domain/mod.rs b/src/dsp_meta/domain/mod.rs index f2bb0a3a..da7e37cb 100644 --- a/src/dsp_meta/domain/mod.rs +++ b/src/dsp_meta/domain/mod.rs @@ -2,6 +2,8 @@ use std::collections::HashSet; use std::fmt::Debug; use std::hash::Hash; +use url::Url as UrlString; + use hcl::Block; use crate::domain::dataset::Dataset; @@ -205,7 +207,7 @@ impl Default for Description { Self::from(vec![ LangString { iso_code: IsoCode::DE, - string: String::from("Die default Beschreibung."), + string: String::from("Die Default-Beschreibung."), }, LangString { iso_code: IsoCode::EN, @@ -232,9 +234,27 @@ impl From> for Description { } } +#[derive(Debug, PartialEq, Eq)] +pub struct UrlValue { + pub value: UrlString, + pub text: String, +} + +impl Default for UrlValue { + fn default() -> Self { + UrlValue { + value: UrlString::try_from("https://default.xyz").unwrap(), + text: "Default URL description".to_string(), + } + } +} + #[derive(Debug, Default, Clone, PartialEq)] pub struct HowToCite(String); +#[derive(Debug, Default, PartialEq)] +pub struct Keyword(LangString); + #[derive(Debug, Default, Clone, PartialEq)] pub struct StartDate(String); @@ -250,6 +270,28 @@ pub struct Grants(Vec); #[derive(Debug, Default, PartialEq)] pub struct Title(String); +#[derive(Debug, Default, PartialEq)] +pub struct Discipline { + discipline_type: DisciplineType, + description: LangString, + url: UrlValue, +} + +#[derive(Debug, Default, PartialEq)] +pub enum DisciplineType { + #[default] + Snf, +} + +#[derive(Debug, PartialEq)] +pub struct Publication(String); + +impl Default for Publication { + fn default() -> Self { + Publication("Default publication".to_string()) + } +} + #[cfg(test)] mod tests { use hcl::body; diff --git a/src/dsp_meta/domain/project.rs b/src/dsp_meta/domain/project.rs index 8a582f86..c2d6b0f4 100644 --- a/src/dsp_meta/domain/project.rs +++ b/src/dsp_meta/domain/project.rs @@ -1,11 +1,11 @@ use crate::domain::convert::project::{ExtractedProjectAttributes, ExtractedProjectBlocks}; use crate::domain::{ - AlternativeNames, CreatedAt, CreatedBy, Description, EndDate, HowToCite, Name, Shortcode, - StartDate, TeaserText, + AlternativeNames, CreatedAt, CreatedBy, Description, Discipline, EndDate, HowToCite, Keyword, + Name, Publication, Shortcode, StartDate, TeaserText, UrlValue, }; use crate::errors::DspMetaError; -#[derive(Debug, Default, Clone, PartialEq)] +#[derive(Debug, Default, PartialEq)] pub struct Project { pub created_at: CreatedAt, pub created_by: CreatedBy, @@ -14,9 +14,13 @@ pub struct Project { pub alternative_names: AlternativeNames, pub teaser_text: TeaserText, pub description: Description, + pub url: UrlValue, pub how_to_cite: HowToCite, pub start_date: StartDate, pub end_date: Option, + pub keywords: Vec, + pub disciplines: Vec, + pub publications: Vec, } impl TryFrom<&hcl::Block> for Project { @@ -74,6 +78,10 @@ impl TryFrom<&hcl::Block> for Project { let alternative_names = AlternativeNames::default(); let description = Description::default(); + let url = UrlValue::default(); + let keywords = vec![Keyword::default()]; + let disciplines = vec![Discipline::default()]; + let publications = vec![Publication::default()]; let project = Project { created_at, @@ -83,9 +91,13 @@ impl TryFrom<&hcl::Block> for Project { alternative_names, teaser_text, description, + url, how_to_cite, start_date, end_date, + keywords, + disciplines, + publications, }; Ok(project)