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, package_root: PathBuf, target_dir: PathBuf, } impl PackageHandler { const DIR_JAVA: &str = "java/"; pub fn new>(src_dir: P, package_root: P, target_dir: P) -> crate::Result { 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> { 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(&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 for Package {}