diff --git a/src/repo.rs b/src/repo/contents.rs similarity index 87% rename from src/repo.rs rename to src/repo/contents.rs index 9b76840..41191a2 100644 --- a/src/repo.rs +++ b/src/repo/contents.rs @@ -1,70 +1,38 @@ -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::{ - env, fs::{self, File}, io::Write, path::{Path, PathBuf}, process::Command, rc::Rc, }; -use tempfile::NamedTempFile; -use crate::{ - config::{read_config, SiloConfig}, - templating, -}; +use crate::templating; + +use super::{ApplyContext, ParseContext}; +use chksum::sha2_256::chksum; +use dialoguer::Confirm; +use globset::{Glob, GlobSet, GlobSetBuilder}; use lazy_static::lazy_static; +use miette::{Context, IntoDiagnostic, Result}; +use serde::{Deserialize, Serialize}; +use tempfile::NamedTempFile; #[derive(Clone, Debug)] -pub struct SiloRepo { +pub struct Contents { pub root: DirEntry, - pub config: SiloConfig, } -impl SiloRepo { - pub fn open(path: &Path) -> Result { - if !path.try_exists().into_diagnostic()? { - bail!("The repository {path:?} does not exist"); - } - let config = read_config(path)?; - - Ok(Self { - root: DirEntry::parse( - Rc::new(ParseContext::new(GlobSet::empty(), config.clone())), - 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.root.apply(&ctx, &cwd) +impl Contents { + pub fn parse(pctx: ParseContext, path: PathBuf) -> Result { + let root = DirEntry::parse(Rc::new(pctx), path.to_owned())?; + Ok(Self { root }) } -} - -pub struct ParseContext { - ignored: GlobSet, - config: SiloConfig, -} -impl ParseContext { - pub fn new(ignored: GlobSet, config: SiloConfig) -> Self { - Self { ignored, config } + pub fn apply(&self, actx: &ApplyContext, cwd: &Path) -> Result<()> { + self.root.apply(actx, cwd) } } -pub struct ApplyContext { - config: SiloConfig, -} - lazy_static! { static ref IGNORED_PATHS: GlobSet = GlobSetBuilder::new() .add(Glob::new("**/.git").unwrap()) diff --git a/src/repo/mod.rs b/src/repo/mod.rs new file mode 100644 index 0000000..f2559de --- /dev/null +++ b/src/repo/mod.rs @@ -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 { + 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, +}