use core::fmt;
use std::path::PathBuf;
use crate::{static_scheme::error::StaticSchemeError, LexerInfo, DynamicSchemeError};
pub struct SerializationErrorPartial
{
pub msg: String
}
impl SerializationErrorPartial
{
pub
fn new(msg: String) -> Self
{
return Self{ msg: msg };
}
}
impl fmt::Display for SerializationErrorPartial
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
write!(f, "{}", self.msg)?;
return Ok(());
}
}
impl fmt::Debug for SerializationErrorPartial
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
write!(f, "{}", self.msg)?;
return Ok(());
}
}
pub type SerializationPartialRes<T> = Result<T, SerializationErrorPartial>;
#[macro_export]
macro_rules! ser_partial_throw
{
($($arg:tt)*) =>
{
return
std::result::Result::Err(
$crate::serializator::error::SerializationErrorPartial::new(format!($($arg)*)
)
)
};
}
#[macro_export]
macro_rules! map_ser_partial_throw
{
($($arg:tt)*) =>
{
$crate::serializator::error::SerializationErrorPartial::new(format!($($arg)*))
};
}
pub struct SerializationError
{
serializ_name: Option<String>,
file_path: Option<PathBuf>,
location: LexerInfo,
msg: String,
}
impl SerializationError
{
pub
fn new(serial_name: Option<&String>, file_path: Option<&PathBuf>, location: &LexerInfo, msg: String) -> Self
{
return
Self
{
serializ_name: serial_name.map(|f| f.clone()),
file_path: file_path.map(|f| f.clone()),
location: location.clone(),
msg: msg
};
}
pub
fn new_dyn(err: DynamicSchemeError) -> Self
{
return
Self
{
serializ_name: None,
file_path: None,
location: LexerInfo::notexistent(),
msg: err.to_string()
};
}
pub
fn new_static_err(err: StaticSchemeError) -> Self
{
return
Self
{
serializ_name: None,
file_path: None,
location: err.location,
msg: err.msg
};
}
pub
fn new_partial_err(err: SerializationErrorPartial) -> Self
{
return
Self
{
serializ_name: None,
file_path: None,
location: LexerInfo::notexistent(),
msg: err.msg
};
}
pub
fn add_name<N>(&mut self, serializ_name: N)
where N: AsRef<str>
{
self.serializ_name = Some(serializ_name.as_ref().to_string());
}
pub
fn add_path(&mut self, file_path: Option<&PathBuf>)
{
self.file_path = file_path.map(|f| f.clone());
}
pub
fn set_location(&mut self, location: &LexerInfo)
{
if self.location.exists() == true
{
return;
}
self.location = location.clone();
}
pub
fn concat_error_msg(&mut self, msg: String)
{
self.msg = [self.msg.as_str(), " ", msg.as_str()].concat();
}
}
impl From<DynamicSchemeError> for SerializationError
{
fn from(err: DynamicSchemeError) -> SerializationError
{
return
SerializationError::new_dyn(err);
}
}
impl From<StaticSchemeError> for SerializationError
{
fn from(err: StaticSchemeError) -> SerializationError
{
return
SerializationError::new_static_err(err);
}
}
impl From<SerializationErrorPartial> for SerializationError
{
fn from(err: SerializationErrorPartial) -> SerializationError
{
return
SerializationError::new_partial_err(err);
}
}
impl fmt::Display for SerializationError
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
write!(f, "[serializator] serializator: '{}', file: '{}', near: '{}', ",
self.serializ_name.as_ref().map_or("unknown", |f| f.as_str()),
self.file_path.as_ref().map_or("unknown".to_string(), |f| f.display().to_string()),
self.location)?;
write!(f, "error: '{}'", self.msg)?;
return Ok(());
}
}
impl fmt::Debug for SerializationError
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
write!(f, "[serializator] serializator: '{}', file: '{}', near: '{}', ",
self.serializ_name.as_ref().map_or("unknown", |f| f.as_str()),
self.file_path.as_ref().map_or("unknown".to_string(), |f| f.display().to_string()),
self.location)?;
write!(f, "error: '{}'", self.msg)?;
return Ok(());
}
}
pub type SerializationRes<T> = Result<T, SerializationError>;
#[macro_export]
macro_rules! serial_throw_text
{
($($arg:tt)*) =>
{
return
std::result::Result::Err(
$crate::serializator::error::SerializationError::new(None, None, &crate::lexer::lexer_info::LexerInfo::notexistent(), format!($($arg)*)
)
)
};
}
#[macro_export]
macro_rules! serial_throw_li
{
($li:expr, $($arg:tt)*) =>
{
return
std::result::Result::Err(
$crate::serializator::error::SerializationError::new(None, None, $li, format!($($arg)*)
)
)
};
}
#[macro_export]
macro_rules! map_serial_throw_all
{
($szname:expr, $file_path:expr, $($arg:tt)*) =>
(
$crate::serializator::error::SerializationError::new(Some($szname), $file_path, &crate::lexer::lexer_info::LexerInfo::notexistent(), format!($($arg)*))
)
}
#[macro_export]
macro_rules! serial_throw_all
{
($szname:expr, $file_path:expr, $($arg:tt)*) =>
{
return
std::result::Result::Err(
$crate::serializator::error::SerializationError::new(Some($szname), $file_path, &crate::lexer::lexer_info::LexerInfo::notexistent(), format!($($arg)*)
)
)
};
}
#[macro_export]
macro_rules! map_serial_throw_add
{
($e:expr, $szname:expr, $file_path:expr) =>
(
{
$e.add_name($szname);
$e.add_path($file_path);
$e
}
)
}
#[macro_export]
macro_rules! map_into_dyn_throw_li
{
($err:expr, $li:expr) =>
{
{
let e: $crate::serializator::error::SerializationError = $err.into();
e.set_location($li);
return e;
}
};
}
#[macro_export]
macro_rules! map_into_dyn_throw_msg
{
($err:expr, $($arg:tt)*) =>
{
{
let mut e: $crate::serializator::error::SerializationError = $err.into();
e.concat_error_msg(format!($($arg)*));
return e;
}
};
}