-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: convert project - publication (#42)
- Loading branch information
Showing
7 changed files
with
196 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
normalize_comments = true | ||
reorder_imports = true | ||
# Basic | ||
max_width = 100 | ||
# Imports | ||
group_imports = "StdExternalCrate" | ||
newline_style = "Unix" | ||
imports_granularity = "Module" | ||
reorder_imports = true | ||
# Comments | ||
comment_width = 100 | ||
normalize_comments = true | ||
wrap_comments = true | ||
# Consistency | ||
newline_style = "Unix" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::domain::value::simple_text_data::SimpleTextData; | ||
use crate::errors::DspMetaError; | ||
|
||
const PUBLICATION_BLOCK_IDENTIFIER: &str = "publication"; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum Publication { | ||
SimpleText(SimpleTextData), | ||
} | ||
|
||
impl TryFrom<&hcl::Block> for Publication { | ||
type Error = DspMetaError; | ||
|
||
fn try_from(block: &hcl::Block) -> Result<Self, Self::Error> { | ||
if block.identifier.as_str() != PUBLICATION_BLOCK_IDENTIFIER { | ||
let msg = format!( | ||
"The passed block is not named correctly. Expected 'publication', however got '{}' instead.", | ||
block.identifier.as_str() | ||
); | ||
return Err(DspMetaError::CreateValueObject(msg)); | ||
} | ||
|
||
let attributes: Vec<&hcl::Attribute> = block.body.attributes().collect(); | ||
|
||
SimpleTextData::try_from(attributes).map(Publication::SimpleText) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_try_from_correct_block() { | ||
let block = hcl::block!( | ||
publication { | ||
text = "A publication" | ||
} | ||
); | ||
|
||
let publication = Publication::try_from(&block).unwrap(); | ||
|
||
match publication { | ||
Publication::SimpleText(data) => { | ||
assert_eq!(data, SimpleTextData("A publication".to_string())); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_try_from_incorrect_block() { | ||
let block = hcl::block!( | ||
publication_other { | ||
text = "A publication" | ||
} | ||
); | ||
|
||
let publication = Publication::try_from(&block); | ||
|
||
assert!(publication.is_err()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use tracing::warn; | ||
|
||
use crate::errors::DspMetaError; | ||
|
||
const TEXT_ATTRIBUTE_IDENTIFIER: &str = "text"; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct SimpleTextData(pub String); | ||
|
||
impl TryFrom<Vec<&hcl::Attribute>> for SimpleTextData { | ||
type Error = DspMetaError; | ||
|
||
fn try_from(attributes: Vec<&hcl::Attribute>) -> Result<Self, Self::Error> { | ||
let mut text_attribute_value: Option<String> = None; | ||
|
||
for attribute in attributes { | ||
match attribute.key() { | ||
TEXT_ATTRIBUTE_IDENTIFIER => { | ||
if text_attribute_value.is_some() { | ||
return Err(DspMetaError::CreateValueObject( | ||
"Multiple text attributes are not allowed.".to_string(), | ||
)); | ||
} | ||
text_attribute_value = match attribute.expr() { | ||
hcl::Expression::String(value) => Ok(Some(value.to_owned())), | ||
_ => Err(DspMetaError::CreateValueObject( | ||
"The attribute value is not of String type.".to_string(), | ||
)), | ||
}?; | ||
} | ||
_ => { | ||
warn!("Parse error: unknown attribute '{}'.", attribute.key()); | ||
} | ||
} | ||
} | ||
Ok(SimpleTextData(text_attribute_value.ok_or_else(|| { | ||
DspMetaError::CreateValueObject("Missing text attribute.".to_string()) | ||
})?)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use tracing_test::traced_test; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_try_from_attributes() { | ||
let attribute = hcl::Attribute::new("text", "some text"); | ||
let text_data = SimpleTextData::try_from(vec![&attribute]).unwrap(); | ||
assert_eq!(text_data, SimpleTextData("some text".to_string())); | ||
} | ||
|
||
#[traced_test] | ||
#[test] | ||
fn test_try_from_attributes_missing_text() { | ||
let attribute = hcl::Attribute::new("some_other_attribute", "some text"); | ||
let text_data = SimpleTextData::try_from(vec![&attribute]); | ||
assert!(text_data.is_err()); | ||
assert!(logs_contain( | ||
"Parse error: unknown attribute 'some_other_attribute'" | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_try_from_attributes_multiple_text() { | ||
let attribute = hcl::Attribute::new("text", "some text"); | ||
let attribute2 = hcl::Attribute::new("text", "some text"); | ||
let text_data = SimpleTextData::try_from(vec![&attribute, &attribute2]); | ||
assert!(text_data.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_try_from_attributes_wrong_type() { | ||
let attribute = hcl::Attribute::new("text", 1); | ||
let text_data = SimpleTextData::try_from(vec![&attribute]); | ||
assert!(text_data.is_err()); | ||
} | ||
} |