use crate::span::Span;
use crate::traits::{ErrorCatalog, ErrorEntry};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Severity {
Error,
Warning,
Info,
}
#[derive(Debug, Clone)]
pub struct RuneError {
pub code: String,
pub message: String,
pub span: Option<Span>,
pub severity: Severity,
pub reference: Option<String>,
pub suggestion: Option<String>,
}
impl RuneError {
pub fn parse(message: impl Into<String>, span: Span) -> Self {
Self {
code: "RUNE_E001".into(),
message: message.into(),
span: Some(span),
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn namespace(message: impl Into<String>, span: Option<Span>) -> Self {
Self {
code: "RUNE_E002".into(),
message: message.into(),
span,
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn module_not_found(message: impl Into<String>, span: Option<Span>) -> Self {
Self {
code: "RUNE_E003".into(),
message: message.into(),
span,
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn ambiguous_verb(message: impl Into<String>, span: Option<Span>) -> Self {
Self {
code: "RUNE_E004".into(),
message: message.into(),
span,
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn version_mismatch(message: impl Into<String>, span: Option<Span>) -> Self {
Self {
code: "RUNE_E005".into(),
message: message.into(),
span,
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn type_error(message: impl Into<String>, span: Option<Span>) -> Self {
Self {
code: "RUNE_E006".into(),
message: message.into(),
span,
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn division_by_zero(span: Option<Span>) -> Self {
Self {
code: "RUNE_E007".into(),
message: "Division by zero".into(),
span,
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn undefined_variable(name: &str, span: Option<Span>) -> Self {
Self {
code: "RUNE_E008".into(),
message: format!("Undefined variable '{}'", name),
span,
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn method_not_found(method: &str, type_name: &str, span: Option<Span>) -> Self {
Self {
code: "RUNE_E009".into(),
message: format!("No method '{}' on type '{}'", method, type_name),
span,
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn unresolved(message: impl Into<String>, span: Option<Span>) -> Self {
Self {
code: "RUNE_E010".into(),
message: message.into(),
span,
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn argument(message: impl Into<String>, span: Option<Span>) -> Self {
Self {
code: "RUNE_E011".into(),
message: message.into(),
span,
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn log_unavailable(namespace: &str, span: Option<Span>) -> Self {
Self {
code: "RUNE_E012".into(),
message: format!("No logger registered for namespace '{}'", namespace),
span,
severity: Severity::Error,
reference: None,
suggestion: Some("Register a logger on the namespace via NamespaceScope::logger()".into()),
}
}
pub fn class_not_found(name: &str, span: Option<Span>) -> Self {
Self {
code: "RUNE_E013".into(),
message: format!("Class '{}' is not defined", name),
span,
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn field_required(class: &str, field: &str, span: Option<Span>) -> Self {
Self {
code: "RUNE_E014".into(),
message: format!("Field '{}' is required by class '{}'", field, class),
span,
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn unknown_field(class: &str, field: &str, span: Option<Span>) -> Self {
Self {
code: "RUNE_E015".into(),
message: format!("Class '{}' has no field '{}'", class, field),
span,
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn field_not_found(class: &str, field: &str, span: Option<Span>) -> Self {
Self {
code: "RUNE_E016".into(),
message: format!("No field '{}' on instance of '{}'", field, class),
span,
severity: Severity::Error,
reference: None,
suggestion: None,
}
}
pub fn with_reference(mut self, reference: impl Into<String>) -> Self {
self.reference = Some(reference.into());
self
}
pub fn with_suggestion(mut self, suggestion: impl Into<String>) -> Self {
self.suggestion = Some(suggestion.into());
self
}
}
impl fmt::Display for RuneError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let level = match self.severity {
Severity::Error => "error",
Severity::Warning => "warning",
Severity::Info => "info",
};
write!(f, "{}[{}]: {}", level, self.code, self.message)?;
if let Some(span) = &self.span {
write!(f, " at {}", span)?;
}
Ok(())
}
}
impl std::error::Error for RuneError {}
pub type RuneResult<T> = Result<T, RuneError>;
pub struct RuneErrorCatalog {
entries: Vec<ErrorEntry>,
}
impl RuneErrorCatalog {
pub fn new() -> Self {
Self {
entries: vec![
ErrorEntry { code: "RUNE_E001".into(), prefix: "RUNE".into(), title: "Parse error".into(), template: "{message}".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E002".into(), prefix: "RUNE".into(), title: "Namespace error".into(), template: "{message}".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E003".into(), prefix: "RUNE".into(), title: "Module not found".into(), template: "{message}".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E004".into(), prefix: "RUNE".into(), title: "Ambiguous verb".into(), template: "{message}".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E005".into(), prefix: "RUNE".into(), title: "Version mismatch".into(), template: "{message}".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E006".into(), prefix: "RUNE".into(), title: "Type error".into(), template: "{message}".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E007".into(), prefix: "RUNE".into(), title: "Division by zero".into(), template: "Division by zero".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E008".into(), prefix: "RUNE".into(), title: "Undefined variable".into(), template: "Undefined variable '{name}'".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E009".into(), prefix: "RUNE".into(), title: "Method not found".into(), template: "No method '{method}' on type '{type}'".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E010".into(), prefix: "RUNE".into(), title: "Unresolved error".into(), template: "{message}".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E011".into(), prefix: "RUNE".into(), title: "Argument error".into(), template: "{message}".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E012".into(), prefix: "RUNE".into(), title: "Log unavailable".into(), template: "No logger registered for namespace '{namespace}'".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E013".into(), prefix: "RUNE".into(), title: "Class not found".into(), template: "Class '{name}' is not defined".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E014".into(), prefix: "RUNE".into(), title: "Field required".into(), template: "Field '{field}' is required by class '{class}'".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E015".into(), prefix: "RUNE".into(), title: "Unknown field".into(), template: "Class '{class}' has no field '{field}'".into(), severity: Severity::Error, reference: None },
ErrorEntry { code: "RUNE_E016".into(), prefix: "RUNE".into(), title: "Field not found".into(), template: "No field '{field}' on instance of '{class}'".into(), severity: Severity::Error, reference: None },
],
}
}
}
impl Default for RuneErrorCatalog {
fn default() -> Self {
Self::new()
}
}
impl ErrorCatalog for RuneErrorCatalog {
fn entries(&self) -> &[ErrorEntry] {
&self.entries
}
fn get(&self, code: &str) -> Option<&ErrorEntry> {
self.entries.iter().find(|e| e.code == code)
}
fn render(&self, code: &str, params: &[(&str, &str)]) -> String {
match self.get(code) {
Some(entry) => {
let mut result = entry.template.clone();
for (key, val) in params {
result = result.replace(&format!("{{{}}}", key), val);
}
result
}
None => format!("[{}] Unknown error code", code),
}
}
}
pub struct CompositeErrorCatalog {
catalogs: Vec<Box<dyn ErrorCatalog>>,
}
impl CompositeErrorCatalog {
pub fn new(catalogs: Vec<Box<dyn ErrorCatalog>>) -> Self {
Self { catalogs }
}
}
impl ErrorCatalog for CompositeErrorCatalog {
fn entries(&self) -> &[ErrorEntry] {
self.catalogs.first().map(|c| c.entries()).unwrap_or(&[])
}
fn get(&self, code: &str) -> Option<&ErrorEntry> {
for catalog in &self.catalogs {
if let Some(entry) = catalog.get(code) {
return Some(entry);
}
}
None
}
fn render(&self, code: &str, params: &[(&str, &str)]) -> String {
for catalog in &self.catalogs {
if catalog.get(code).is_some() {
return catalog.render(code, params);
}
}
format!("[{}] Unknown error code", code)
}
}