-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement alternative ErrorReporter for testing
- Loading branch information
Showing
6 changed files
with
145 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,91 @@ | ||
#[derive(Default)] | ||
pub struct ErrorReporter { | ||
pub trait ErrorReporter { | ||
fn error(&mut self, line: usize, message: &str) { | ||
self.report(line, "", message) | ||
} | ||
|
||
fn report(&mut self, line: usize, place: &str, message: &str); | ||
|
||
fn reset(&mut self); | ||
fn had_error(&self) -> bool; | ||
} | ||
|
||
pub struct ConsoleErrorReporter { | ||
had_error: bool, | ||
} | ||
|
||
impl ErrorReporter { | ||
pub fn error(&mut self, line: usize, message: &str) { | ||
self.report(line, "", message) | ||
impl ConsoleErrorReporter { | ||
pub fn new() -> Self { | ||
Self { had_error: false } | ||
} | ||
} | ||
|
||
pub fn report(&mut self, line: usize, place: &str, message: &str) { | ||
impl ErrorReporter for ConsoleErrorReporter { | ||
fn report(&mut self, line: usize, place: &str, message: &str) { | ||
eprintln!("[line {}] Error {}: {}", line, place, message); | ||
self.had_error = true; | ||
} | ||
|
||
pub fn reset(&mut self) { | ||
fn reset(&mut self) { | ||
self.had_error = false | ||
} | ||
pub fn had_error(&self) -> bool { | ||
fn had_error(&self) -> bool { | ||
self.had_error | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
pub mod testing { | ||
use super::ErrorReporter; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct Logline { | ||
pub line: usize, | ||
pub place: String, | ||
pub message: String, | ||
} | ||
|
||
impl Logline { | ||
pub fn new(line: usize, place: &str, message: &str) -> Self { | ||
Self { | ||
line, | ||
place: place.into(), | ||
message: message.into(), | ||
} | ||
} | ||
} | ||
|
||
pub struct VectorErrorReporter { | ||
had_error: bool, | ||
errors: Vec<Logline>, | ||
} | ||
|
||
impl VectorErrorReporter { | ||
pub fn new() -> Self { | ||
Self { | ||
had_error: false, | ||
errors: Vec::new(), | ||
} | ||
} | ||
pub fn errors(&self) -> &Vec<Logline> { | ||
&self.errors | ||
} | ||
} | ||
|
||
impl ErrorReporter for VectorErrorReporter { | ||
fn report(&mut self, line: usize, place: &str, message: &str) { | ||
self.errors.push(Logline { | ||
line, | ||
place: place.into(), | ||
message: message.into(), | ||
}); | ||
self.had_error = true; | ||
} | ||
|
||
fn reset(&mut self) { | ||
self.had_error = false | ||
} | ||
fn had_error(&self) -> bool { | ||
self.had_error | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters