Skip to content

Commit

Permalink
model: use Option<&T> instead of &Option<T>
Browse files Browse the repository at this point in the history
Simplify semantics for callers by always passing Option types directly
instead of passing a reference to an option. This saves a level of
indirection while also making things easier on callers.
  • Loading branch information
davvid committed Jul 1, 2023
1 parent 0e166c7 commit 066ea00
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub fn read_grafts_recursive(

// Read child grafts recursively after the immutable scope has ended.
for (graft_name, path, root) in details {
app.add_graft_config(id, &graft_name, &path, &root)?;
app.add_graft_config(id, &graft_name, &path, root.as_ref())?;
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn expand_vars(
}

let expr = var.get_expr();
let result = value(&app_context, config, expr);
let result = value(app_context, config, expr);
var.set_value(result.clone());

return Some(result);
Expand Down
22 changes: 11 additions & 11 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,8 @@ impl Configuration {
pub fn update(
&mut self,
app_context: &ApplicationContext,
config: &Option<std::path::PathBuf>,
root: &Option<std::path::PathBuf>,
config: Option<&std::path::PathBuf>,
root: Option<&std::path::PathBuf>,
config_verbose: u8,
parent: Option<ConfigId>,
) -> Result<(), errors::GardenError> {
Expand Down Expand Up @@ -930,8 +930,8 @@ impl Graft {
&self.name
}

pub fn get_id(&self) -> &Option<ConfigId> {
&self.id
pub fn get_id(&self) -> Option<ConfigId> {
self.id
}

pub fn set_id(&mut self, id: ConfigId) {
Expand Down Expand Up @@ -1214,8 +1214,8 @@ impl ApplicationContext {
let config_verbose = options.debug_level("config");
app_context.get_root_config_mut().update(
&app_context,
&options.config,
&options.root,
options.config.as_ref(),
options.root.as_ref(),
config_verbose,
None,
)?;
Expand All @@ -1230,14 +1230,14 @@ impl ApplicationContext {
/// Construct an ApplicationContext from a path and root using default MainOptions.
pub fn from_path_and_root(
pathbuf: std::path::PathBuf,
root: &Option<std::path::PathBuf>,
root: Option<&std::path::PathBuf>,
) -> Result<Self, errors::GardenError> {
let options = cli::MainOptions::new();
let app_context = Self::new(options.clone());
let config_verbose = options.debug_level("config");
app_context.get_root_config_mut().update(
&app_context,
&Some(pathbuf),
Some(&pathbuf),
root,
config_verbose,
None,
Expand All @@ -1250,7 +1250,7 @@ impl ApplicationContext {

/// Construct an ApplicationContext from a path using default MainOptions.
pub fn from_path(pathbuf: std::path::PathBuf) -> Result<Self, errors::GardenError> {
Self::from_path_and_root(pathbuf, &None)
Self::from_path_and_root(pathbuf, None)
}

/// Construct an ApplicationContext from a path using default MainOptions.
Expand Down Expand Up @@ -1306,12 +1306,12 @@ impl ApplicationContext {
config_id: ConfigId,
graft_name: &str,
path: &std::path::Path,
root: &Option<std::path::PathBuf>,
root: Option<&std::path::PathBuf>,
) -> Result<(), errors::GardenError> {
let path = path.to_path_buf();
let config_verbose = self.options.debug_level("config");
let mut graft_config = Configuration::new();
graft_config.update(self, &Some(path), root, config_verbose, Some(config_id))?;
graft_config.update(self, Some(&path), root, config_verbose, Some(config_id))?;

// The app Arena takes ownershp of the Configuration.
let graft_id = self.add_graft(config_id, graft_config);
Expand Down
2 changes: 1 addition & 1 deletion tests/plant_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fn plant_git_worktree() -> Result<()> {
let pathbuf = fixture.pathbuf("garden.yaml");
let app_context = garden::model::ApplicationContext::from_path_and_root(
pathbuf,
&Some(fixture.root_pathbuf()),
Some(&fixture.root_pathbuf()),
)?;
let cfg = app_context.get_root_config();
assert_eq!(2, cfg.trees.len());
Expand Down
2 changes: 1 addition & 1 deletion tests/query_config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn tree_name_from_pathbuf() -> Result<()> {
let pathbuf = std::path::PathBuf::from("tests/data/worktree.yaml");
let app_context = garden::model::ApplicationContext::from_path_and_root(
pathbuf,
&Some(fixture.root_pathbuf()),
Some(&fixture.root_pathbuf()),
)?;
let cfg = app_context.get_root_config();

Expand Down

0 comments on commit 066ea00

Please sign in to comment.