You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.2 KiB
72 lines
1.2 KiB
//! Error contexts
|
|
use super::*;
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, Default)]
|
|
pub struct Context
|
|
{
|
|
file: Option<&'static str>,
|
|
line: Option<u32>,
|
|
column: Option<u32>,
|
|
}
|
|
|
|
impl Context
|
|
{
|
|
/// Create a new populated context
|
|
pub const fn with_all(file: &'static str, line: u32, column: u32) -> Self
|
|
{
|
|
Self {
|
|
file: Some(file),
|
|
line: Some(line),
|
|
column: Some(column)
|
|
}
|
|
}
|
|
|
|
pub fn is_empty(&self) -> bool
|
|
{
|
|
self.column.is_none() &&
|
|
self.line.is_none() &&
|
|
self.file.is_none()
|
|
}
|
|
|
|
|
|
}
|
|
|
|
impl fmt::Display for Context
|
|
{
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
{
|
|
if self.is_empty() {
|
|
write!(f, "<unbound>")
|
|
} else {
|
|
if let Some(file) = self.file {
|
|
write!(f, "{} ", file)
|
|
} else {
|
|
write!(f, "? ")
|
|
}?;
|
|
if let Some(line) = self.line {
|
|
write!(f, "{}:", line)
|
|
} else {
|
|
write!(f, "?:")
|
|
}?;
|
|
if let Some(column) = self.column {
|
|
write!(f, "{}", column)
|
|
} else {
|
|
write!(f, "?")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct ContextMessage<D>(pub(super) D, pub(super) Context)
|
|
where D: fmt::Display;
|
|
|
|
impl<D> ContextMessage<D>
|
|
where D: fmt::Display
|
|
{
|
|
pub const fn new(ctx: Context, msg: D) -> Self
|
|
{
|
|
Self(msg, ctx)
|
|
}
|
|
}
|
|
|