From 81f8438e2375c9a2077303ab151f5968560484ed Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 11 Dec 2020 18:30:51 +0200 Subject: refactor: how FsEntry structs are created Now from a Metadata struct, instead of a bunch of field values. The justification for this is that callers shouldn't have to unpack a Metadata, especially since it'll be different for each operating system in the future. Keep all that in one place instead. --- src/fsentry.rs | 58 +++++++++++++++++----------------------------------------- src/fsiter.rs | 8 ++------ 2 files changed, 19 insertions(+), 47 deletions(-) diff --git a/src/fsentry.rs b/src/fsentry.rs index ca73ef6..b8be212 100644 --- a/src/fsentry.rs +++ b/src/fsentry.rs @@ -1,4 +1,5 @@ use serde::{Deserialize, Serialize}; +use std::fs::{FileType, Metadata}; use std::path::{Path, PathBuf}; /// A file system entry. @@ -19,26 +20,11 @@ pub struct FilesystemEntry { #[allow(clippy::len_without_is_empty)] impl FilesystemEntry { - fn new(kind: FilesystemKind, path: &Path, len: u64) -> Self { - Self { - path: path.to_path_buf(), - kind, - len, - } - } - - pub fn regular

(path: P, len: u64) -> Self - where - P: AsRef, - { - Self::new(FilesystemKind::Regular, path.as_ref(), len) - } - - pub fn directory

(path: P) -> Self - where - P: AsRef, - { - Self::new(FilesystemKind::Directory, path.as_ref(), 0) + pub fn from_metadata(path: &Path, meta: &Metadata) -> Self { + let path = path.to_path_buf(); + let kind = FilesystemKind::from_file_type(meta.file_type()); + let len = meta.len(); + Self { path, kind, len } } pub fn kind(&self) -> FilesystemKind { @@ -62,6 +48,16 @@ pub enum FilesystemKind { } impl FilesystemKind { + pub fn from_file_type(file_type: FileType) -> Self { + if file_type.is_file() { + FilesystemKind::Regular + } else if file_type.is_dir() { + FilesystemKind::Directory + } else { + panic!("unknown file type {:?}", file_type); + } + } + pub fn as_code(&self) -> u8 { match self { FilesystemKind::Regular => 0, @@ -86,27 +82,7 @@ pub enum Error { #[cfg(test)] mod test { - use super::{FilesystemEntry, FilesystemKind}; - use std::path::Path; - - #[test] - fn regular_file() { - let filename = Path::new("foo.dat"); - let len = 123; - let e = FilesystemEntry::regular(filename, len); - assert_eq!(e.kind(), FilesystemKind::Regular); - assert_eq!(e.path(), filename); - assert_eq!(e.len(), len); - } - - #[test] - fn directory() { - let filename = Path::new("foo.dat"); - let e = FilesystemEntry::directory(filename); - assert_eq!(e.kind(), FilesystemKind::Directory); - assert_eq!(e.path(), filename); - assert_eq!(e.len(), 0); - } + use super::FilesystemKind; #[test] fn file_kind_regular_round_trips() { diff --git a/src/fsiter.rs b/src/fsiter.rs index 929c81e..3c08179 100644 --- a/src/fsiter.rs +++ b/src/fsiter.rs @@ -28,10 +28,6 @@ impl Iterator for FsIterator { fn new_entry(e: &walkdir::DirEntry) -> anyhow::Result { let meta = e.metadata()?; - let kind = if meta.is_dir() { - FilesystemEntry::directory(e.path()) - } else { - FilesystemEntry::regular(e.path(), meta.len()) - }; - Ok(kind) + let entry = FilesystemEntry::from_metadata(e.path(), &meta); + Ok(entry) } -- cgit v1.2.1