Files
raven/crates/core/src/package.rs
2026-02-15 17:39:48 -05:00

87 lines
2.4 KiB
Rust

use std::hash::Hash;
use std::path::{Path, PathBuf};
use fs::expand_files;
use serde::{Deserialize, Serialize};
use crate::class::Class;
use crate::prey::{F_PREY_LOCK, F_PREY_TOML, Prey, PreyLock};
/// Hashing is only based off the Prey.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PackageHandler {
prey: Prey,
prey_lock: Option<PreyLock>,
package_root: PathBuf,
target_dir: PathBuf,
}
impl PackageHandler {
const DIR_JAVA: &str = "java/";
pub fn new<P: AsRef<Path>>(src_dir: P, package_root: P, target_dir: P) -> crate::Result<Self> {
let package_root = src_dir.as_ref().join(package_root.as_ref());
Ok(Self {
prey: Prey::try_from(package_root.join(F_PREY_TOML))?,
prey_lock: PreyLock::try_from(package_root.join(F_PREY_LOCK)).ok(),
package_root,
target_dir: target_dir.as_ref().to_path_buf(),
})
}
pub fn entry_point(&self) -> PathBuf {
self.prey.entry_point()
}
pub fn name(&self) -> String {
self.prey.name()
}
pub fn version(&self) -> semver::Version {
self.prey.version()
}
pub fn get_update_targets(&mut self) -> crate::Result<Vec<&Class>> {
if self.prey_lock.is_none() {
// Try to pass a reference to the class so that there's mutability of the object
// available at the workspace level instead of parsing all the way down the
// tree. How I do this, idk. My brain is friend from a few days of JS.
self.prey_lock = Some(PreyLock::from(expand_files(
self.package_root.join(Self::DIR_JAVA),
)?));
return Ok(self.prey_lock.as_ref().unwrap().classes.iter().collect());
}
Ok(self
.prey_lock
.as_ref()
.unwrap()
.classes
.iter()
.filter_map(|tracked| {
if tracked.is_updated().is_ok_and(|v| v == true) {
Some(tracked)
} else {
None
}
})
.collect())
}
}
impl Hash for PackageHandler {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.prey.hash(state);
}
}
/// Data struct
#[derive(Debug, Clone, Deserialize, Serialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Package {
pub entry_point: PathBuf,
}
//impl Into<Dependency> for Package {}