#[path = "support.rs"]
mod support;
use noyalib::i18n::{DefaultFormatter, MessageFormatter, UserFormatter};
use noyalib::{Error, Value, from_str};
fn main() {
support::header("noyalib -- i18n_formatters");
support::task_with_output("DefaultFormatter — verbatim Display output", || {
let err = from_str::<Value>("a: [unclosed").unwrap_err();
vec![DefaultFormatter.format(&err)]
});
support::task_with_output("UserFormatter — plain-language sentence", || {
let err = from_str::<Value>("a: [unclosed").unwrap_err();
vec![UserFormatter.format(&err)]
});
support::task_with_output("UserFormatter handles each Error category", || {
let errors: Vec<Error> = vec![
Error::DuplicateKey("api_key".into()),
Error::MissingField("password".into()),
Error::TypeMismatch {
expected: "integer",
found: "string".into(),
},
Error::RecursionLimitExceeded { depth: 256 },
Error::UnknownAnchor("backend-config".into()),
];
errors
.iter()
.map(|e| format!("{:32}{}", format!("{e:.32}"), UserFormatter.format(e)))
.collect()
});
support::task_with_output("Custom formatter — implement MessageFormatter", || {
struct ShoutFormatter;
impl MessageFormatter for ShoutFormatter {
fn format(&self, error: &Error) -> String {
error.to_string().to_uppercase()
}
}
let err = from_str::<Value>("a: [unclosed").unwrap_err();
vec![err.render_with_formatter(&ShoutFormatter)]
});
support::task_with_output(
"Error::render_with_formatter — dispatch entry point",
|| {
let err = from_str::<Value>("a: [unclosed").unwrap_err();
vec![
format!("dev: {}", err.render_with_formatter(&DefaultFormatter)),
format!("user: {}", err.render_with_formatter(&UserFormatter)),
]
},
);
}