use std::fs::{self, File};
use std::io;
use std::path::Path;
use crate::error::{AppError, Result};
pub fn open_regular_file(path: &Path) -> Result<(File, u64)> {
if !fs::metadata(path)?.is_file() {
return Err(AppError::InputNotRegularFile(path.to_path_buf()));
}
let file = File::open(path)?;
let metadata = file.metadata()?;
if !metadata.is_file() {
return Err(AppError::InputNotRegularFile(path.to_path_buf()));
}
Ok((file, metadata.len()))
}
pub fn reject_non_regular_file(path: &Path) -> Result<()> {
match fs::metadata(path) {
Ok(metadata) if !metadata.is_file() => {
Err(AppError::InputNotRegularFile(path.to_path_buf()))
}
Ok(_) => Ok(()),
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
Err(error) => Err(AppError::Io(error)),
}
}