fix: impl Display for path, file and dir
This commit is contained in:
parent
a58796f2f0
commit
84b3303c80
5 changed files with 49 additions and 21 deletions
|
@ -35,9 +35,7 @@ async fn main() -> kxio::Result<()> {
|
||||||
|
|
||||||
// Checks if the path exists (whether a file, directory, etc)
|
// Checks if the path exists (whether a file, directory, etc)
|
||||||
if path.exists()? {
|
if path.exists()? {
|
||||||
// extracts the path from the handle
|
eprintln!("The file {path} already exists. Aborting!");
|
||||||
let pathbuf = path.as_pathbuf();
|
|
||||||
eprintln!("The file {} already exists. Aborting!", pathbuf.display());
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,9 +77,9 @@ async fn download_and_save_to_file(
|
||||||
let body = response.text().await?;
|
let body = response.text().await?;
|
||||||
println!("fetched {} bytes", body.bytes().len());
|
println!("fetched {} bytes", body.bytes().len());
|
||||||
|
|
||||||
println!("writing file: {}", file_path.display());
|
|
||||||
// Uses the file system abstraction to create a handle for a file.
|
// Uses the file system abstraction to create a handle for a file.
|
||||||
let file: kxio::fs::PathReal<kxio::fs::FileMarker> = fs.file(file_path);
|
let file: kxio::fs::PathReal<kxio::fs::FileMarker> = fs.file(file_path);
|
||||||
|
println!("writing file: {file}");
|
||||||
// Writes the body to the file.
|
// Writes the body to the file.
|
||||||
file.write(body)?;
|
file.write(body)?;
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
//
|
//
|
||||||
use crate::fs::{DirItem, DirItemIterator, Result};
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
//
|
//
|
||||||
use crate::fs::Result;
|
use crate::fs::Result;
|
||||||
|
|
||||||
use super::{reader::Reader, Error, FileHandle, PathHandle, PathMarker};
|
use super::{reader::Reader, Error, FileHandle, FileMarker, PathHandle, PathMarker, PathReal};
|
||||||
|
|
||||||
impl FileHandle {
|
impl FileHandle {
|
||||||
/// Returns a [Reader] for the file.
|
/// 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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
//
|
//
|
||||||
use std::{
|
use std::{
|
||||||
marker::PhantomData,
|
fmt::Display, marker::PhantomData, path::{Path, PathBuf}
|
||||||
path::{Path, PathBuf},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::fs::{Error, Result};
|
use crate::fs::{Error, Result};
|
||||||
|
@ -367,3 +366,9 @@ impl From<FileHandle> for PathHandle<PathMarker> {
|
||||||
PathReal::new(file.base, file.path)
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
37
tests/fs.rs
37
tests/fs.rs
|
@ -16,6 +16,14 @@ mod path {
|
||||||
|
|
||||||
assert_eq!(read.base(), fs.base());
|
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 {
|
mod is_link {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
@ -456,20 +464,13 @@ mod path {
|
||||||
mod file {
|
mod file {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
// // test for reading the symlink metadata
|
#[test]
|
||||||
// #[test]
|
fn display() {
|
||||||
// fn symlink_metadata() -> TestResult {
|
let fs = fs::temp().expect("temp fs");
|
||||||
// let fs = fs::temp().expect("temp fs");
|
let path = fs.base().join("foo");
|
||||||
// let file_path = fs.base().join("foo");
|
let file_handle = fs.file(&path);
|
||||||
// let file = fs.file(&file_path);
|
assert_eq!(file_handle.to_string(), format!("{}", path.display()));
|
||||||
// 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]
|
#[test]
|
||||||
fn create_hard_link() -> TestResult {
|
fn create_hard_link() -> TestResult {
|
||||||
|
@ -793,6 +794,14 @@ mod dir {
|
||||||
|
|
||||||
use super::*;
|
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]
|
#[test]
|
||||||
fn path_is_dir() {
|
fn path_is_dir() {
|
||||||
let fs = fs::temp().expect("temp fs");
|
let fs = fs::temp().expect("temp fs");
|
||||||
|
|
Loading…
Reference in a new issue