From 5b48028b594e4219788e35ea37b49e8c703e9ffd Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Fri, 22 Dec 2023 01:25:57 -0800 Subject: [PATCH] garden: reduce visibility and eliminate dead code Use pub(crate) to reduce the surface area of exposed symbols. --- src/cli.rs | 2 +- src/cmds/cmd.rs | 12 ++--- src/cmds/exec.rs | 2 +- src/cmds/grow.rs | 2 +- src/cmds/inspect.rs | 2 +- src/collections.rs | 4 +- src/eval.rs | 15 ++---- src/git.rs | 2 +- src/macros.rs | 4 +- src/model.rs | 115 +++++++++++++++++++++----------------------- src/path.rs | 10 ++-- src/query.rs | 15 +++--- src/syntax.rs | 24 +++------ 13 files changed, 92 insertions(+), 117 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index bb8975fd..3ffffce3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -87,7 +87,7 @@ impl MainOptions { } /// Parse a vector of debug level arguments to count how many have been set -pub fn debug_level(debug: &[String], name: &str) -> u8 { +fn debug_level(debug: &[String], name: &str) -> u8 { debug.iter().filter(|&x| x == name).count() as u8 } diff --git a/src/cmds/cmd.rs b/src/cmds/cmd.rs index 34b86a58..e8275a5c 100644 --- a/src/cmds/cmd.rs +++ b/src/cmds/cmd.rs @@ -174,11 +174,7 @@ pub fn main_custom(app_context: &model::ApplicationContext, arguments: &Vec Result { +fn cmd(app_context: &model::ApplicationContext, query: &str, params: &CmdParams) -> Result { // Mutable scope for app.get_root_config_mut() let config = app_context.get_root_config_mut(); // Resolve the tree query into a vector of tree contexts. @@ -191,7 +187,7 @@ pub fn cmd( } } -pub fn run_cmd_breadth_first( +fn run_cmd_breadth_first( app_context: &model::ApplicationContext, contexts: &[model::TreeContext], params: &CmdParams, @@ -256,7 +252,7 @@ pub fn run_cmd_breadth_first( Ok(exit_status) } -pub fn run_cmd_depth_first( +fn run_cmd_depth_first( app_context: &model::ApplicationContext, contexts: &[model::TreeContext], params: &CmdParams, @@ -382,7 +378,7 @@ fn run_cmd_vec( } /// Run cmd() over a Vec of tree queries -pub fn cmds(app: &model::ApplicationContext, params: &CmdParams) -> Result<()> { +fn cmds(app: &model::ApplicationContext, params: &CmdParams) -> Result<()> { let mut exit_status = errors::EX_OK; for query in ¶ms.queries { diff --git a/src/cmds/exec.rs b/src/cmds/exec.rs index 55e943d6..4501499b 100644 --- a/src/cmds/exec.rs +++ b/src/cmds/exec.rs @@ -41,7 +41,7 @@ pub fn main(app_context: &model::ApplicationContext, exec_options: &ExecOptions) } /// Execute a command over every tree in the evaluated tree query. -pub fn exec( +fn exec( app_context: &model::ApplicationContext, config: &model::Configuration, quiet: bool, diff --git a/src/cmds/grow.rs b/src/cmds/grow.rs index efac050d..4c088ba0 100644 --- a/src/cmds/grow.rs +++ b/src/cmds/grow.rs @@ -40,7 +40,7 @@ pub fn main(app: &model::ApplicationContext, options: &GrowOptions) -> Result<() } /// Create/update trees in the evaluated tree query. -pub fn grow( +fn grow( app_context: &model::ApplicationContext, configured_worktrees: &mut HashSet, quiet: bool, diff --git a/src/cmds/inspect.rs b/src/cmds/inspect.rs index 8539cb42..063bd9d9 100644 --- a/src/cmds/inspect.rs +++ b/src/cmds/inspect.rs @@ -27,7 +27,7 @@ pub fn main(app_context: &model::ApplicationContext, options: &mut InspectOption } /// Inspect every tree in the evaluated tree query -pub fn inspect( +fn inspect( app_context: &model::ApplicationContext, config: &model::Configuration, verbose: u8, diff --git a/src/collections.rs b/src/collections.rs index 256b8484..f671a367 100644 --- a/src/collections.rs +++ b/src/collections.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; /// Update a IndexSet "a" with the values from "b" #[inline] -pub fn append_indexset(a: &mut IndexSet, b: &IndexSet) +pub(crate) fn append_indexset(a: &mut IndexSet, b: &IndexSet) where T: Clone + Eq + Ord + std::hash::Hash, { @@ -15,7 +15,7 @@ where /// Update a Hashmap "a" with the values from "b". #[inline] -pub fn append_hashmap(a: &mut HashMap, b: &HashMap) +pub(crate) fn append_hashmap(a: &mut HashMap, b: &HashMap) where K: Clone + Eq + Ord + std::hash::Hash, V: Clone, diff --git a/src/eval.rs b/src/eval.rs index 693a2305..c5bb22a3 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -120,15 +120,6 @@ fn expand_tree_vars( Some(String::new()) } -/// Expand variables using a tree context. -fn _expand_tree_context_vars( - _app: &model::ApplicationContext, - _tree_context: &model::TreeContext, - _name: &str, -) -> Result, String> { - Ok(None) -} - /// Expand variables at global scope only fn expand_vars( app_context: &model::ApplicationContext, @@ -237,7 +228,7 @@ pub fn tree_value( /// Resolve an expression in a garden/tree/global scope for execution by a shell. /// This is used to generate the commands used internally by garden. -pub fn tree_value_for_shell( +fn tree_value_for_shell( app_context: &model::ApplicationContext, config: &model::Configuration, expr: &str, @@ -292,7 +283,7 @@ pub fn value( /// Evaluate `$ ` command strings, AKA "exec expressions". /// The result of the expression is the stdout output from the command. -pub fn exec_expression(string: &str, pathbuf: Option) -> String { +fn exec_expression(string: &str, pathbuf: Option) -> String { let cmd = syntax::trim_exec(string); let mut proc = subprocess::Exec::shell(cmd); // Run the exec expression inside the tree's directory when specified. @@ -337,7 +328,7 @@ pub fn multi_variable( } /// Evaluate a variable in the given context for execution in a shell -pub fn variables_for_shell( +fn variables_for_shell( app_context: &model::ApplicationContext, config: &model::Configuration, variables: &mut Vec, diff --git a/src/git.rs b/src/git.rs index 4c9a8792..ffb9fa87 100644 --- a/src/git.rs +++ b/src/git.rs @@ -89,7 +89,7 @@ pub fn branches(path: &std::path::Path) -> Vec { } /// Return the current branch name for the specified repository path. -pub fn branch(path: &std::path::Path) -> Option { +pub(crate) fn branch(path: &std::path::Path) -> Option { let cmd = ["git", "symbolic-ref", "--quiet", "--short", "HEAD"]; let exec = cmd::exec_in_dir(&cmd, &path); if let Ok(output) = cmd::stdout_to_string(exec) { diff --git a/src/macros.rs b/src/macros.rs index aee7af26..e42dfb54 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -2,7 +2,7 @@ /// /// Parameters: /// - `args`: A `std::fmt::Arguments` -pub fn error(args: std::fmt::Arguments) { +pub(crate) fn error(args: std::fmt::Arguments) { eprintln!("error: {args}"); } @@ -10,7 +10,7 @@ pub fn error(args: std::fmt::Arguments) { /// /// Parameters: /// - `args`: A `std::fmt::Arguments` -pub fn debug(args: std::fmt::Arguments) { +pub(crate) fn debug(args: std::fmt::Arguments) { eprintln!("debug: {args}"); } diff --git a/src/model.rs b/src/model.rs index 7d4f5bb7..eadb4f97 100644 --- a/src/model.rs +++ b/src/model.rs @@ -226,7 +226,7 @@ impl Tree { &self.name } - pub fn get_name_mut(&mut self) -> &mut String { + pub(crate) fn get_name_mut(&mut self) -> &mut String { &mut self.name } @@ -234,16 +234,16 @@ impl Tree { &self.path } - pub fn get_path_mut(&mut self) -> &mut Variable { + pub(crate) fn get_path_mut(&mut self) -> &mut Variable { &mut self.path } - pub fn path_is_valid(&self) -> bool { + pub(crate) fn path_is_valid(&self) -> bool { self.path.get_value().is_some() } /// Build a canonicalized pathbuf for the current tree. - pub fn canonical_pathbuf(&self) -> Option { + pub(crate) fn canonical_pathbuf(&self) -> Option { if let Some(pathbuf) = self.pathbuf() { if let Ok(canon_path) = pathbuf.canonicalize() { return Some(canon_path); @@ -254,7 +254,7 @@ impl Tree { } /// Build a pathbuf for the current tree. - pub fn pathbuf(&self) -> Option { + pub(crate) fn pathbuf(&self) -> Option { if !self.path_is_valid() { return None; } @@ -271,7 +271,7 @@ impl Tree { } } - pub fn symlink_as_ref(&self) -> Result<&String, errors::GardenError> { + pub(crate) fn symlink_as_ref(&self) -> Result<&String, errors::GardenError> { match self.symlink.get_value() { Some(value) => Ok(value), None => Err(errors::GardenError::ConfigurationError(format!( @@ -281,7 +281,7 @@ impl Tree { } } - pub fn reset_variables(&self) { + pub(crate) fn reset_variables(&self) { // self.path is a variable but it is not reset because // the tree path is evaluated once when the configuration // is first read, and never again. @@ -297,7 +297,7 @@ impl Tree { } /// Copy the guts of another tree into the current tree. - pub fn clone_from_tree(&mut self, tree: &Tree) { + pub(crate) fn clone_from_tree(&mut self, tree: &Tree) { append_hashmap(&mut self.commands, &tree.commands); append_hashmap(&mut self.gitconfig, &tree.gitconfig); append_hashmap(&mut self.variables, &tree.variables); @@ -337,7 +337,7 @@ impl Tree { } /// Update internal flags in response to newly read data. - pub fn update_flags(&mut self) { + pub(crate) fn update_flags(&mut self) { if !self.symlink.is_empty() { self.is_symlink = true; } @@ -364,11 +364,11 @@ impl Group { } /// Return an owned copy of the name field. - pub fn get_name_owned(&self) -> String { + pub(crate) fn get_name_owned(&self) -> String { self.get_name().to_owned() } - pub fn get_name_mut(&mut self) -> &mut String { + pub(crate) fn get_name_mut(&mut self) -> &mut String { &mut self.name } } @@ -390,12 +390,12 @@ impl Template { &self.name } - pub fn get_name_mut(&mut self) -> &mut String { + pub(crate) fn get_name_mut(&mut self) -> &mut String { &mut self.name } /// Apply this template onto the specified tree. - pub fn apply(&self, tree: &mut Tree) { + pub(crate) fn apply(&self, tree: &mut Tree) { tree.clone_from_tree(&self.tree); } } @@ -419,7 +419,7 @@ impl Garden { &self.name } - pub fn get_name_mut(&mut self) -> &mut String { + pub(crate) fn get_name_mut(&mut self) -> &mut String { &mut self.name } } @@ -458,8 +458,8 @@ pub struct Configuration { /// highest precedence and override variables defined by any configuration or tree. pub override_variables: VariableHashMap, pub verbose: u8, - pub tree_branches: bool, - pub parent_id: Option, + pub(crate) tree_branches: bool, + pub(crate) parent_id: Option, id: Option, } @@ -477,7 +477,7 @@ impl Configuration { } } - pub fn initialize(&mut self, app_context: &ApplicationContext) { + pub(crate) fn initialize(&mut self, app_context: &ApplicationContext) { // Evaluate garden.root let expr = String::from(self.root.get_expr()); let value = eval::value(app_context, self, &expr); @@ -495,7 +495,7 @@ impl Configuration { self.reset(); } - pub fn update( + pub(crate) fn update( &mut self, app_context: &ApplicationContext, config: Option<&std::path::PathBuf>, @@ -569,7 +569,7 @@ impl Configuration { } /// Apply MainOptions to a Configuration. - pub fn update_options( + pub(crate) fn update_options( &mut self, options: &cli::MainOptions, ) -> Result<(), errors::GardenError> { @@ -618,7 +618,7 @@ impl Configuration { Ok(()) } - pub fn reset(&mut self) { + pub(crate) fn reset(&mut self) { // Reset variables to allow for tree-scope evaluation self.reset_variables(); @@ -688,7 +688,7 @@ impl Configuration { } /// Return a path string relative to the garden root - pub fn tree_path(&self, path: &str) -> String { + pub(crate) fn tree_path(&self, path: &str) -> String { if std::path::PathBuf::from(path).is_absolute() { // Absolute path, nothing to do path.into() @@ -702,7 +702,7 @@ impl Configuration { } /// Return a pathbuf relative to the garden root. - pub fn relative_pathbuf(&self, path: &str) -> std::path::PathBuf { + pub(crate) fn relative_pathbuf(&self, path: &str) -> std::path::PathBuf { let pathbuf = std::path::PathBuf::from(path); if pathbuf.is_absolute() { // Absolute path, nothing to do @@ -721,13 +721,13 @@ impl Configuration { } /// Evaluate and return a path string relative to the garden root. - pub fn eval_tree_path(&mut self, app_context: &ApplicationContext, path: &str) -> String { + fn eval_tree_path(&mut self, app_context: &ApplicationContext, path: &str) -> String { let value = eval::value(app_context, self, path); self.tree_path(&value) } /// Resolve a pathbuf relative to the config directory. - pub fn config_pathbuf(&self, path: &str) -> Option { + pub(crate) fn config_pathbuf(&self, path: &str) -> Option { let path_buf = std::path::PathBuf::from(path); if path_buf.is_absolute() { // Absolute path, nothing to do @@ -745,7 +745,7 @@ impl Configuration { /// Resolve a pathbuf relative to specified include file or the config directory. /// Returns the first file found. The include file's directory is checked first. - pub fn config_pathbuf_from_include( + fn config_pathbuf_from_include( &self, include_path: &std::path::Path, path: &str, @@ -772,7 +772,7 @@ impl Configuration { } /// Resolve a path string relative to the config directory. - pub fn config_path(&self, path: &str) -> String { + fn config_path(&self, path: &str) -> String { if let Some(path_buf) = self.config_pathbuf(path) { path_buf.to_string_lossy().to_string() } else { @@ -781,23 +781,13 @@ impl Configuration { } /// Evaluate and resolve a path string and relative to the config directory. - pub fn eval_config_path(&self, app_context: &ApplicationContext, path: &str) -> String { + pub(crate) fn eval_config_path(&self, app_context: &ApplicationContext, path: &str) -> String { let value = eval::value(app_context, self, path); self.config_path(&value) } - /// Evaluate and resolve a pathbuf relative to the config directory. - pub fn eval_config_pathbuf( - &self, - app_context: &ApplicationContext, - path: &str, - ) -> Option { - let value = eval::value(app_context, self, path); - self.config_pathbuf(&value) - } - /// Evaluate and resolve a pathbuf relative to the config directory for "includes". - pub fn eval_config_pathbuf_from_include( + pub(crate) fn eval_config_pathbuf_from_include( &self, app_context: &ApplicationContext, include_path: Option<&std::path::Path>, @@ -814,7 +804,7 @@ impl Configuration { } /// Reset resolved variables - pub fn reset_variables(&mut self) { + pub(crate) fn reset_variables(&mut self) { for var in self.variables.values() { var.reset(); } @@ -830,21 +820,21 @@ impl Configuration { } /// Set the ConfigId from the Arena for this configuration. - pub fn set_id(&mut self, id: ConfigId) { + pub(crate) fn set_id(&mut self, id: ConfigId) { self.id = Some(id); } - pub fn get_id(&self) -> Option { + pub(crate) fn get_id(&self) -> Option { self.id } /// Set the parent ConfigId from the Arena for this configuration. - pub fn set_parent(&mut self, id: ConfigId) { + pub(crate) fn set_parent(&mut self, id: ConfigId) { self.parent_id = Some(id); } /// Set the config path and the dirname fields - pub fn set_path(&mut self, path: std::path::PathBuf) { + pub(crate) fn set_path(&mut self, path: std::path::PathBuf) { let mut dirname = path.clone(); dirname.pop(); @@ -853,7 +843,7 @@ impl Configuration { } /// Get the config path if it is defined. - pub fn get_path(&self) -> Result<&std::path::PathBuf, errors::GardenError> { + pub(crate) fn get_path(&self) -> Result<&std::path::PathBuf, errors::GardenError> { self.path .as_ref() .ok_or_else(|| errors::GardenError::AssertionError("cfg.path is unset".into())) @@ -861,7 +851,7 @@ impl Configuration { /// Get a path string for this configuration. /// Returns the current directory when the configuration does not have a valid path. - pub fn get_path_for_display(&self) -> String { + pub(crate) fn get_path_for_display(&self) -> String { let default_pathbuf = std::path::PathBuf::from("."); self.path .as_ref() @@ -871,13 +861,13 @@ impl Configuration { } /// Return true if the configuration contains the named graft. - pub fn contains_graft(&self, name: &str) -> bool { + pub(crate) fn contains_graft(&self, name: &str) -> bool { let graft_name = syntax::trim(name); self.grafts.contains_key(graft_name) } /// Return a graft by name. - pub fn get_graft(&self, name: &str) -> Result<&Graft, errors::GardenError> { + pub(crate) fn get_graft(&self, name: &str) -> Result<&Graft, errors::GardenError> { let graft_name = syntax::trim(name); self.grafts.get(graft_name).ok_or_else(|| { errors::GardenError::ConfigurationError(format!("{name}: no such graft")) @@ -886,7 +876,7 @@ impl Configuration { /// Parse a "graft::value" string and return the ConfigId for the graft and the /// remaining unparsed "value". - pub fn get_graft_id<'a>( + pub(crate) fn get_graft_id<'a>( &self, value: &'a str, ) -> Result<(ConfigId, &'a str), errors::GardenError> { @@ -907,12 +897,12 @@ impl Configuration { } /// Find a tree by name and return a reference if it exists. - pub fn get_tree(&self, name: &str) -> Option<&Tree> { + pub(crate) fn get_tree(&self, name: &str) -> Option<&Tree> { self.trees.get(name) } /// Return a pathbuf for the specified Tree index - pub fn get_tree_pathbuf(&self, tree_name: &str) -> Option { + pub(crate) fn get_tree_pathbuf(&self, tree_name: &str) -> Option { self.get_tree(tree_name) .map(|tree| tree.canonical_pathbuf()) .unwrap_or(None) @@ -947,7 +937,7 @@ impl Graft { self.id } - pub fn set_id(&mut self, id: ConfigId) { + pub(crate) fn set_id(&mut self, id: ConfigId) { self.id = Some(id); } } @@ -1115,7 +1105,7 @@ impl ColorMode { } } - pub fn update(&mut self) { + pub(crate) fn update(&mut self) { if *self == ColorMode::Auto { // Speedup future calls to is_enabled() by performing the "auto" // is_terminal() check once and caching the result. @@ -1135,7 +1125,7 @@ impl ColorMode { // Color is an alias for yansi::Paint. pub type Color = yansi::Paint; -pub fn display_missing_tree(tree: &Tree, path: &str, verbose: u8) -> String { +pub(crate) fn display_missing_tree(tree: &Tree, path: &str, verbose: u8) -> String { if verbose > 0 { format!( "{} {} {} {}", @@ -1154,7 +1144,12 @@ pub fn display_missing_tree(tree: &Tree, path: &str, verbose: u8) -> String { } } -pub fn display_tree(tree: &Tree, path_str: &str, tree_branches: bool, verbose: u8) -> String { +pub(crate) fn display_tree( + tree: &Tree, + path_str: &str, + tree_branches: bool, + verbose: u8, +) -> String { if verbose > 0 { if tree_branches { if let Some(path) = tree.canonical_pathbuf() { @@ -1197,7 +1192,7 @@ pub fn display_tree(tree: &Tree, path_str: &str, tree_branches: bool, verbose: u } /// Print a tree if it exists, otherwise print a missing tree -pub fn print_tree(tree: &Tree, tree_branches: bool, verbose: u8, quiet: bool) -> bool { +pub(crate) fn print_tree(tree: &Tree, tree_branches: bool, verbose: u8, quiet: bool) -> bool { if let Ok(path) = tree.path_as_ref() { // Sparse gardens/missing trees are expected. Skip these entries. if !std::path::PathBuf::from(&path).exists() { @@ -1217,7 +1212,7 @@ pub fn print_tree(tree: &Tree, tree_branches: bool, verbose: u8, quiet: bool) -> } /// Print a tree -pub fn print_tree_details(tree: &Tree, tree_branches: bool, verbose: u8, quiet: bool) { +pub(crate) fn print_tree_details(tree: &Tree, tree_branches: bool, verbose: u8, quiet: bool) { if !quiet { if let Ok(path) = tree.path_as_ref() { eprintln!("{}", display_tree(tree, path, tree_branches, verbose)); @@ -1323,7 +1318,7 @@ impl ApplicationContext { } #[allow(clippy::mut_from_ref)] - pub fn get_config_mut(&self, id: ConfigId) -> &mut Configuration { + pub(crate) fn get_config_mut(&self, id: ConfigId) -> &mut Configuration { unsafe { (*self.arena.as_ptr()).get_mut(id).unwrap().get_mut() } } @@ -1335,12 +1330,12 @@ impl ApplicationContext { self.get_config(self.get_root_id()) } - pub fn get_root_config_mut(&self) -> &mut Configuration { + pub(crate) fn get_root_config_mut(&self) -> &mut Configuration { self.get_config_mut(self.get_root_id()) } /// Add a child Configuration graft onto the parent ConfigId. - pub fn add_graft(&self, parent: ConfigId, config: Configuration) -> ConfigId { + pub(crate) fn add_graft(&self, parent: ConfigId, config: Configuration) -> ConfigId { let graft_id = self.arena.borrow_mut().new_node(config); // Take ownership of config. parent.append(graft_id, &mut self.arena.borrow_mut()); @@ -1350,7 +1345,7 @@ impl ApplicationContext { } /// Attach a graft to the configuration specified by ConfigId. - pub fn add_graft_config( + pub(crate) fn add_graft_config( &self, config_id: ConfigId, graft_name: &str, diff --git a/src/path.rs b/src/path.rs index d83d82bd..5627ad2a 100644 --- a/src/path.rs +++ b/src/path.rs @@ -1,17 +1,17 @@ use super::errors; /// Return the current directoy as a PathBuf. -pub fn current_dir() -> std::path::PathBuf { +pub(crate) fn current_dir() -> std::path::PathBuf { std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")) } /// Return the current directory as a string. -pub fn current_dir_string() -> String { +pub(crate) fn current_dir_string() -> String { current_dir().to_string_lossy().to_string() } /// Return the home directory for the current user. -pub fn home_dir() -> std::path::PathBuf { +pub(crate) fn home_dir() -> std::path::PathBuf { dirs::home_dir().unwrap_or_else(|| std::path::PathBuf::from("/tmp")) } @@ -23,7 +23,7 @@ pub fn abspath(path: &std::path::Path) -> std::path::PathBuf { } /// Strip a prefix from a path. -pub fn strip_prefix( +pub(crate) fn strip_prefix( root: &std::path::Path, path: &std::path::Path, ) -> Result { @@ -44,7 +44,7 @@ pub fn strip_prefix( } /// Strip a prefix from a path. Returns a path as a string. -pub fn strip_prefix_into_string( +pub(crate) fn strip_prefix_into_string( root: &std::path::Path, path: &std::path::Path, ) -> Result { diff --git a/src/query.rs b/src/query.rs index f8bdaa77..c8405e93 100644 --- a/src/query.rs +++ b/src/query.rs @@ -80,7 +80,7 @@ pub fn resolve_trees( /// - config: `&garden::model::Configuration` /// - pattern: `&glob::Pattern` -pub fn garden_trees( +fn garden_trees( app_context: &model::ApplicationContext, config: &model::Configuration, pattern: &glob::Pattern, @@ -254,12 +254,15 @@ pub fn trees_from_pattern( } /// Return a tree context for the specified path string. -pub fn tree_from_path(config: &model::Configuration, path: &str) -> Option { +pub(crate) fn tree_from_path( + config: &model::Configuration, + path: &str, +) -> Option { tree_from_pathbuf(config, &std::path::PathBuf::from(path)) } /// Return a tree context for the specified path. -pub fn tree_from_pathbuf( +fn tree_from_pathbuf( config: &model::Configuration, path: &std::path::Path, ) -> Option { @@ -297,7 +300,7 @@ pub fn tree_name_from_path( /// Return the name of an existing tree from an absolute path. -pub fn tree_name_from_abspath( +pub(crate) fn tree_name_from_abspath( config: &model::Configuration, path: &std::path::Path, ) -> Option { @@ -364,7 +367,7 @@ pub fn tree_context( garden: garden_name.into(), } })?; - let contexts = query::garden_trees(app_context, config, &pattern); + let contexts = garden_trees(app_context, config, &pattern); if contexts.is_empty() { return Err(errors::GardenError::GardenNotFound { @@ -420,7 +423,7 @@ pub fn find_tree( } /// Return a path that that is either the tree's path or the tree's shared worktree path. -pub fn shared_worktree_path( +pub(crate) fn shared_worktree_path( app_context: &model::ApplicationContext, config: &model::Configuration, ctx: &model::TreeContext, diff --git a/src/syntax.rs b/src/syntax.rs index 546a173e..0cd8b4d4 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -1,10 +1,10 @@ /// Return true if the string contains 0-9 digits only -pub fn is_digit(string: &str) -> bool { +pub(crate) fn is_digit(string: &str) -> bool { string.chars().all(|c| c.is_ascii_digit()) } /// Return true if `string` is an `$ exec` expression. -pub fn is_exec(string: &str) -> bool { +pub(crate) fn is_exec(string: &str) -> bool { string.starts_with("$ ") } @@ -24,12 +24,12 @@ pub fn is_tree(string: &str) -> bool { } /// Return true if `string` is a variable "replace" operation. -pub fn is_append_op(string: &str) -> bool { +pub(crate) fn is_append_op(string: &str) -> bool { string.ends_with('+') } /// Return true if `string` is a variable "append" operation. -pub fn is_replace_op(string: &str) -> bool { +pub(crate) fn is_replace_op(string: &str) -> bool { string.ends_with('=') } @@ -44,7 +44,7 @@ pub fn is_git_dir(string: &str) -> bool { } /// Trim garden, group, and tree prefixes -pub fn trim(string: &str) -> &str { +pub(crate) fn trim(string: &str) -> &str { let needs_trim = is_group(string) || is_tree(string) || is_garden(string); if !string.is_empty() && needs_trim { &string[1..] @@ -64,16 +64,6 @@ pub fn trim_exec(string: &str) -> &str { } } -/// Trim "+" and "=" suffixes from strings. -pub fn trim_op(string: &str) -> &str { - let needs_trim = is_append_op(string) || is_replace_op(string); - if !string.len() > 1 && needs_trim { - &string[..string.len() - 1] - } else { - string - } -} - /// Trim "+" and "=" suffixes in-place. pub fn trim_op_inplace(string: &mut String) { let len = string.len(); @@ -171,7 +161,7 @@ pub fn escape_shell_variables(string: &str) -> String { } /// Return the value of a boolean as a string. -pub fn bool_to_string(value: bool) -> String { +pub(crate) fn bool_to_string(value: bool) -> String { match value { true => string!("true"), false => string!("false"), @@ -179,7 +169,7 @@ pub fn bool_to_string(value: bool) -> String { } /// Return the value of a string as a boolean. Accepts "true", "false", "1" and "0". -pub fn string_to_bool(value: &str) -> Option { +pub(crate) fn string_to_bool(value: &str) -> Option { match value { "true" | "1" => Some(true), "false" | "0" => Some(false),