Files
raven/retired/nest_lib_rs_old/prey.rs
2026-02-15 09:36:04 -05:00

128 lines
3.0 KiB
Rust

use std::collections::HashSet;
use std::collections::hash_set::Iter;
use std::fs::{File, OpenOptions};
use std::io::Read;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use path::{PathHandled, PathHandler};
use serde::{Deserialize, Serialize};
use crate::Error;
use crate::class::Class;
use crate::meta::Meta;
use crate::package::Package;
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct Prey {
package: Package,
meta: Meta,
}
impl Prey {
pub fn new<S: ToString>(name: S) -> Self {
Prey {
package: (),
meta: (),
}
}
}
impl TryFrom<File> for Prey {
type Error = Error;
fn try_from(value: File) -> Result<Self, Self::Error> {
let mut value = value;
let mut buf = String::new();
value.read_to_string(&mut buf)?;
Ok(toml::from_str(buf.as_str())?)
}
}
impl TryFrom<PathBuf> for Prey {
type Error = Error;
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
Prey::try_from(OpenOptions::new().read(true).open(value)?)
}
}
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct PreyLock {
#[serde(skip_deserializing, skip_serializing)]
ph: Option<Arc<Mutex<PathHandler>>>,
classes: HashSet<Class>,
}
impl PreyLock {
pub fn new(ph: Arc<Mutex<PathHandler>>, classes: HashSet<Class>) -> Self {
Self {
ph: Some(ph),
classes,
}
}
pub fn update(&mut self) -> crate::Result<()> {
let ph = self.ph.as_ref().ok_or(Error::MissingPathHandler)?.clone();
self.classes = self
.classes
.clone()
.into_iter()
.filter_map(|mut class| {
let class = class.with_path_handler(Arc::clone(&ph));
// If the class is up-to-date and no errors occurred during the check
if class.is_updated().is_ok_and(|b| b == true) {
Some(class.to_owned())
} else {
None
}
})
.collect();
Ok(())
}
/// Iterates over the entries in `PreyLock`.
pub fn iter(&self) -> Iter<'_, Class> {
self.classes.iter()
}
pub fn insert(&mut self, class: Class) -> bool {
self.classes.insert(class)
}
}
impl TryFrom<File> for PreyLock {
type Error = Error;
fn try_from(value: File) -> Result<Self, Self::Error> {
let mut value = value;
let mut buf = String::new();
value.read_to_string(&mut buf)?;
Ok(toml::from_str(buf.as_str())?)
}
}
impl TryFrom<PathBuf> for PreyLock {
type Error = Error;
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
PreyLock::try_from(OpenOptions::new().read(true).open(value)?)
}
}
impl PathHandled for PreyLock {
fn set_path_handler(&mut self, ph: Arc<Mutex<PathHandler>>) {
self.ph = Some(ph);
}
fn with_path_handler(&mut self, ph: Arc<Mutex<PathHandler>>) -> &mut Self {
self.ph = Some(ph);
self
}
}