Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::format::CodeStr;
use crate::format::CodePath;
use colored::{Colorize, control::SHOULD_COLORIZE};
use std::{
cmp::{max, min},
Expand Down Expand Up @@ -50,14 +50,14 @@ pub fn throw<T: error::Error + 'static>(
format!(
"{} {} {}",
"[Error]".red().bold(),
format!("[{}]", path.to_string_lossy().code_str()).magenta(),
format!("[{}]", path.code_path()).magenta(),
message,
)
} else {
format!(
"{} {} {}\n\n{}",
"[Error]".red().bold(),
format!("[{}]", path.to_string_lossy().code_str()).magenta(),
format!("[{}]", path.code_path()).magenta(),
message,
listing,
)
Expand All @@ -66,7 +66,7 @@ pub fn throw<T: error::Error + 'static>(
format!(
"{} {} {}",
"[Error]".red().bold(),
format!("[{}]", path.to_string_lossy().code_str()).magenta(),
format!("[{}]", path.code_path()).magenta(),
message,
)
}
Expand Down
12 changes: 12 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
use colored::{ColoredString, Colorize, control::SHOULD_COLORIZE};
use std::path::Path;

// This trait formats a filesystem path for human-facing diagnostic output.
pub trait CodePath {
fn code_path(&self) -> ColoredString;
}

impl CodePath for Path {
fn code_path(&self) -> ColoredString {
self.to_string_lossy().code_str()
}
}

// This trait has a function for formatting "code-like" text, such as a file path. The reason it's
// implemented as a trait and not just a function is so we can use it with method syntax, as in
Expand Down
7 changes: 2 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod unifier;
use crate::{
error::{Error, throw},
evaluator::evaluate,
format::CodeStr,
format::{CodePath, CodeStr},
parser::parse,
tokenizer::tokenize,
type_checker::type_check,
Expand Down Expand Up @@ -124,10 +124,7 @@ fn run(source_path: &Path, check_only: bool) -> Result<(), Error> {
// Read the file.
let source_contents = read_to_string(source_path).map_err(|error| {
throw(
&format!(
"Error when reading file {}.",
source_path.to_string_lossy().code_str(),
),
&format!("Error when reading file {}.", source_path.code_path()),
None,
None,
Some(error),
Expand Down
6 changes: 3 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
error::{Error, SourceRange, listing, throw},
evaluator::is_value,
format::CodeStr,
format::{CodePath, CodeStr},
term,
term::free_variables,
token::{self, TerminatorType, Token},
Expand Down Expand Up @@ -3748,7 +3748,7 @@ fn parse_group<'a>(
"{} {} This parenthesis was never closed:\n\n{}\n\nIt was \
expected to be closed at the end of this line:\n\n{}",
"[Error]".red().bold(),
format!("[{}]", path.to_string_lossy().code_str()).magenta(),
format!("[{}]", path.code_path()).magenta(),
left_parenthesis_listing,
unexpected_token_listing,
)
Expand All @@ -3766,7 +3766,7 @@ fn parse_group<'a>(
"{} {} This parenthesis was never closed:\n\n{}\n\nIt was \
expected to be closed before {}:\n\n{}",
"[Error]".red().bold(),
format!("[{}]", path.to_string_lossy().code_str()).magenta(),
format!("[{}]", path.code_path()).magenta(),
left_parenthesis_listing,
tokens[next].to_string().code_str(),
unexpected_token_listing,
Expand Down
Loading