use std::error::Error;
use std::fmt::{Display, Formatter};
use super::{TemplateEngineException, TemplateProcessingException};
#[derive(Debug)]
pub struct TemplateInputException {
processing: TemplateProcessingException,
}
impl TemplateInputException {
#[must_use]
pub fn new(message: Option<String>) -> Self {
Self {
processing: TemplateProcessingException::new(message),
}
}
pub fn with_cause<E>(message: Option<String>, cause: E) -> Self
where
E: Error + Send + Sync + 'static,
{
Self {
processing: TemplateProcessingException::with_cause(message, cause),
}
}
pub fn with_template_and_cause<E>(
message: Option<String>,
template_name: Option<String>,
cause: E,
) -> Self
where
E: Error + Send + Sync + 'static,
{
Self {
processing: TemplateProcessingException::with_template_and_cause(
message,
template_name,
cause,
),
}
}
#[must_use]
pub fn with_location(
message: Option<String>,
template_name: Option<String>,
line: i32,
col: i32,
) -> Self {
Self {
processing: TemplateProcessingException::with_location(
message,
template_name,
line,
col,
),
}
}
pub fn with_location_and_cause<E>(
message: Option<String>,
template_name: Option<String>,
line: i32,
col: i32,
cause: E,
) -> Self
where
E: Error + Send + Sync + 'static,
{
Self {
processing: TemplateProcessingException::with_location_and_cause(
message,
template_name,
line,
col,
cause,
),
}
}
#[must_use]
pub fn get_message(&self) -> String {
self.processing.get_message()
}
#[must_use]
pub fn get_template_name(&self) -> Option<&str> {
self.processing.get_template_name()
}
#[must_use]
pub fn has_template_name(&self) -> bool {
self.processing.has_template_name()
}
#[must_use]
pub fn get_line(&self) -> Option<i32> {
self.processing.get_line()
}
#[must_use]
pub fn get_col(&self) -> Option<i32> {
self.processing.get_col()
}
#[must_use]
pub fn has_line_and_col(&self) -> bool {
self.processing.has_line_and_col()
}
pub fn set_template_name(&mut self, template_name: Option<String>) {
self.processing.set_template_name(template_name);
}
pub fn set_line_and_col(&mut self, line: i32, col: i32) {
self.processing.set_line_and_col(line, col);
}
}
impl Display for TemplateInputException {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.processing, formatter)
}
}
impl Error for TemplateInputException {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.processing.source()
}
}
impl TemplateEngineException for TemplateInputException {
fn as_processing_exception_mut(&mut self) -> Option<&mut TemplateProcessingException> {
Some(&mut self.processing)
}
}
impl AsRef<TemplateProcessingException> for TemplateInputException {
fn as_ref(&self) -> &TemplateProcessingException {
&self.processing
}
}
impl AsMut<TemplateProcessingException> for TemplateInputException {
fn as_mut(&mut self) -> &mut TemplateProcessingException {
&mut self.processing
}
}
#[cfg(test)]
mod tests {
use std::error::Error;
use std::io;
use super::TemplateInputException;
#[test]
fn maps_every_java_constructor_and_inherited_operation() {
let plain = TemplateInputException::new(Some("input".to_owned()));
assert_eq!(plain.get_message(), "input");
assert!(!plain.has_template_name());
assert!(plain.source().is_none());
let caused =
TemplateInputException::with_cause(Some("input".to_owned()), io::Error::other("cause"));
assert_eq!(
caused.source().map(ToString::to_string),
Some("cause".to_owned())
);
let template_caused = TemplateInputException::with_template_and_cause(
Some("input".to_owned()),
Some("index.html".to_owned()),
io::Error::other("cause"),
);
assert_eq!(template_caused.get_template_name(), Some("index.html"));
assert_eq!(template_caused.get_line(), None);
let located = TemplateInputException::with_location(
Some("input".to_owned()),
Some("index.html".to_owned()),
3,
4,
);
assert_eq!(located.get_line(), Some(3));
assert_eq!(located.get_col(), Some(4));
assert!(located.has_line_and_col());
let mut located_caused = TemplateInputException::with_location_and_cause(
Some("input".to_owned()),
Some("old.html".to_owned()),
5,
6,
io::Error::other("cause"),
);
assert_eq!(located_caused.as_ref().get_line(), Some(5));
located_caused.set_template_name(Some("new.html".to_owned()));
located_caused.set_line_and_col(-1, 8);
located_caused
.as_mut()
.set_template_name(Some("new.html".to_owned()));
assert_eq!(located_caused.get_template_name(), Some("new.html"));
assert_eq!(located_caused.get_line(), None);
assert_eq!(located_caused.get_col(), Some(8));
assert!(!located_caused.has_line_and_col());
assert_eq!(
located_caused.to_string(),
"input (template: \"new.html\" - , col 8)"
);
}
}