diff --git a/src/actions/actions.rs b/src/actions/actions.rs index 0836446..a9158d9 100644 --- a/src/actions/actions.rs +++ b/src/actions/actions.rs @@ -12,7 +12,7 @@ use tokio::io::{self, AsyncReadExt, AsyncWriteExt}; use unindent::Unindent; use crate::actions::{State, Value}; -use crate::manifest::actions::*; +use crate::config::actions::*; use crate::path::{PathClean, Traverser}; use crate::spinner::Spinner; diff --git a/src/actions/executor.rs b/src/actions/executor.rs index ec7d93e..a32f826 100644 --- a/src/actions/executor.rs +++ b/src/actions/executor.rs @@ -6,7 +6,7 @@ use miette::Diagnostic; use thiserror::Error; use tokio::fs; -use crate::manifest::{ActionSingle, ActionSuite, Actions, Manifest}; +use crate::config::{ActionSingle, ActionSuite, Actions, Config}; #[derive(Debug, Diagnostic, Error)] pub enum ExecutorError { @@ -60,27 +60,27 @@ impl Default for State { /// An executor. #[derive(Debug)] pub struct Executor { - /// The manifest to use for execution. - manifest: Manifest, + /// The config to use for execution. + config: Config, } impl Executor { /// Create a new executor. - pub fn new(manifest: Manifest) -> Self { - Self { manifest } + pub fn new(config: Config) -> Self { + Self { config } } /// Execute the actions. pub async fn execute(&self) -> miette::Result<()> { - match &self.manifest.actions { + match &self.config.actions { | Actions::Suite(suites) => self.suite(suites).await?, | Actions::Flat(actions) => self.flat(actions).await?, | Actions::Empty => println!("No actions found."), }; // Delete the config file if needed. - if self.manifest.options.delete { - fs::remove_file(&self.manifest.config) + if self.config.options.delete { + fs::remove_file(&self.config.config) .await .map_err(|source| { ExecutorError::Io { @@ -137,7 +137,7 @@ impl Executor { /// Execute a single action. async fn single(&self, action: &ActionSingle, state: &mut State) -> miette::Result<()> { - let root = &self.manifest.root; + let root = &self.config.root; match action { | ActionSingle::Copy(action) => action.execute(root).await, diff --git a/src/actions/prompts.rs b/src/actions/prompts.rs index e66d133..7bcc8c6 100644 --- a/src/actions/prompts.rs +++ b/src/actions/prompts.rs @@ -8,7 +8,7 @@ use inquire::ui::{Color, RenderConfig, StyleSheet, Styled}; use inquire::{Confirm, Editor, InquireError, Select, Text}; use crate::actions::{State, Value}; -use crate::manifest::prompts; +use crate::config::prompts; /// Helper struct holding useful static methods. struct Inquirer; diff --git a/src/app.rs b/src/app.rs index ca95ab2..ed46168 100644 --- a/src/app.rs +++ b/src/app.rs @@ -8,7 +8,7 @@ use miette::Diagnostic; use thiserror::Error; use crate::actions::Executor; -use crate::manifest::{Manifest, ManifestOptionsOverrides}; +use crate::config::{Config, ConfigOptionsOverrides}; use crate::repository::{LocalRepository, RemoteRepository}; use crate::unpacker::Unpacker; @@ -88,20 +88,20 @@ impl App { ) }))?; - // Load the manifest. - let manifest = match self.cli.command { + // Load the config. + let config = match self.cli.command { | BaseCommand::Remote { src, path, meta, delete } => { - let options = ManifestOptionsOverrides { delete }; + let options = ConfigOptionsOverrides { delete }; Self::remote(src, path, meta, options).await? }, | BaseCommand::Local { src, path, meta, delete } => { - let options = ManifestOptionsOverrides { delete }; + let options = ConfigOptionsOverrides { delete }; Self::local(src, path, meta, options).await? }, }; // Create executor and kick off execution. - let executor = Executor::new(manifest); + let executor = Executor::new(config); executor.execute().await?; Ok(()) @@ -112,8 +112,8 @@ impl App { src: String, path: Option, meta: Option, - overrides: ManifestOptionsOverrides, - ) -> miette::Result { + overrides: ConfigOptionsOverrides, + ) -> miette::Result { // Parse repository. let remote = RemoteRepository::new(src, meta)?; @@ -135,13 +135,13 @@ impl App { let unpacker = Unpacker::new(tarball); unpacker.unpack_to(&destination)?; - // Now we need to read the manifest (if it is present). - let mut manifest = Manifest::new(&destination); + // Now we need to read the config (if it is present). + let mut config = Config::new(&destination); - manifest.load()?; - manifest.override_with(overrides); + config.load()?; + config.override_with(overrides); - Ok(manifest) + Ok(config) } /// Preparation flow for local repositories. @@ -149,8 +149,8 @@ impl App { src: String, path: Option, meta: Option, - overrides: ManifestOptionsOverrides, - ) -> miette::Result { + overrides: ConfigOptionsOverrides, + ) -> miette::Result { // Create repository. let local = LocalRepository::new(src, meta); @@ -196,13 +196,13 @@ impl App { println!("{}", "~ Removed inner .git directory\n".dim()); } - // Now we need to read the manifest (if it is present). - let mut manifest = Manifest::new(&destination); + // Now we need to read the config (if it is present). + let mut config = Config::new(&destination); - manifest.load()?; - manifest.override_with(overrides); + config.load()?; + config.override_with(overrides); - Ok(manifest) + Ok(config) } } diff --git a/src/manifest/actions.rs b/src/config/actions.rs similarity index 98% rename from src/manifest/actions.rs rename to src/config/actions.rs index d56480b..ed87457 100644 --- a/src/manifest/actions.rs +++ b/src/config/actions.rs @@ -1,6 +1,6 @@ use std::collections::HashSet; -use crate::manifest::prompts::*; +use crate::config::prompts::*; /// Copies a file or directory. Glob-friendly. Overwrites by default. #[derive(Debug)] diff --git a/src/manifest/manifest.rs b/src/config/config.rs similarity index 81% rename from src/manifest/manifest.rs rename to src/config/config.rs index 5b62421..adc9d2d 100644 --- a/src/manifest/manifest.rs +++ b/src/config/config.rs @@ -7,16 +7,16 @@ use kdl::{KdlDocument, KdlNode}; use miette::{Diagnostic, LabeledSpan, NamedSource, Report}; use thiserror::Error; -use crate::manifest::actions::*; -use crate::manifest::prompts::*; -use crate::manifest::KdlUtils; +use crate::config::actions::*; +use crate::config::prompts::*; +use crate::config::KdlUtils; -const MANIFEST_NAME: &str = "arx.kdl"; +const CONFIG_NAME: &str = "arx.kdl"; -/// Helper macro to create a [ManifestError::Diagnostic] in a slightly less verbose way. +/// Helper macro to create a [ConfigError::Diagnostic] in a slightly less verbose way. macro_rules! diagnostic { ($source:ident = $code:expr, $($key:ident = $value:expr,)* $fmt:literal $($arg:tt)*) => { - ManifestError::Diagnostic( + ConfigError::Diagnostic( miette::Report::from( miette::diagnostic!($($key = $value,)* $fmt $($arg)*) ).with_source_code(Arc::clone($code)) @@ -25,9 +25,9 @@ macro_rules! diagnostic { } #[derive(Debug, Diagnostic, Error)] -pub enum ManifestError { +pub enum ConfigError { #[error("{message}")] - #[diagnostic(code(arx::manifest::io))] + #[diagnostic(code(arx::config::io))] Io { message: String, #[source] @@ -43,27 +43,27 @@ pub enum ManifestError { Diagnostic(Report), } -/// Manifest options. These may be overriden from the CLI. +/// Config options. These may be overriden from the CLI. #[derive(Debug)] -pub struct ManifestOptions { - /// Whether to delete the manifest after we (successfully) done running. +pub struct ConfigOptions { + /// Whether to delete the config after we (successfully) done running. pub delete: bool, } -impl Default for ManifestOptions { +impl Default for ConfigOptions { fn default() -> Self { Self { delete: true } } } -/// Manifest options that may override parsed options. +/// Config options that may override parsed options. #[derive(Debug, Default)] -pub struct ManifestOptionsOverrides { - /// Whether to delete the manifest after we (successfully) done running. +pub struct ConfigOptionsOverrides { + /// Whether to delete the config after we (successfully) done running. pub delete: Option, } -/// Represents a manifest actions set that can be a vec of [ActionSuite] *or* [ActionSingle]. +/// Represents a config actions set that can be a vec of [ActionSuite] *or* [ActionSingle]. /// /// So, actions should be defined either like this: /// @@ -122,26 +122,26 @@ pub enum ActionSingle { Unknown(Unknown), } -/// Arx manifest (config). +/// Arx config. #[derive(Debug)] -pub struct Manifest { - /// Manifest directory. +pub struct Config { + /// Config directory. pub root: PathBuf, /// Source. Wrapped in an [Arc] for cheap clones. pub source: Arc, - /// Manifest file path. + /// Config file path. pub config: PathBuf, - /// Manifest options. - pub options: ManifestOptions, + /// Config options. + pub options: ConfigOptions, /// Actions. pub actions: Actions, } -impl Manifest { - /// Creates a new manifest from the given path and options. +impl Config { + /// Creates a new config from the given path and options. pub fn new(root: &Path) -> Self { let root = root.to_path_buf(); - let config = root.join(MANIFEST_NAME); + let config = root.join(CONFIG_NAME); // NOTE: Creating dummy source first, will be overwritten with actual data on load. This is done // because of some limitations around `NamedSource` and related entities like `SourceCode` which @@ -153,48 +153,48 @@ impl Manifest { Self { config, - options: ManifestOptions::default(), + options: ConfigOptions::default(), actions: Actions::Empty, source, root, } } - /// Tries to apply the given overrides to the manifest options. - pub fn override_with(&mut self, overrides: ManifestOptionsOverrides) { + /// Tries to apply the given overrides to the config options. + pub fn override_with(&mut self, overrides: ConfigOptionsOverrides) { if let Some(delete) = overrides.delete { self.options.delete = delete; } } - /// Tries to load and parse the manifest. - pub fn load(&mut self) -> Result<(), ManifestError> { + /// Tries to load and parse the config. + pub fn load(&mut self) -> Result<(), ConfigError> { if self.exists() { let doc = self.parse()?; - self.options = self.get_manifest_options(&doc)?; - self.actions = self.get_manifest_actions(&doc)?; + self.options = self.get_config_options(&doc)?; + self.actions = self.get_config_actions(&doc)?; } Ok(()) } - /// Checks if the manifest exists under `self.root`. + /// Checks if the config exists under `self.root`. fn exists(&self) -> bool { self.config.try_exists().unwrap_or(false) } - /// Reads and parses the manifest into a [KdlDocument]. - fn parse(&mut self) -> Result { - let filename = self.root.join(MANIFEST_NAME); + /// Reads and parses the config into a [KdlDocument]. + fn parse(&mut self) -> Result { + let filename = self.root.join(CONFIG_NAME); let contents = fs::read_to_string(&filename).map_err(|source| { - ManifestError::Io { - message: "Failed to read the manifest.".to_string(), + ConfigError::Io { + message: "Failed to read the config.".to_string(), source, } })?; - let document = contents.parse().map_err(ManifestError::Kdl)?; + let document = contents.parse().map_err(ConfigError::Kdl)?; // Replace dummy source with actual data. self.source = Arc::new(NamedSource::new(filename.display().to_string(), contents)); @@ -202,14 +202,14 @@ impl Manifest { Ok(document) } - /// Tries to parse options from the manifest. - fn get_manifest_options(&self, doc: &KdlDocument) -> Result { + /// Tries to parse options from the config. + fn get_config_options(&self, doc: &KdlDocument) -> Result { let options = doc .get("options") .and_then(KdlNode::children) .map(|children| { let nodes = children.nodes(); - let mut defaults = ManifestOptions::default(); + let mut defaults = ConfigOptions::default(); for node in nodes { let option = node.name().to_string().to_ascii_lowercase(); @@ -219,7 +219,7 @@ impl Manifest { defaults.delete = node.get_bool(0).ok_or_else(|| { diagnostic!( source = &self.source, - code = "arx::manifest::options", + code = "arx::config::options", labels = vec![LabeledSpan::at( node.span().to_owned(), "this node requires a boolean argument" @@ -240,12 +240,12 @@ impl Manifest { match options { | Some(Ok(options)) => Ok(options), | Some(Err(err)) => Err(err), - | None => Ok(ManifestOptions::default()), + | None => Ok(ConfigOptions::default()), } } - /// Tries to parse actions from the manifest. - fn get_manifest_actions(&self, doc: &KdlDocument) -> Result { + /// Tries to parse actions from the config. + fn get_config_actions(&self, doc: &KdlDocument) -> Result { #[inline] fn is_suite(node: &KdlNode) -> bool { node.name().value() == "suite" @@ -286,8 +286,8 @@ impl Manifest { } // Otherwise we have invalid actions block. else { - Err(ManifestError::Diagnostic(miette::miette!( - code = "arx::manifest::actions", + Err(ConfigError::Diagnostic(miette::miette!( + code = "arx::config::actions", "You can use either suites of actions or a flat list of single actions, not both." ))) } @@ -300,7 +300,7 @@ impl Manifest { } } - fn get_action_suite(&self, node: &KdlNode) -> Result { + fn get_action_suite(&self, node: &KdlNode) -> Result { let mut actions = Vec::new(); // Fail if we stumbled upon a nameless suite. @@ -316,7 +316,7 @@ impl Manifest { Ok(ActionSuite { name, actions }) } - fn get_action_single(&self, node: &KdlNode) -> Result { + fn get_action_single(&self, node: &KdlNode) -> Result { let kind = node.name().to_string().to_ascii_lowercase(); let action = match kind.as_str() { @@ -428,14 +428,14 @@ impl Manifest { Ok(action) } - fn get_arg_string(&self, node: &KdlNode) -> Result { + fn get_arg_string(&self, node: &KdlNode) -> Result { let start = node.span().offset(); let end = start + node.name().len(); node.get_string(0).ok_or_else(|| { diagnostic!( source = &self.source, - code = "arx::manifest::actions", + code = "arx::config::actions", labels = vec![ LabeledSpan::at(start..end, "this node requires a string argument"), LabeledSpan::at_offset(end, "argument should be here") @@ -445,11 +445,11 @@ impl Manifest { }) } - fn get_attr_string(&self, node: &KdlNode, key: &str) -> Result { + fn get_attr_string(&self, node: &KdlNode, key: &str) -> Result { node.get_string(key).ok_or_else(|| { diagnostic!( source = &self.source, - code = "arx::manifest::actions", + code = "arx::config::actions", labels = vec![LabeledSpan::at( node.span().to_owned(), format!("this node requires the `{key}` attribute") @@ -463,7 +463,7 @@ impl Manifest { &self, node: &'kdl KdlNode, nodes: Vec<&str>, - ) -> Result<&'kdl KdlDocument, ManifestError> { + ) -> Result<&'kdl KdlDocument, ConfigError> { let suffix = if nodes.len() > 1 { "s" } else { "" }; let nodes = nodes .iter() @@ -476,7 +476,7 @@ impl Manifest { node.children().ok_or_else(|| { diagnostic!( source = &self.source, - code = "arx::manifest::actions", + code = "arx::config::actions", labels = vec![LabeledSpan::at( node.span().to_owned(), format!("this node requires the following child nodes: {nodes}") @@ -486,11 +486,11 @@ impl Manifest { }) } - fn get_hint(&self, parent: &KdlNode, nodes: &KdlDocument) -> Result { + fn get_hint(&self, parent: &KdlNode, nodes: &KdlDocument) -> Result { let hint = nodes.get("hint").ok_or_else(|| { diagnostic!( source = &self.source, - code = "arx::manifest::actions", + code = "arx::config::actions", labels = vec![LabeledSpan::at( parent.span().to_owned(), "prompts require a `hint` child node" @@ -502,15 +502,11 @@ impl Manifest { self.get_arg_string(hint) } - fn get_options( - &self, - parent: &KdlNode, - nodes: &KdlDocument, - ) -> Result, ManifestError> { + fn get_options(&self, parent: &KdlNode, nodes: &KdlDocument) -> Result, ConfigError> { let options = nodes.get("options").ok_or_else(|| { diagnostic!( source = &self.source, - code = "arx::manifest::actions", + code = "arx::config::actions", labels = vec![LabeledSpan::at( parent.span().to_owned(), "select prompts require the `options` child node" @@ -534,7 +530,7 @@ impl Manifest { } else { return Err(diagnostic!( source = &self.source, - code = "arx::manifest::actions", + code = "arx::config::actions", labels = vec![LabeledSpan::at( span, "option values can be either strings or numbers" @@ -546,7 +542,7 @@ impl Manifest { let option = value.ok_or_else(|| { diagnostic!( source = &self.source, - code = "arx::manifest::actions", + code = "arx::config::actions", labels = vec![LabeledSpan::at( span, "failed to converted this value to a string" diff --git a/src/manifest/mod.rs b/src/config/mod.rs similarity index 65% rename from src/manifest/mod.rs rename to src/config/mod.rs index fc0f6d5..6f4d2e8 100644 --- a/src/manifest/mod.rs +++ b/src/config/mod.rs @@ -1,8 +1,8 @@ -pub use manifest::*; +pub use config::*; pub use utils::*; pub mod actions; pub mod prompts; -mod manifest; +mod config; mod utils; diff --git a/src/manifest/prompts.rs b/src/config/prompts.rs similarity index 100% rename from src/manifest/prompts.rs rename to src/config/prompts.rs diff --git a/src/manifest/utils.rs b/src/config/utils.rs similarity index 100% rename from src/manifest/utils.rs rename to src/config/utils.rs diff --git a/src/lib.rs b/src/lib.rs index 43cd610..b585c68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ pub(crate) mod actions; pub mod app; -pub(crate) mod manifest; +pub(crate) mod config; pub(crate) mod path; pub(crate) mod repository; pub(crate) mod spinner;