forked from Cutieguwu/raven
Most of the refactor. Need to switch machines.
This commit is contained in:
78
crates/core/src/prey.rs
Normal file
78
crates/core/src/prey.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use std::collections::HashSet;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::class::Class;
|
||||
use crate::meta::Meta;
|
||||
use crate::package::Package;
|
||||
|
||||
pub const F_PREY_TOML: &str = "Prey.toml";
|
||||
pub const F_PREY_LOCK: &str = "Prey.lock";
|
||||
|
||||
/// Data struct
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, Hash, PartialEq, Eq)]
|
||||
pub struct Prey {
|
||||
package: Package,
|
||||
meta: Meta,
|
||||
}
|
||||
|
||||
impl Prey {
|
||||
pub fn entry_point(&self) -> PathBuf {
|
||||
self.package.entry_point.clone()
|
||||
}
|
||||
|
||||
pub fn name(&self) -> String {
|
||||
self.meta.name.clone()
|
||||
}
|
||||
|
||||
pub fn version(&self) -> semver::Version {
|
||||
self.meta.version.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<PathBuf> for Prey {
|
||||
type Error = crate::Error;
|
||||
|
||||
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
|
||||
let f = OpenOptions::new().read(true).open(value)?;
|
||||
Self::try_from(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<File> for Prey {
|
||||
type Error = crate::Error;
|
||||
|
||||
fn try_from(mut value: File) -> Result<Self, Self::Error> {
|
||||
let mut buf = String::new();
|
||||
value.read_to_string(&mut buf)?;
|
||||
Ok(toml::from_str(buf.as_str())?)
|
||||
}
|
||||
}
|
||||
|
||||
/// Data struct
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
||||
pub struct PreyLock {
|
||||
classes: HashSet<Class>,
|
||||
}
|
||||
|
||||
impl TryFrom<PathBuf> for PreyLock {
|
||||
type Error = crate::Error;
|
||||
|
||||
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
|
||||
let f = OpenOptions::new().read(true).open(value)?;
|
||||
Self::try_from(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<File> for PreyLock {
|
||||
type Error = crate::Error;
|
||||
|
||||
fn try_from(mut value: File) -> Result<Self, Self::Error> {
|
||||
let mut buf = String::new();
|
||||
value.read_to_string(&mut buf)?;
|
||||
Ok(toml::from_str(buf.as_str())?)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user