From 406cb22a746c823895dc1875a43ea7e09d47ac37 Mon Sep 17 00:00:00 2001 From: Stephan Boyer Date: Wed, 7 Oct 2026 02:47:13 -0700 Subject: [PATCH 1/2] Merge the code formatting traits into one `CodePath` and `CodeStr` each had a single method for formatting code-like text for diagnostics. They're now one trait, `CodeStr`, with an impl for each type it formats, so call sites use `code_str()` whether they're formatting a string or a path. --- src/format.rs | 17 ++++++----------- src/run.rs | 7 ++----- src/state.rs | 6 +++--- 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/format.rs b/src/format.rs index ed780f7..578e3a0 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,17 +1,6 @@ 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 // `x.code_str()`. Rust does not allow us to implement methods on primitive types such as `str`. @@ -31,6 +20,12 @@ impl CodeStr for str { } } +impl CodeStr for Path { + fn code_str(&self) -> ColoredString { + self.to_string_lossy().code_str() + } +} + #[cfg(test)] mod tests { use crate::format::CodeStr; diff --git a/src/run.rs b/src/run.rs index 5cf17ce..be0b443 100644 --- a/src/run.rs +++ b/src/run.rs @@ -5,7 +5,7 @@ use crate::{ }; #[cfg(target_os = "linux")] -use crate::format::CodePath; +use crate::format::CodeStr; use byte_unit::{Byte, UnitType}; use chrono::{DateTime, Utc}; use regex::RegexSet; @@ -367,10 +367,7 @@ fn get_disk_by_file<'a>(disks: &'a [Disk], path: &Path) -> io::Result<&'a Disk> .filter(|d| path.starts_with(d.mount_point())) .max_by_key(|d| d.mount_point().as_os_str().len()) .ok_or_else(|| { - io::Error::other(format!( - "Unable to find disk for path {}.", - path.code_path(), - )) + io::Error::other(format!("Unable to find disk for path {}.", path.code_str())) }) } diff --git a/src/state.rs b/src/state.rs index 8e61857..9b0f566 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,4 +1,4 @@ -use crate::format::CodePath; +use crate::format::CodeStr; use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, @@ -57,7 +57,7 @@ pub fn load() -> io::Result { // Log what we are trying to do in case an error occurs. trace!( "Attempting to load the state from {}\u{2026}", - path.code_path(), + path.code_str(), ); // Read the YAML from disk. @@ -76,7 +76,7 @@ pub fn save(state: &State) -> io::Result<()> { // Check if we have a path. if let Some(path) = path() { // Log what we're trying to do in case an error occurs. - trace!("Persisting the state to {}\u{2026}", path.code_path()); + trace!("Persisting the state to {}\u{2026}", path.code_str()); // The `unwrap` is safe due to [ref:state_path_has_parent]. let parent = path.parent().unwrap().to_owned(); From afe4d6c8bb9b8805e5dfe1ab175559f67402372e Mon Sep 17 00:00:00 2001 From: Stephan Boyer Date: Wed, 7 Oct 2026 02:55:04 -0700 Subject: [PATCH 2/2] Remove a duplicate import on Linux The Linux-only import of `CodePath` became a second import of `CodeStr`, which the module already imports on every platform. --- src/run.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/run.rs b/src/run.rs index be0b443..57ef645 100644 --- a/src/run.rs +++ b/src/run.rs @@ -3,9 +3,6 @@ use crate::{ format::CodeStr, state::{self, State}, }; - -#[cfg(target_os = "linux")] -use crate::format::CodeStr; use byte_unit::{Byte, UnitType}; use chrono::{DateTime, Utc}; use regex::RegexSet;