diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-12-11 18:30:51 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-12-11 19:02:18 +0200 |
commit | 81f8438e2375c9a2077303ab151f5968560484ed (patch) | |
tree | c4fd17ad3110a5f8ad4903a64ce5d6d66a054040 /src/fsentry.rs | |
parent | 0e1cfff1de7939cd1a36876258ec2b8a61aed0bb (diff) | |
download | obnam2-81f8438e2375c9a2077303ab151f5968560484ed.tar.gz |
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.
Diffstat (limited to 'src/fsentry.rs')
-rw-r--r-- | src/fsentry.rs | 58 |
1 files changed, 17 insertions, 41 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<P>(path: P, len: u64) -> Self - where - P: AsRef<Path>, - { - Self::new(FilesystemKind::Regular, path.as_ref(), len) - } - - pub fn directory<P>(path: P) -> Self - where - P: AsRef<Path>, - { - 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() { |