forked from Cutieguwu/raven
111 lines
2.8 KiB
Rust
111 lines
2.8 KiB
Rust
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::dependency::Dependency;
|
|
use crate::meta::Meta;
|
|
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<()> {
|
|
let mut path = path.as_ref().to_path_buf();
|
|
if path.is_dir() {
|
|
path = path.join(F_NEST_TOML);
|
|
}
|
|
|
|
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())?)
|
|
}
|
|
}
|