Skip to content

Commit

Permalink
refactor conversion of project
Browse files Browse the repository at this point in the history
  • Loading branch information
subotic committed Aug 31, 2023
1 parent d809c9c commit 56fbd69
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 35 deletions.
9 changes: 5 additions & 4 deletions src/dsp_meta/domain/converter/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use hcl::Block;

use crate::domain::converter::project::project_blocks::parse_project_blocks;
use crate::domain::project::Project;
use crate::domain::{
AlternativeNames, CreatedAt, CreatedBy, Description, EndDate, HowToCite, Name, Shortcode,
StartDate, TeaserText, ID,
};
use crate::domain::{AlternativeNames, Description, ID};
use crate::errors::DspMetaError;

mod project_attributes;
Expand Down Expand Up @@ -92,6 +89,10 @@ mod tests {
use hcl::block;
use tracing_test::traced_test;

use crate::domain::{
CreatedAt, CreatedBy, EndDate, HowToCite, Name, Shortcode, StartDate, TeaserText,
};

use super::*;

#[traced_test]
Expand Down
52 changes: 22 additions & 30 deletions src/dsp_meta/domain/converter/project/project_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,24 @@ use hcl::{Attribute, Expression};
use tracing::warn;

use crate::domain::{
CreatedAt, CreatedBy, EndDate, HowToCite, Name, ProjectValue, Shortcode, StartDate, TeaserText,
CreatedAt, CreatedBy, EndDate, HowToCite, Name, Shortcode, StartDate, TeaserText,
};
use crate::errors::DspMetaError;

pub struct ExtractedAttributes {
pub struct ExtractedAttributes<'a> {
pub created_at: Option<CreatedAt>,
pub created_by: Option<CreatedBy>,
pub shortcode: Option<Shortcode>,
pub name: Option<Name>,
pub teaser_text: Option<TeaserText>,
pub how_to_cite: Option<HowToCite>,
pub start_date: Option<StartDate>,
pub end_date: Option<EndDate>,
pub created_by: Option<CreatedBy<'a>>,
pub shortcode: Option<Shortcode<'a>>,
pub name: Option<Name<'a>>,
pub teaser_text: Option<TeaserText<'a>>,
pub how_to_cite: Option<HowToCite<'a>>,
pub start_date: Option<StartDate<'a>>,
pub end_date: Option<EndDate<'a>>,
}

pub fn extract_project_attributes(
attributes: Vec<&Attribute>,
) -> Result<ExtractedAttributes, DspMetaError> {
let mut results: HashMap<&str, ProjectValue> = HashMap::new();

let mut created_at: Option<CreatedAt> = None;
let mut created_by: Option<CreatedBy> = None;
let mut shortcode: Option<Shortcode> = None;
Expand All @@ -45,65 +43,59 @@ pub fn extract_project_attributes(
}
"created_by" => {
created_by = match attribute.expr() {
Expression::String(value) => Ok(Some(CreatedBy(value.to_string()))),
Expression::String(value) => Ok(Some(CreatedBy(value))),
_ => Err(DspMetaError::ParseProject(
"Parse error: created_by needs to be a string.",
)),
}?
}
"shortcode" => {
let shortcode = match attribute.expr() {
Expression::String(value) => Ok(Some(Shortcode(value.to_string()))),
shortcode = match attribute.expr() {
Expression::String(value) => Ok(Some(Shortcode(value))),
_ => Err(DspMetaError::ParseProject(
"Parse error: shortcode needs to be a string.",
)),
}?;
results.insert("shortcode", shortcode);
}
"name" => {
let name = match attribute.expr() {
Expression::String(value) => Ok(Some(Name(value.to_string()))),
name = match attribute.expr() {
Expression::String(value) => Ok(Some(Name(value))),
_ => Err(DspMetaError::ParseProject(
"Parse error: name needs to be a string.",
)),
}?;
results.insert("name", name);
}
"teaser_text" => {
let teaser_text = match attribute.expr() {
Expression::String(value) => Ok(Some(TeaserText(value.to_string()))),
Expression::String(value) => Ok(Some(TeaserText(value))),
_ => Err(DspMetaError::ParseProject(
"Parse error: teaser_text needs to be a string.",
)),
}?;
results.insert("teaser_text", teaser_text);
}
"how_to_cite" => {
let how_to_cite = match attribute.expr() {
Expression::String(value) => Ok(ProjectValue::HowToCite(HowToCite::new(value))),
how_to_cite = match attribute.expr() {
Expression::String(value) => Ok(Some(HowToCite(value))),
_ => Err(DspMetaError::ParseProject(
"Parse error: how_to_cite needs to be a string.",
)),
}?;
results.insert("how_to_cite", how_to_cite);
}
"start_date" => {
let start_date = match attribute.expr() {
Expression::String(value) => Ok(ProjectValue::StartDate(StartDate::new(value))),
start_date = match attribute.expr() {
Expression::String(value) => Ok(Some(StartDate(value))),
_ => Err(DspMetaError::ParseProject(
"Parse error: start_date needs to be a string.",
)),
}?;
results.insert("start_date", start_date);
}
"end_date" => {
let end_date = match attribute.expr() {
Expression::String(value) => Ok(ProjectValue::EndDate(EndDate::new(value))),
end_date = match attribute.expr() {
Expression::String(value) => Ok(Some(EndDate(value))),
_ => Err(DspMetaError::ParseProject(
"Parse error: end_date needs to be a string.",
)),
}?;
results.insert("end_date", end_date);
}
_ => {
warn!("Parse error: unknown attribute '{}'.", attribute.key());
Expand Down Expand Up @@ -141,7 +133,7 @@ mod tests {
let attribute = Attribute::new("created_by", "someone");
let attributes = vec![&attribute];
let result = extract_project_attributes(attributes).unwrap();
assert_eq!(result.created_by.unwrap(), CreatedBy::from("someone"));
assert_eq!(result.created_by.unwrap(), CreatedBy("someone"));
}

#[traced_test]
Expand Down
6 changes: 5 additions & 1 deletion src/dsp_meta/domain/converter/project/project_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ use std::collections::HashMap;

use hcl::Block;

use crate::domain::ProjectValue;
use crate::errors::DspMetaError;

struct ExtractedProjectBlocks<'a> {
pub alternative_names: Vec<AlterntiveName>,
pub description: Option<Description<'a>>,
}

pub fn parse_project_blocks(
blocks: Vec<&Block>,
) -> Result<HashMap<&str, ProjectValue>, DspMetaError> {
Expand Down
26 changes: 26 additions & 0 deletions src/dsp_meta/domain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ pub struct Name<'a>(&'a str);
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub struct AlternativeNames(HashMap<String, String>);

pub struct AlternativeName {}

/// Represents a string in a specific language.
pub struct LangString<'a> {
pub iso_code: IsoCode,
pub string: &'a str,
}

/// Language codes according to ISO 639-1
/// Not an exhaustive list.
enum IsoCode {
DE, // German
EN, // English
FR, // French
IT, // Italian
ES, // Spanish
PT, // Portuguese
NL, // Dutch
PL, // Polish
RU, // Russian
JA, // Japanese
ZH, // Chinese
AR, // Arabic
FA, // Persian
}

#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub struct TeaserText<'a>(&'a str);

Expand Down

0 comments on commit 56fbd69

Please sign in to comment.