Speed up brute force building with Prey.lock hashing and caching.

This commit is contained in:
Cutieguwu
2026-02-16 20:41:31 -05:00
parent 1009a84c06
commit f0d22e6b79
8 changed files with 170 additions and 92 deletions

View File

@@ -1,7 +1,9 @@
use std::collections::HashSet;
use std::fs::{File, OpenOptions};
use std::io::Read;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use fs::expand_files;
use serde::{Deserialize, Serialize};
use crate::class::Class;
@@ -68,14 +70,54 @@ impl TryFrom<File> for Prey {
/// Data struct
#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct PreyLock {
pub classes: Vec<Class>,
classes: HashSet<Class>,
}
impl PreyLock {
pub fn new<P: AsRef<Path>>(package_root: P, package_src_root: P) -> crate::Result<Self> {
Ok(Self::from_paths(
expand_files(package_root)?,
package_src_root,
))
}
pub fn from_paths<P: AsRef<Path>>(paths: Vec<PathBuf>, package_src_root: P) -> Self {
let mut lock = Self::default();
lock.classes = paths
.iter()
.filter_map(|f| {
let dep = Class::new(package_src_root.as_ref().to_path_buf(), f.to_owned());
if dep.is_ok() {
Some(dep.unwrap())
} else {
None
}
})
.collect();
lock
}
pub fn write<P: AsRef<Path>>(&self, package_root: P) -> crate::Result<()> {
let mut package_root = package_root.as_ref().to_path_buf();
if package_root.is_dir() {
package_root = package_root.join(F_PREY_LOCK);
}
Ok(OpenOptions::new()
.write(true)
.create(true)
.open(package_root)?
.write_all(toml::to_string_pretty(&self)?.as_bytes())?)
}
pub fn with_class(&mut self, class: Class) -> &mut Self {
self.classes.push(class);
self.classes.insert(class);
self
}
pub fn classes(&mut self) -> &mut HashSet<Class> {
&mut self.classes
}
}
/// Load the PreyLock from Prey.lock file.
@@ -97,21 +139,3 @@ impl TryFrom<File> for PreyLock {
Ok(toml::from_str(buf.as_str())?)
}
}
impl From<Vec<PathBuf>> for PreyLock {
fn from(value: Vec<PathBuf>) -> Self {
let mut lock = Self::default();
lock.classes = value
.iter()
.filter_map(|f| {
let dep = Class::try_from(f.to_owned());
if dep.is_ok() {
Some(dep.unwrap())
} else {
None
}
})
.collect();
lock
}
}