From e8ce88188bcec4dcaee755edf8595e1be41fb167 Mon Sep 17 00:00:00 2001 From: Stephan Boyer Date: Sun, 20 Sep 2026 02:43:03 +0800 Subject: [PATCH] Standardize path formatting --- src/error.rs | 8 ++++---- src/format.rs | 12 ++++++++++++ src/main.rs | 7 ++----- src/parser.rs | 6 +++--- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/error.rs b/src/error.rs index 473dc2d..a918a0a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -use crate::format::CodeStr; +use crate::format::CodePath; use colored::{Colorize, control::SHOULD_COLORIZE}; use std::{ cmp::{max, min}, @@ -50,14 +50,14 @@ pub fn throw( 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, ) @@ -66,7 +66,7 @@ pub fn throw( format!( "{} {} {}", "[Error]".red().bold(), - format!("[{}]", path.to_string_lossy().code_str()).magenta(), + format!("[{}]", path.code_path()).magenta(), message, ) } diff --git a/src/format.rs b/src/format.rs index c0f37d0..ed780f7 100644 --- a/src/format.rs +++ b/src/format.rs @@ -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 diff --git a/src/main.rs b/src/main.rs index de5d976..4e7c764 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, @@ -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), diff --git a/src/parser.rs b/src/parser.rs index 6382c13..9d1ba61 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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}, @@ -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, ) @@ -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,