From c0cb868dc5d74a17f527b74c318238540b7d3398 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 21 Nov 2024 08:00:15 +0000 Subject: [PATCH] fix: impl Display for path, file and dir --- examples/get.rs | 6 ++---- src/fs/dir.rs | 8 ++++++++ src/fs/file.rs | 10 +++++++++- src/fs/path.rs | 7 +++++++ tests/fs.rs | 37 +++++++++++++++++++++++-------------- 5 files changed, 49 insertions(+), 19 deletions(-) diff --git a/examples/get.rs b/examples/get.rs index ef6dc90..1ef946d 100644 --- a/examples/get.rs +++ b/examples/get.rs @@ -35,9 +35,7 @@ async fn main() -> kxio::Result<()> { // Checks if the path exists (whether a file, directory, etc) if path.exists()? { - // extracts the path from the handle - let pathbuf = path.as_pathbuf(); - eprintln!("The file {} already exists. Aborting!", pathbuf.display()); + eprintln!("The file {path} already exists. Aborting!"); return Ok(()); } @@ -79,9 +77,9 @@ async fn download_and_save_to_file( let body = response.text().await?; println!("fetched {} bytes", body.bytes().len()); - println!("writing file: {}", file_path.display()); // Uses the file system abstraction to create a handle for a file. let file: kxio::fs::PathReal = fs.file(file_path); + println!("writing file: {file}"); // Writes the body to the file. file.write(body)?; diff --git a/src/fs/dir.rs b/src/fs/dir.rs index 8121ca1..194d31e 100644 --- a/src/fs/dir.rs +++ b/src/fs/dir.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + // use crate::fs::{DirItem, DirItemIterator, Result}; @@ -106,3 +108,9 @@ impl TryFrom> for DirHandle { } } } + +impl Display for DirHandle { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}/", self.as_pathbuf().display()) + } +} diff --git a/src/fs/file.rs b/src/fs/file.rs index cd4b8d7..d96a205 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -1,7 +1,9 @@ +use std::fmt::Display; + // use crate::fs::Result; -use super::{reader::Reader, Error, FileHandle, PathHandle, PathMarker}; +use super::{reader::Reader, Error, FileHandle, FileMarker, PathHandle, PathMarker, PathReal}; impl FileHandle { /// Returns a [Reader] for the file. @@ -112,3 +114,9 @@ impl TryFrom> for FileHandle { } } } + +impl Display for PathReal { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.as_pathbuf().display()) + } +} diff --git a/src/fs/path.rs b/src/fs/path.rs index aa2ea8c..f9e38e4 100644 --- a/src/fs/path.rs +++ b/src/fs/path.rs @@ -1,5 +1,6 @@ // use std::{ + fmt::Display, marker::PhantomData, path::{Path, PathBuf}, }; @@ -367,3 +368,9 @@ impl From for PathHandle { PathReal::new(file.base, file.path) } } + +impl Display for PathReal { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.as_pathbuf().display()) + } +} diff --git a/tests/fs.rs b/tests/fs.rs index 6e64ab9..65dafd8 100644 --- a/tests/fs.rs +++ b/tests/fs.rs @@ -16,6 +16,14 @@ mod path { assert_eq!(read.base(), fs.base()); } + + #[test] + fn display() { + let fs = fs::temp().expect("temp fs"); + let path = fs.base().join("foo"); + let path_handle = fs.path(&path); + assert_eq!(path_handle.to_string(), format!("{}", path.display())); + } mod is_link { use super::*; @@ -456,20 +464,13 @@ mod path { mod file { use super::*; - // // test for reading the symlink metadata - // #[test] - // fn symlink_metadata() -> TestResult { - // let fs = fs::temp().expect("temp fs"); - // let file_path = fs.base().join("foo"); - // let file = fs.file(&file_path); - // file.write("bar").expect("write"); - // let link_path = fs.base().join("bar"); - // let link = fs.path(&link_path); - // file.soft_link(&link).expect("soft_link"); - // let md = link.symlink_metadata().expect("symlink metadata"); - // assert!(md.is_file()); - // Ok(()) - // } + #[test] + fn display() { + let fs = fs::temp().expect("temp fs"); + let path = fs.base().join("foo"); + let file_handle = fs.file(&path); + assert_eq!(file_handle.to_string(), format!("{}", path.display())); + } #[test] fn create_hard_link() -> TestResult { @@ -793,6 +794,14 @@ mod dir { use super::*; + #[test] + fn display() { + let fs = fs::temp().expect("temp fs"); + let path = fs.base().join("foo"); + let dir_handle = fs.dir(&path); + assert_eq!(dir_handle.to_string(), format!("{}/", path.display())); + } + #[test] fn path_is_dir() { let fs = fs::temp().expect("temp fs");