Skip to content

Commit

Permalink
refactor(mrml-cli): use error attributes
Browse files Browse the repository at this point in the history
Signed-off-by: Jérémie Drouet <[email protected]>
  • Loading branch information
jdrouet committed Aug 2, 2024
1 parent 20089e2 commit 5c3164b
Showing 1 changed file with 60 additions and 21 deletions.
81 changes: 60 additions & 21 deletions packages/mrml-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Cow;
use std::collections::HashSet;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::iter::FromIterator;
Expand All @@ -17,7 +18,11 @@ use mrml::prelude::print::Printable;
use mrml::prelude::render::RenderOptions;

fn format_parser_error(error: ParserError) -> String {
format!("couldn't parse document: {error}")
if let Some(src) = error.source() {
format!("{error}: {src}")
} else {
format!("{error}")
}
}

#[derive(ValueEnum, Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -248,6 +253,8 @@ fn main() {
mod tests {
use clap::Parser;

use crate::format_parser_error;

use super::Options;
use mrml::prelude::parser::{loader::IncludeLoaderError, Error as ParserError, Origin, Span};

Expand All @@ -264,71 +271,93 @@ mod tests {
#[test]
fn format_parser_error_end_of_stream_in_root() {
assert_eq!(
ParserError::EndOfStream(Origin::Root).to_string(),
format_parser_error(ParserError::EndOfStream {
origin: Origin::Root
}),
"unexpected end of stream in root template"
);
}

#[test]
fn format_parser_error_end_of_stream_in_include() {
assert_eq!(
ParserError::EndOfStream(origin_include()).to_string(),
format_parser_error(ParserError::EndOfStream {
origin: origin_include()
}),
"unexpected end of stream in template from \"foo.mjml\""
);
}

#[test]
fn format_parser_error_unexpected_element_in_root() {
assert_eq!(
ParserError::UnexpectedElement(Origin::Root, any_span()).to_string(),
format_parser_error(ParserError::UnexpectedElement {
origin: Origin::Root,
position: any_span()
}),
"unexpected element in root template at position 10..20"
);
}

#[test]
fn format_parser_error_unexpected_element_in_include() {
assert_eq!(
ParserError::UnexpectedElement(origin_include(), any_span()).to_string(),
format_parser_error(ParserError::UnexpectedElement {
origin: origin_include(),
position: any_span()
}),
"unexpected element in template from \"foo.mjml\" at position 10..20"
);
}

#[test]
fn format_parser_error_invalid_attribute_in_root() {
assert_eq!(
ParserError::InvalidAttribute(Origin::Root, any_span()).to_string(),
format_parser_error(ParserError::InvalidAttribute {
origin: Origin::Root,
position: any_span()
}),
"invalid attribute in root template at position 10..20"
);
}

#[test]
fn format_parser_error_invalid_attribute_in_include() {
assert_eq!(
ParserError::InvalidAttribute(origin_include(), any_span()).to_string(),
format_parser_error(ParserError::InvalidAttribute {
origin: origin_include(),
position: any_span()
}),
"invalid attribute in template from \"foo.mjml\" at position 10..20"
);
}

#[test]
fn format_parser_error_invalid_format_in_root() {
assert_eq!(
ParserError::InvalidFormat(Origin::Root, any_span()).to_string(),
format_parser_error(ParserError::InvalidFormat {
origin: Origin::Root,
position: any_span()
}),
"invalid format in root template at position 10..20"
);
}

#[test]
fn format_parser_error_invalid_format_in_include() {
assert_eq!(
ParserError::InvalidFormat(origin_include(), any_span()).to_string(),
format_parser_error(ParserError::InvalidFormat {
origin: origin_include(),
position: any_span()
}),
"invalid format in template from \"foo.mjml\" at position 10..20"
);
}

#[test]
fn format_parser_error_include_loader_error_in_root() {
assert_eq!(
ParserError::IncludeLoaderError {
format_parser_error(ParserError::IncludeLoaderError {
origin: Origin::Root,
position: any_span(),
source: IncludeLoaderError {
Expand All @@ -337,16 +366,15 @@ mod tests {
message: None,
cause: None,
}
}
.to_string(),
}),
"unable to load included template in root template at position 10..20: foo.mjml entity not found"
);
}

#[test]
fn format_parser_error_include_loader_error_in_include() {
assert_eq!(
ParserError::IncludeLoaderError {
format_parser_error(ParserError::IncludeLoaderError {
origin: Origin::Root,
position: any_span(),
source: IncludeLoaderError {
Expand All @@ -355,40 +383,51 @@ mod tests {
message: None,
cause: None,
}
}
.to_string(),
}),
"unable to load included template in root template at position 10..20: foo.mjml entity not found"
);
}

#[test]
fn format_parser_error_missing_attribute_in_root() {
assert_eq!(
ParserError::MissingAttribute("name", Origin::Root, any_span()).to_string(),
"missing attribute name in element in root template at position 10..20"
format_parser_error(ParserError::MissingAttribute {
name: "name",
origin: Origin::Root,
position: any_span()
}),
"missing attribute \"name\" in element in root template at position 10..20"
);
}

#[test]
fn format_parser_error_missing_attribute_in_include() {
assert_eq!(
ParserError::MissingAttribute("name", origin_include(), any_span()).to_string(),
"missing attribute name in element in template from \"foo.mjml\" at position 10..20"
format_parser_error(ParserError::MissingAttribute {
name: "name",
origin: origin_include(),
position: any_span()
}),
"missing attribute \"name\" in element in template from \"foo.mjml\" at position 10..20"
);
}

#[test]
fn format_parser_error_size_limit_in_root() {
assert_eq!(
ParserError::SizeLimit(Origin::Root).to_string(),
format_parser_error(ParserError::SizeLimit {
origin: Origin::Root
}),
"size limit reached in root template"
);
}

#[test]
fn format_parser_error_size_limit_in_include() {
assert_eq!(
ParserError::SizeLimit(origin_include()).to_string(),
format_parser_error(ParserError::SizeLimit {
origin: origin_include()
}),
"size limit reached in template from \"foo.mjml\""
);
}
Expand Down

0 comments on commit 5c3164b

Please sign in to comment.