use proc_macro::Span;
#[derive(Debug)]
pub struct Error
{
msg: String,
#[cfg(feature = "pretty_errors")]
span: Span,
#[cfg(feature = "pretty_errors")]
hint: String,
}
impl Error
{
pub fn new(msg: impl Into<String>) -> Self
{
#[cfg(feature = "pretty_errors")]
{
Self {
msg: msg.into(),
span: Span::call_site(),
hint: "".to_string(),
}
}
#[cfg(not(feature = "pretty_errors"))]
{
Self { msg: msg.into() }
}
}
#[allow(unused_variables)]
#[allow(unused_mut)]
pub fn span(mut self, span: Span) -> Self
{
#[cfg(feature = "pretty_errors")]
{
self.span = span;
}
self
}
#[allow(unused_variables)]
#[allow(unused_mut)]
pub fn hint(mut self, hint: impl Into<String>) -> Self
{
#[cfg(feature = "pretty_errors")]
{
self.hint = hint.into();
}
self
}
pub fn extract(self) -> (Span, String)
{
#[cfg(feature = "pretty_errors")]
{
(
self.span,
if !self.hint.is_empty()
{
self.msg + "\n" + self.hint.as_str()
}
else
{
self.msg
},
)
}
#[cfg(not(feature = "pretty_errors"))]
{
(Span::call_site(), self.msg)
}
}
}