From 30b982df05eccff0e28747c8fa1ef71fcc065a74 Mon Sep 17 00:00:00 2001 From: Yago Iglesias Date: Mon, 6 May 2024 17:49:50 +0200 Subject: [PATCH] refactor: Implement Display instead of ToString --- src/path.rs | 21 +++++++++++---------- src/setup.rs | 2 +- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/path.rs b/src/path.rs index d9ce6a1..ad20053 100644 --- a/src/path.rs +++ b/src/path.rs @@ -1,4 +1,5 @@ use std::{ + fmt::Display, fs::read_dir, path::{Path, PathBuf}, }; @@ -40,11 +41,11 @@ impl File { } } -impl ToString for File { - fn to_string(&self) -> String { +impl Display for File { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::Image(image) => image.to_string(), - Self::Folder(folder) => folder.to_string(), + Self::Image(image) => write!(f, "{}", image), + Self::Folder(folder) => write!(f, "{}", folder), } } } @@ -192,9 +193,9 @@ impl ImagePath { } } -impl ToString for ImagePath { - fn to_string(&self) -> String { - self.path.to_str().unwrap().to_owned() +impl Display for ImagePath { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.path.to_str().unwrap()) } } @@ -228,9 +229,9 @@ impl AnimtaedFolder { } } -impl ToString for AnimtaedFolder { - fn to_string(&self) -> String { - self.path.to_str().unwrap().to_owned() +impl Display for AnimtaedFolder { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.path.to_str().unwrap()) } } diff --git a/src/setup.rs b/src/setup.rs index 35bcf52..91cca93 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -21,7 +21,7 @@ pub fn run(settings: Settings, action: Actions) { Err(err) => eprintln!("Error, {}", err), }, Actions::Get => match get_next_wallpaper(&settings) { - Ok(wallpaper) => println!("{}", wallpaper.to_string()), + Ok(wallpaper) => println!("{}", wallpaper), Err(err) => eprintln!("Error, {}", err), }, }