use core::fmt;
use crate::
{
lexer::lexer_error::LexerError,
serializator::error::SerializationErrorPartial, LexerInfo
};
pub struct StaticSchemeError
{
pub filename: Option<String>,
pub location: LexerInfo,
pub proc_name: Option<String>,
pub args: Option<String>,
pub msg: String,
}
impl StaticSchemeError
{
pub
fn new(filename: Option<String>, location: &LexerInfo, proc_name: Option<&str>, args: Option<String>, msg: String) -> Self
{
return
StaticSchemeError
{
filename,
location: location.clone(),
proc_name: proc_name.map(|f| f.to_string()),
args,
msg
};
}
pub
fn new_ser_partial(err: SerializationErrorPartial) -> Self
{
return
StaticSchemeError
{
filename: None,
location: LexerInfo::notexistent(),
proc_name: None,
args: None,
msg: err.msg
};
}
pub
fn new_lexer(err: LexerError) -> Self
{
return
StaticSchemeError
{
filename: Some(err.file_path.display().to_string()),
location: err.location,
proc_name: None,
args: None,
msg: err.message
};
}
pub
fn concat_error_msg(&mut self, msg: String)
{
self.msg = [self.msg.as_str(), " ", msg.as_str()].concat();
}
pub
fn set_filename<F>(&mut self, filename: F)
where F: AsRef<str>
{
self.filename = Some(filename.as_ref().to_string());
}
pub
fn set_location(&mut self, location: &LexerInfo)
{
if self.location.exists() == true
{
return;
}
self.location = location.clone();
}
pub
fn set_proc_name(&mut self, proc_name: &str)
{
if self.proc_name.is_some() == true
{
return;
}
self.proc_name = Some(proc_name.to_string());
}
}
impl fmt::Display for StaticSchemeError
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
write!(f, "[static_scheme] file: '{}', near: '{}', ",
self.filename.as_ref().map_or("unknown", |f| f.as_str()), self.location)?;
if self.proc_name.is_some() == true
{
write!(f, "procedure: '{}', ", self.proc_name.as_ref().unwrap())?;
}
write!(f, "error: '{}'", self.msg)?;
if self.args.is_some() == true
{
return write!(f, "\n args: '{:?}'", self.args.as_ref().unwrap());
}
else
{
return Ok(());
}
}
}
impl fmt::Debug for StaticSchemeError
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
write!(f, "[static_scheme] file: '{}', near: '{}', ",
self.filename.as_ref().map_or("unknown", |f| f.as_str()), self.location)?;
if self.proc_name.is_some() == true
{
write!(f, "procedure: '{}', ", self.proc_name.as_ref().unwrap())?;
}
write!(f, "error: '{}'", self.msg)?;
if self.args.is_some() == true
{
return write!(f, "\n args: '{:?}'", self.args.as_ref().unwrap());
}
else
{
return Ok(());
}
}
}
impl From<SerializationErrorPartial> for StaticSchemeError
{
fn from(err: SerializationErrorPartial) -> StaticSchemeError
{
return
StaticSchemeError::new_ser_partial(err);
}
}
pub type StaticSchemeRes<T> = Result<T, StaticSchemeError>;
#[macro_export]
macro_rules! map_static_throw_text
{
($err:expr, $($arg:tt)*) =>
(
{
$err.concat_error_msg(format!($($arg)*));
return $err;
}
);
}
#[macro_export]
macro_rules! map_new_static_throw_text
{
($($arg:tt)*) =>
(
$crate::static_scheme::error::StaticSchemeError::new(None, &crate::lexer::lexer_info::LexerInfo::notexistent(), None, None, format!($($arg)*))
);
}
#[macro_export]
macro_rules! map_static_throw
{
($err:expr, $file:expr) =>
(
{
$err.set_filename($file);
return $err;
}
);
($err:expr, $file:expr, $li:expr, $($arg:tt)*) =>
(
{
$err.concat_error_msg(format!($($arg)*));
$err.set_filename($file);
$err.set_location($li);
return $err;
}
);
}
#[macro_export]
macro_rules! map_static_throw_proc
{
($err:expr, $proc_name:expr) =>
{
{
$err.set_proc_name($proc_name);
return $err;
}
};
}
#[macro_export]
macro_rules! map_into_static_throw_li_proc
{
($err:expr, $li:expr, $proc_name:expr) =>
{
{
let mut e: $crate::static_scheme::error::StaticSchemeError = $err.into();
e.set_location($li);
e.set_proc_name($proc_name);
return e;
}
};
}
#[macro_export]
macro_rules! map_static_throw_li_proc
{
($err:expr, $li:expr, $proc_name:expr) =>
{
{
$err.set_location($li);
$err.set_proc_name($proc_name);
return $err;
}
};
}
#[macro_export]
macro_rules! static_throw_proc
{
($li:expr, $proc_name:expr, $($arg:tt)*) =>
{
return
std::result::Result::Err(
$crate::static_scheme::error::StaticSchemeError::new(None, $li, Some($proc_name), None, format!($($arg)*))
)
};
}
#[macro_export]
macro_rules! static_throw_text
{
($($arg:tt)*) =>
{
return
std::result::Result::Err(
$crate::static_scheme::error::StaticSchemeError::new(None, &crate::lexer::lexer_info::LexerInfo::notexistent(), None, None, format!($($arg)*))
)
};
}
#[macro_export]
macro_rules! static_throw
{
($li:expr, $($arg:tt)*) =>
{
return
std::result::Result::Err(
$crate::static_scheme::error::StaticSchemeError::new(None, $li, None, None, format!($($arg)*)
)
)
};
}
#[macro_export]
macro_rules! new_map_static_throw_file
{
($file:expr, $($arg:tt)*) =>
{
$crate::static_scheme::error::StaticSchemeError::new(Some($file), &crate::lexer::lexer_info::LexerInfo::notexistent(), None, None, format!($($arg)*))
};
}
#[macro_export]
macro_rules! static_throw_file
{
($file:expr, $li:expr, $($arg:tt)*) =>
{
return
std::result::Result::Err(
$crate::static_scheme::error::StaticSchemeError::new(Some($file), $li, None, None, format!($($arg)*)
)
)
};
($file:expr, $li:expr, $proc_name:expr, $($arg:tt)*) =>
{
return
std::result::Result::Err(
$crate::static_scheme::error::StaticSchemeError::new(Some($file), $li, Some($proc_name), None, format!($($arg)*)
)
)
};
($file:expr, $li:expr, $proc_name:expr, $args:expr, $($arg:tt)*) =>
{
return
std::result::Result::Err(
$crate::static_scheme::error::StaticSchemeError::new(Some($file), $li, Some($proc_name), Some($args), format!($($arg)*)
)
)
};
}