forked from Cutieguwu/raven
Most of the refactor. Need to switch machines.
This commit is contained in:
105
crates/core/src/nest.rs
Normal file
105
crates/core/src/nest.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
use std::collections::HashSet;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::{Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::meta::Meta;
|
||||
use crate::prelude::Dependency;
|
||||
use crate::workspace::Workspace;
|
||||
|
||||
pub const F_NEST_TOML: &str = "Nest.toml";
|
||||
pub const F_NEST_LOCK: &str = "Nest.lock";
|
||||
|
||||
/// Data struct
|
||||
#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq, Eq)]
|
||||
pub struct Nest {
|
||||
workspace: Workspace,
|
||||
meta: Meta,
|
||||
dependencies: HashSet<Dependency>,
|
||||
}
|
||||
|
||||
impl Nest {
|
||||
pub fn new<S: ToString>(name: S) -> Self {
|
||||
Self {
|
||||
workspace: Workspace::default(),
|
||||
meta: Meta::new(name),
|
||||
dependencies: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write<P: AsRef<Path>>(&self, path: P) -> crate::Result<()> {
|
||||
Ok(OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open(path)?
|
||||
.write_all(toml::to_string_pretty(&self)?.as_bytes())?)
|
||||
}
|
||||
|
||||
pub fn default_package(&self) -> PathBuf {
|
||||
self.workspace.default_package.clone()
|
||||
}
|
||||
|
||||
pub fn name(&self) -> String {
|
||||
self.meta.name.clone()
|
||||
}
|
||||
|
||||
pub fn set_default_package<P: AsRef<Path>>(&mut self, package: P) {
|
||||
self.workspace.default_package = package.as_ref().to_path_buf();
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<PathBuf> for Nest {
|
||||
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 Nest {
|
||||
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, Default, Deserialize, Serialize, PartialEq, Eq)]
|
||||
pub struct NestLock {
|
||||
pub dependencies: Vec<Dependency>,
|
||||
}
|
||||
|
||||
impl NestLock {
|
||||
pub fn write<P: AsRef<Path>>(&self, path: P) -> crate::Result<()> {
|
||||
Ok(OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open(path)?
|
||||
.write_all(toml::to_string_pretty(&self)?.as_bytes())?)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<PathBuf> for NestLock {
|
||||
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 NestLock {
|
||||
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