Remove repo content parsing to submodule

main
trivernis 9 months ago
parent e1718162ee
commit efd1d54c2c
Signed by: Trivernis
GPG Key ID: 7E6D18B61C8D2F4B

@ -1,68 +1,36 @@
use chksum::sha2_256::chksum;
use dialoguer::Confirm;
use globset::{Glob, GlobSet, GlobSetBuilder};
use miette::{bail, Context, IntoDiagnostic, Result};
use serde::{Deserialize, Serialize};
use std::{ use std::{
env,
fs::{self, File}, fs::{self, File},
io::Write, io::Write,
path::{Path, PathBuf}, path::{Path, PathBuf},
process::Command, process::Command,
rc::Rc, rc::Rc,
}; };
use tempfile::NamedTempFile;
use crate::{ use crate::templating;
config::{read_config, SiloConfig},
templating, use super::{ApplyContext, ParseContext};
}; use chksum::sha2_256::chksum;
use dialoguer::Confirm;
use globset::{Glob, GlobSet, GlobSetBuilder};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use miette::{Context, IntoDiagnostic, Result};
use serde::{Deserialize, Serialize};
use tempfile::NamedTempFile;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct SiloRepo { pub struct Contents {
pub root: DirEntry, pub root: DirEntry,
pub config: SiloConfig,
}
impl SiloRepo {
pub fn open(path: &Path) -> Result<Self> {
if !path.try_exists().into_diagnostic()? {
bail!("The repository {path:?} does not exist");
} }
let config = read_config(path)?;
Ok(Self { impl Contents {
root: DirEntry::parse( pub fn parse(pctx: ParseContext, path: PathBuf) -> Result<Self> {
Rc::new(ParseContext::new(GlobSet::empty(), config.clone())), let root = DirEntry::parse(Rc::new(pctx), path.to_owned())?;
path.to_owned(), Ok(Self { root })
)?,
config,
})
} }
pub fn apply(&self) -> Result<()> { pub fn apply(&self, actx: &ApplyContext, cwd: &Path) -> Result<()> {
let cwd = dirs::home_dir().unwrap_or(env::current_dir().into_diagnostic()?); self.root.apply(actx, cwd)
let ctx = ApplyContext {
config: self.config.clone(),
};
self.root.apply(&ctx, &cwd)
}
} }
pub struct ParseContext {
ignored: GlobSet,
config: SiloConfig,
}
impl ParseContext {
pub fn new(ignored: GlobSet, config: SiloConfig) -> Self {
Self { ignored, config }
}
}
pub struct ApplyContext {
config: SiloConfig,
} }
lazy_static! { lazy_static! {

@ -0,0 +1,54 @@
mod contents;
use globset::GlobSet;
use miette::{bail, IntoDiagnostic, Result};
use std::{env, path::Path};
use crate::config::{read_config, SiloConfig};
use self::contents::Contents;
#[derive(Clone, Debug)]
pub struct SiloRepo {
pub config: SiloConfig,
contents: Contents,
}
impl SiloRepo {
pub fn open(path: &Path) -> Result<Self> {
if !path.try_exists().into_diagnostic()? {
bail!("The repository {path:?} does not exist");
}
let config = read_config(path)?;
let pctx = ParseContext::new(GlobSet::empty(), config.clone());
Ok(Self {
contents: Contents::parse(pctx, path.to_owned())?,
config,
})
}
pub fn apply(&self) -> Result<()> {
let cwd = dirs::home_dir().unwrap_or(env::current_dir().into_diagnostic()?);
let ctx = ApplyContext {
config: self.config.clone(),
};
self.contents.apply(&ctx, &cwd)
}
}
pub struct ParseContext {
ignored: GlobSet,
config: SiloConfig,
}
impl ParseContext {
pub fn new(ignored: GlobSet, config: SiloConfig) -> Self {
Self { ignored, config }
}
}
pub struct ApplyContext {
config: SiloConfig,
}
Loading…
Cancel
Save