fix: impl Display for path, file and dir
Some checks failed
Rust / build (map[name:stable]) (push) Failing after 45s
Rust / build (map[name:nightly]) (push) Failing after 1m38s

This commit is contained in:
Paul Campbell 2024-11-21 08:00:15 +00:00
parent a58796f2f0
commit 84b3303c80
5 changed files with 49 additions and 21 deletions

View file

@ -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<kxio::fs::FileMarker> = fs.file(file_path);
println!("writing file: {file}");
// Writes the body to the file.
file.write(body)?;

View file

@ -1,3 +1,5 @@
use std::fmt::Display;
//
use crate::fs::{DirItem, DirItemIterator, Result};
@ -106,3 +108,9 @@ impl TryFrom<PathHandle<PathMarker>> for DirHandle {
}
}
}
impl Display for DirHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}/", self.as_pathbuf().display())
}
}

View file

@ -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<PathHandle<PathMarker>> for FileHandle {
}
}
}
impl Display for PathReal<FileMarker> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_pathbuf().display())
}
}

View file

@ -1,7 +1,6 @@
//
use std::{
marker::PhantomData,
path::{Path, PathBuf},
fmt::Display, marker::PhantomData, path::{Path, PathBuf}
};
use crate::fs::{Error, Result};
@ -367,3 +366,9 @@ impl From<FileHandle> for PathHandle<PathMarker> {
PathReal::new(file.base, file.path)
}
}
impl Display for PathReal<PathMarker> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_pathbuf().display())
}
}

View file

@ -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");