Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
60adc5019This file is pinned to the selected release. Download the original to open it in your design tool.
//! Errors generated by the compiler.
use super::{Id, InfoIdx};
use itertools::Itertools;
#[derive(PartialEq, Eq, Hash)]
pub struct Error {
pub kind: String,
pub notes: Vec<InfoIdx>,
}
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}: {}",
self.kind,
self.notes
.iter()
.map(|n| format!("{:?}", n))
.collect_vec()
.join(", ")
)
}
}
impl Error {
pub fn add_note(mut self, note: InfoIdx) -> Self {
self.notes.push(note);
self
}
pub fn invalid_file(f: String) -> Self {
Self {
kind: format!("invalid file: {}", f),
notes: vec![],
}
}
pub fn write_error(e: String) -> Self {
Self {
kind: format!("failed to write output: {}", e),
notes: vec![],
}
}
pub fn malformed<S: ToString>(msg: S) -> Self {
Self {
kind: msg.to_string(),
notes: vec![],
}
}
pub fn undefined<I: ToString, S: ToString>(name: I, kind: S) -> Self {
Self {
kind: format!(
"undefined {} name: {}",
kind.to_string(),
name.to_string(),
),
notes: vec![],
}
}
pub fn already_bound<S: ToString>(name: Id, kind: S) -> Self {
Self {
kind: format!(
"name `{name}' is already bound by {}",
kind.to_string()
),
notes: vec![],
}
}
pub fn misc(msg: String) -> Self {
Self {
kind: msg,
notes: vec![],
}
}
}
/// Convience wrapper to represent success or meaningul compiler error.
pub type FilamentResult<T> = std::result::Result<T, Error>;
// Conversions from other error types to our error type so that
// we can use `?` in all the places.
impl From<std::str::Utf8Error> for Error {
fn from(err: std::str::Utf8Error) -> Self {
Error::invalid_file(err.to_string())
}
}
impl From<std::io::Error> for Error {
fn from(_e: std::io::Error) -> Self {
Error::write_error("IO Error".to_string())
}
}