Skip to content

Commit

Permalink
implemented display for most errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed Jun 25, 2024
1 parent f53e244 commit 724ac08
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 17 deletions.
29 changes: 29 additions & 0 deletions src/cli/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::fmt::{Display, Formatter};
use crate::compiler::ast::Error;

#[derive(Debug)]
pub(crate) enum CliError {
UnknownCommand,
CantCreateWatcher(notify::Error),
CantStartWatcher(notify::Error),
CantCrateOutputFile(std::io::Error),
CantReadInputFile(std::io::Error),
AstParsingFailed(Error),
CompilationError(crate::compiler::CompilerError),
}

impl Display for CliError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
CliError::UnknownCommand => write!(f, "Unknown command"),
CliError::CantCreateWatcher(e) => write!(f, "Can't create watcher: {}", e),
CliError::CantStartWatcher(e) => write!(f, "Can't start watcher: {}", e),
CliError::CantCrateOutputFile(e) => write!(f, "Can't create output file: {}", e),
CliError::CantReadInputFile(e) => write!(f, "Can't read input file: {}", e),
CliError::AstParsingFailed(e) => write!(f, "Ast parsing error: {}", e),
CliError::CompilationError(e) => write!(f, "Compilation error: {}", e),
}
}
}

impl std::error::Error for CliError {}
19 changes: 5 additions & 14 deletions src/cli.rs → src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::{Debug};
use std::fmt::Debug;
use std::fs::File;
use std::io::Write;
use std::panic::catch_unwind;
Expand All @@ -13,7 +13,9 @@ use notify::EventKind::Modify;
use notify::{Event, RecursiveMode, Watcher};

use crate::compiler::{FileCompilerSource, HexoCompiler, HexoCompilerContext};
use crate::compiler::ast::Error;
pub(crate) use error::CliError;

mod error;

#[derive(Parser)]
#[command(version, about, long_about = None)]
Expand Down Expand Up @@ -43,17 +45,6 @@ enum Commands {
},
}

#[derive(Debug)]
pub(crate) enum CliError {
UnknownCommand,
CantCreateWatcher(notify::Error),
CantStartWatcher(notify::Error),
CantCrateOutputFile(std::io::Error),
CantReadInputFile(std::io::Error),
AstParsingFailed(Error),
CompilationError(crate::compiler::CompilerError),
}

pub(crate) fn run_cli() {
let cli = Cli::parse();

Expand Down Expand Up @@ -98,7 +89,7 @@ fn run_watch(source: String, output: Option<String>) -> Result<(), CliError> {
let mut watcher = notify::recommended_watcher(move |event: Result<Event, _>| {
run_watch_loop(source.clone(), output.clone(), event)
})
.map_err(CliError::CantCreateWatcher)?;
.map_err(CliError::CantCreateWatcher)?;

watcher
.watch(source_path, RecursiveMode::NonRecursive)
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/ast/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use crate::compiler::ast::parser::Rule;

#[derive(Debug)]
pub(crate) enum Error {
PestError(pest::error::Error<Rule>),
Pest(pest::error::Error<Rule>),
UnknownRule { rule_name: String },
}

impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::PestError(e) => write!(f, "Parsing error: {}", e),
Error::Pest(e) => write!(f, "Parsing error: {}", e),
Error::UnknownRule { rule_name } => write!(f, "Unknown rule: {}", rule_name),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/ast/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl AstParser {

pub(crate) fn parse(&self, source: String) -> Result<AstNode, Error> {
let pairs = AstPestParser::parse(Rule::file, source.as_str())
.map_err(Error::PestError)?;
.map_err(Error::Pest)?;

let children: Result<Vec<AstNode>, _> = pairs
.map(parse_ast_pair)
Expand Down
12 changes: 12 additions & 0 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ pub(crate) enum CompilerError {
RST(RstCompilerError),
}

impl std::fmt::Display for CompilerError {

fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CompilerError::IO(e) => write!(f, "IO error: {}", e),
CompilerError::AST(e) => write!(f, "AST error: {}", e),
CompilerError::CST(e) => write!(f, "CST error: {}", e),
CompilerError::RST(e) => write!(f, "RST error: {}", e),
}
}
}

pub(crate) struct HexoCompiler {
context: HexoCompilerContext,
}
Expand Down
13 changes: 13 additions & 0 deletions src/compiler/native_fn/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ pub(crate) enum NativeFunctionError {
MissingArgument { name: String, available_arguments: Vec<String>, function_name: String },
}

impl std::fmt::Display for NativeFunctionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NativeFunctionError::Unknown(name) => write!(f, "Unknown error: {}", name),
NativeFunctionError::MissingArgument { name, available_arguments, function_name } => {
write!(f, "Missing argument {} for function {}. Available arguments: {:?}",
name, function_name, available_arguments
)
}
}
}
}

#[derive(Clone, Debug)]
pub(crate) struct NativeFunctionSignature {
pub name: String,
Expand Down
16 changes: 16 additions & 0 deletions src/compiler/rst/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ pub(crate) enum RstCompilerError {
NativeFunctionExecution(NativeFunctionError),
}

impl std::fmt::Display for RstCompilerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RstCompilerError::UnresolvedConstant { name } => {
write!(f, "Unresolved constant: {}", name)
}
RstCompilerError::UnresolvedFunction { name } => {
write!(f, "Unresolved function: {}", name)
}
RstCompilerError::NativeFunctionExecution(e) => {
write!(f, "Native function execution error: {}", e)
}
}
}
}

pub(crate) struct RstCompiler<'a> {
parent: &'a HexoCompiler,
}
Expand Down

0 comments on commit 724ac08

Please sign in to comment.