44 lines
1 KiB
Rust
44 lines
1 KiB
Rust
use std::{
|
|
fs, io,
|
|
path::{Path, PathBuf},
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use super::SharryFile;
|
|
|
|
#[derive(Debug, Clone, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
|
pub struct FileChecked {
|
|
pub(super) path: PathBuf,
|
|
pub(super) size: u64,
|
|
}
|
|
|
|
impl FileChecked {
|
|
pub fn new(value: impl AsRef<Path>) -> io::Result<Self> {
|
|
let meta = fs::metadata(&value)?;
|
|
if meta.is_file() {
|
|
Ok(Self {
|
|
path: fs::canonicalize(&value)?,
|
|
size: meta.len(),
|
|
})
|
|
} else {
|
|
Err(io::Error::new(
|
|
io::ErrorKind::InvalidInput,
|
|
"Not a regular file",
|
|
))
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'t> SharryFile<'t> for FileChecked {
|
|
/// get a reference to the file's name
|
|
///
|
|
/// Uses `SharryFile::extract_file_name`, which may **panic**!
|
|
fn get_name(&'t self) -> &'t str {
|
|
<Self as SharryFile>::extract_file_name(&self.path)
|
|
}
|
|
|
|
fn get_size(&self) -> u64 {
|
|
self.size
|
|
}
|
|
}
|