-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create traits and implementations for
std
structs
- Loading branch information
Showing
5 changed files
with
112 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//! This module contains extensions to the standard library. | ||
//! | ||
//! The public interface of this module consists of the following traits: | ||
//! | ||
//! * [`Abs`] | ||
//! * [`Ctime`] | ||
mod abs; | ||
mod ctime; | ||
|
||
pub use abs::Abs; | ||
pub use ctime::Ctime; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//! This module provides a trait [`Abs`], that can be used to extend `Path` and | ||
//! `PathBuf` with a method `abs` that converts a path to an absolute path. | ||
use std::env::current_dir; | ||
use std::path::{Path, PathBuf}; | ||
|
||
// ===== | ||
// Trait | ||
// ===== | ||
|
||
/// This trait provides a method `abs` that can be used to convert a path | ||
/// to an absolute path. | ||
pub trait Abs { | ||
/// Convert the given path to an absolute path. | ||
/// | ||
/// This function is appends the path to the current working directory if it | ||
/// is not already absolute and if the current working directory can be | ||
/// determined. In all other cases, the path will be returned as-is. | ||
fn abs(&self) -> PathBuf; | ||
} | ||
|
||
// =============== | ||
// Implementations | ||
// =============== | ||
|
||
impl Abs for Path { | ||
fn abs(&self) -> PathBuf { | ||
abs(self) | ||
} | ||
} | ||
|
||
impl Abs for PathBuf { | ||
fn abs(&self) -> PathBuf { | ||
abs(self) | ||
} | ||
} | ||
|
||
// ======= | ||
// Private | ||
// ======= | ||
|
||
fn abs<P>(path: P) -> PathBuf | ||
where | ||
P: AsRef<Path>, | ||
{ | ||
let path = path.as_ref(); | ||
if !path.is_absolute() { | ||
if let Ok(cwd) = current_dir() { | ||
return cwd.join(path); | ||
} | ||
} | ||
path.to_path_buf() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//! This module provides a trait [`Ctime`], that can be used to extend | ||
//! `Metadata` with a method `c_time` that provides the `st_ctime` of a node | ||
//! with an API that matches the other timestamp fields. | ||
use std::fs::Metadata; | ||
use std::io::Result as IoResult; | ||
#[cfg(unix)] | ||
use std::os::unix::fs::MetadataExt; | ||
use std::time::{Duration, SystemTime, UNIX_EPOCH}; | ||
|
||
// ===== | ||
// Trait | ||
// ===== | ||
|
||
/// This trait provides a method `ctime` that provides the `st_ctime` of a node. | ||
/// What this field represents depends on the operating system. | ||
/// | ||
/// > On some systems (like Unix) is the time of the last metadata change, and, | ||
/// > on others (like Windows), is the creation time. | ||
/// > | ||
/// > — [Python documentation](https://docs.python.org/3/library/stat.html#stat.ST_CTIME) | ||
pub trait Ctime { | ||
/// Compute the `st_ctime` of the node. | ||
/// | ||
/// This function matches the signature of other timestamp fields: | ||
/// | ||
/// * [`accessed`](Metadata::accessed) | ||
/// * [`created`](Metadata::created) | ||
/// * [`modified`](Metadata::modified) | ||
fn c_time(&self) -> IoResult<SystemTime>; | ||
} | ||
|
||
// =============== | ||
// Implementations | ||
// =============== | ||
|
||
impl Ctime for Metadata { | ||
fn c_time(&self) -> IoResult<SystemTime> { | ||
let sec = self.ctime(); | ||
let nanosec = self.ctime_nsec(); | ||
let ctime = UNIX_EPOCH + Duration::new(sec as u64, nanosec as u32); | ||
Ok(ctime) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod config; | ||
mod enums; | ||
mod exc; | ||
mod ext; | ||
mod fmt; | ||
mod models; | ||
mod output; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters