use std::path::{Path, PathBuf};
use crate::constant::REGEX_CACHE_FILE;
use crate::error::{Error, Fallible};
#[derive(Clone, Debug)]
pub struct CacheFile {
path: PathBuf,
}
impl CacheFile {
pub fn from(path: PathBuf) -> Fallible<CacheFile> {
let text = path.file_name().unwrap().to_str().unwrap();
match REGEX_CACHE_FILE.is_match(text) {
false => Err(Error::InvalidCacheFile { path }),
true => Ok(CacheFile { path }),
}
}
#[inline]
pub fn path(&self) -> &Path {
&self.path
}
#[inline]
pub fn file_name(&self) -> &str {
self.path.file_name().unwrap().to_str().unwrap()
}
#[inline]
pub fn package_name(&self) -> &str {
self.file_name().split_once('#').map(|s| s.0).unwrap()
}
#[inline]
pub fn version(&self) -> &str {
self.file_name().splitn(3, '#').collect::<Vec<_>>()[1]
}
}