Skip to content

Commit

Permalink
refactor(dsp-meta): conversion of domain entities / values
Browse files Browse the repository at this point in the history
  • Loading branch information
subotic committed Nov 6, 2023
1 parent 06960cf commit 6262e7a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
4 changes: 3 additions & 1 deletion dsp-meta/src/api/convert/hcl/extracted_project_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use dsp_domain::metadata::value::temporal_coverage::TemporalCoverage;
use dsp_domain::metadata::value::url::Url;
use tracing::warn;

use crate::api::convert::hcl::hcl_block::HclBlock;
use crate::error::DspMetaError;

const ALTERNATIVE_NAME_BLOCK: &str = "alternative_name";
Expand Down Expand Up @@ -47,7 +48,8 @@ impl TryFrom<Vec<&hcl::Block>> for ExtractedProjectBlocks {
for block in blocks {
match block.identifier.as_str() {
ALTERNATIVE_NAME_BLOCK => {
alternative_names.push(AlternativeName::try_from(block)?);
// alternative_names.push(AlternativeName::try_from(block)?);
alternative_names.push(HclBlock(block).try_into()?)
}
DESCRIPTION_BLOCK => {
if description.is_some() {
Expand Down
1 change: 1 addition & 0 deletions dsp-meta/src/api/convert/hcl/hcl_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub struct HclBlock<'a>(pub &'a hcl::Block);
1 change: 1 addition & 0 deletions dsp-meta/src/api/convert/hcl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ mod extracted_project_blocks;
mod project;
pub(crate) mod project_metadata;
mod value;
mod hcl_block;
20 changes: 10 additions & 10 deletions dsp-meta/src/api/convert/hcl/value/alternative_name.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use dsp_domain::metadata::value::alternative_name::AlternativeName;
use dsp_domain::metadata::value::lang_text_data::LangTextData;

use crate::api::convert::hcl::hcl_block::HclBlock;
use crate::error::DspMetaError;

const ALTERNATIVE_NAME_BLOCK_IDENTIFIER: &str = "alternative_name";

impl TryFrom<&hcl::Block> for AlternativeName {
impl<'a> TryInto<AlternativeName> for HclBlock<'a> {
type Error = DspMetaError;

fn try_from(block: &hcl::Block) -> Result<Self, Self::Error> {
if block.identifier.as_str() != ALTERNATIVE_NAME_BLOCK_IDENTIFIER {
fn try_into(self) -> Result<AlternativeName, Self::Error> {
if self.0.identifier.as_str() != ALTERNATIVE_NAME_BLOCK_IDENTIFIER {
let msg = format!(
"The passed block is not named correctly. Expected '{}', however got '{}' instead.",
ALTERNATIVE_NAME_BLOCK_IDENTIFIER,
block.identifier.as_str()
self.0.identifier.as_str()
);
return Err(DspMetaError::CreateValueObject(msg));
}

let attributes: Vec<&hcl::Attribute> = block.body.attributes().collect();
let attributes: Vec<&hcl::Attribute> = self.0.body.attributes().collect();
LangTextData::try_from(attributes).map(|lang_text_data| AlternativeName(lang_text_data.0))
}
}
Expand All @@ -32,7 +33,7 @@ mod tests {
use super::*;

#[test]
fn test_try_from_correct_block() {
fn test_try_into_from_correct_block() {
let block = hcl::block!(
alternative_name {
de = "Der alternative Name"
Expand All @@ -41,8 +42,7 @@ mod tests {
}
);

let alternative_name = AlternativeName::try_from(&block).unwrap();

let alternative_name: AlternativeName = HclBlock(&block).try_into().unwrap();
let mut map: HashMap<IsoCode, String> = HashMap::new();
map.insert(IsoCode::DE, String::from("Der alternative Name"));
map.insert(IsoCode::EN, String::from("The alternative name"));
Expand All @@ -53,7 +53,7 @@ mod tests {
}

#[test]
fn test_try_from_incorrect_block() {
fn test_try_into_from_incorrect_block() {
let block = hcl::block!(
alternative_name_other {
de = "Der alternative Name"
Expand All @@ -62,7 +62,7 @@ mod tests {
}
);

let alternative_name = AlternativeName::try_from(&block);
let alternative_name: Result<AlternativeName, DspMetaError> = HclBlock(&block).try_into();

assert!(alternative_name.is_err());
}
Expand Down
7 changes: 4 additions & 3 deletions dsp-meta/src/api/convert/rdf/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ use sophia::ns::{rdf, Namespace};
use crate::api::convert::rdf::constance::DSP_NAMESPACE_STRING;
use crate::error::Result;

impl Project {
struct ProjectDto(Project);
impl ProjectDto {
pub(crate) fn to_graph(&self) -> Result<LightGraph> {
let mut graph = LightGraph::new();

// http://ns.dasch.swiss/repository#dsp-081C-project a http://ns.dasch.swiss/repository#Project
let dsp = Namespace::new_unchecked(DSP_NAMESPACE_STRING);
let project_iri_suffix = format!("dsp-{}-project", self.shortcode.0);
let project_iri_suffix = format!("dsp-{}-project", self.0.shortcode.0);
let project_iri = dsp.get(&project_iri_suffix)?;

let shortcode_graph = self.shortcode.to_graph(&project_iri)?;
let shortcode_graph = self.0.shortcode.to_graph(&project_iri)?;

graph
.insert(&project_iri, &rdf::type_, &dsp.get("Project")?)
Expand Down
4 changes: 3 additions & 1 deletion dsp-meta/src/api/convert/rdf/value/shortcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use sophia::term::SimpleIri;

use crate::error::Result;

impl Shortcode {
struct ShortcodeDto(Shortcode);

impl ShortcodeDto {
pub fn to_graph(&self, project_iri: &SimpleIri) -> Result<LightGraph> {
let mut graph: LightGraph = LightGraph::new();

Expand Down

0 comments on commit 6262e7a

Please sign in to comment.