Files
raven/crates/core/src/class.rs

50 lines
1.6 KiB
Rust
Executable File

use std::path::{Path, PathBuf};
use java::{JAVA_EXT_CLASS, JAVA_EXT_SOURCE};
use serde::{Deserialize, Serialize};
use crate::{Error, Result};
/// Data struct
#[derive(Debug, Clone, Deserialize, Serialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Class {
/// Path relative to PACKAGE/java, without file extension.
pub path: PathBuf,
pub checksum: String,
}
impl Class {
pub fn new<P: AsRef<Path>>(package_src_root: P, file_path: P) -> Result<Self> {
let mut file_path = file_path.as_ref().to_path_buf();
if file_path.is_absolute() {
file_path = pathsub::sub_paths(file_path.as_path(), package_src_root.as_ref())
.ok_or(Error::MismatchedPackage)?;
}
Ok(Self {
path: dbg!(file_path.with_extension("")),
checksum: sha256::try_digest(package_src_root.as_ref().join(file_path))?,
})
}
pub fn is_updated<P: AsRef<Path>>(&self, class_path: P) -> Result<bool> {
// If the path is local and that file has not been updated.
Ok(class_path
.as_ref()
.join(self.path.as_path())
.with_extension(JAVA_EXT_CLASS)
.exists()
&& self.checksum == sha256::try_digest(self.path.clone())?)
}
pub fn update<P: AsRef<Path>>(&mut self, package_src_root: P) -> Result<()> {
self.checksum = sha256::try_digest(dbg!(
package_src_root
.as_ref()
.join(self.path.as_path())
.with_extension(JAVA_EXT_SOURCE),
))?;
Ok(())
}
}