Impl From<Node> for Listing

This commit is contained in:
Nathan Fisher 2024-01-01 18:30:24 -05:00
parent 63b414b7fd
commit b25a49260e
1 changed files with 29 additions and 1 deletions

View File

@ -5,7 +5,7 @@ use {
};
use {
crate::{filetype::Flag, Error, Special},
crate::{filetype::Flag, Error, FileType, Node, Special},
chrono::NaiveDateTime,
std::{
cmp::Ordering,
@ -26,6 +26,21 @@ pub enum Kind {
Eof,
}
impl From<FileType> for Kind {
fn from(value: FileType) -> Self {
match value {
FileType::Normal(f) => Self::Normal(f.len),
FileType::HardLink(tgt) => Self::HardLink(tgt),
FileType::SoftLink(tgt) => Self::SoftLink(tgt),
FileType::Directory => Self::Directory,
FileType::Character(c) => Self::Character(c),
FileType::Block(b) => Self::Block(b),
FileType::Fifo => Self::Fifo,
FileType::Eof => Self::Eof,
}
}
}
impl Kind {
#[allow(clippy::cast_possible_wrap)]
fn read<R: Read + Seek>(reader: &mut R, flag: Flag) -> Result<Self, Error> {
@ -97,6 +112,19 @@ pub struct Listing {
pub kind: Kind,
}
impl From<Node> for Listing {
fn from(value: Node) -> Self {
Self {
name: value.name,
uid: value.uid,
gid: value.gid,
mtime: value.mtime,
mode: value.mode,
kind: value.filetype.into(),
}
}
}
impl PartialOrd for Listing {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))