forked from Cutieguwu/raven
Speed up brute force building with Prey.lock hashing and caching.
This commit is contained in:
@@ -1,35 +1,49 @@
|
||||
use std::path::PathBuf;
|
||||
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 is_updated(&self) -> crate::Result<bool> {
|
||||
// If the path is local and that file has not been updated.
|
||||
Ok(self.checksum == sha256::try_digest(self.path.clone())?)
|
||||
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 update(&mut self) -> crate::Result<()> {
|
||||
self.checksum = sha256::try_digest(self.path.clone())?;
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Make it clearer that this is for general files,
|
||||
// not nests.
|
||||
impl TryFrom<PathBuf> for Class {
|
||||
type Error = crate::Error;
|
||||
|
||||
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
path: value.clone(),
|
||||
checksum: sha256::try_digest(value)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user