Skip to content

Commit d93b523

Browse files
committed
model: refactor Variable initialization
Use specific functions for building resolved and unresolved variables.
1 parent b051222 commit d93b523

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

src/cmds/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub fn main(options: &cli::MainOptions, init_options: &mut InitOptions) -> Resul
9292
};
9393

9494
let mut config = model::Configuration::new();
95-
config.root = model::Variable::new(init_options.root.clone(), None);
95+
config.root = model::Variable::from_expr(init_options.root.clone());
9696
config.root_path.clone_from(&dirname);
9797
config.path = Some(config_path.clone());
9898

src/config/reader.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ fn parse_recursive(
125125
// Provide GARDEN_ROOT.
126126
config.variables.insert(
127127
string!(constants::GARDEN_ROOT),
128-
model::Variable::new(config.root.get_expr().to_string(), None),
128+
model::Variable::from_expr(config.root.get_expr().to_string()),
129129
);
130130

131131
if let Some(config_path_raw) = config.dirname.as_ref() {
132132
// Calculate an absolute path for GARDEN_CONFIG_DIR.
133133
if let Ok(config_path) = path::canonicalize(config_path_raw) {
134134
config.variables.insert(
135135
string!(constants::GARDEN_CONFIG_DIR),
136-
model::Variable::new(config_path.to_string_lossy().to_string(), None),
136+
model::Variable::from_expr(config_path.to_string_lossy().to_string()),
137137
);
138138
}
139139
}
@@ -377,7 +377,7 @@ fn get_indexset_str(yaml: &Yaml, values: &mut StringSet) -> bool {
377377
/// Construct a model::Variable from a ran YAML object.
378378
fn variable_from_yaml(yaml: &Yaml) -> Option<model::Variable> {
379379
match yaml {
380-
Yaml::String(yaml_str) => Some(model::Variable::new(yaml_str.to_string(), None)),
380+
Yaml::String(yaml_str) => Some(model::Variable::from_expr(yaml_str.to_string())),
381381
Yaml::Array(yaml_array) => {
382382
// If we see an array we loop over so that the first value wins.
383383
for array_value in yaml_array.iter().rev() {
@@ -390,16 +390,13 @@ fn variable_from_yaml(yaml: &Yaml) -> Option<model::Variable> {
390390
// Integers are already resolved.
391391
let int_value = yaml_int.to_string();
392392

393-
Some(model::Variable::new(int_value.to_string(), Some(int_value)))
393+
Some(model::Variable::from_resolved_expr(int_value))
394394
}
395395
Yaml::Boolean(yaml_bool) => {
396396
// Booleans are already resolved.
397397
let bool_value = syntax::bool_to_string(*yaml_bool);
398398

399-
Some(model::Variable::new(
400-
bool_value.to_string(),
401-
Some(bool_value),
402-
))
399+
Some(model::Variable::from_resolved_expr(bool_value))
403400
}
404401
_ => {
405402
// dump_node(yaml, 1, "");
@@ -567,7 +564,7 @@ fn get_template(
567564
template
568565
.tree
569566
.remotes
570-
.insert(string!(constants::ORIGIN), model::Variable::new(url, None));
567+
.insert(string!(constants::ORIGIN), model::Variable::from_expr(url));
571568
return template;
572569
}
573570
// If a `<url>` is configured then populate the "origin" remote.
@@ -576,7 +573,7 @@ fn get_template(
576573
template
577574
.tree
578575
.remotes
579-
.insert(string!(constants::ORIGIN), model::Variable::new(url, None));
576+
.insert(string!(constants::ORIGIN), model::Variable::from_expr(url));
580577
}
581578
}
582579

@@ -675,7 +672,7 @@ fn get_tree_from_url(name: &Yaml, url: &str) -> model::Tree {
675672
}
676673
tree.remotes.insert(
677674
string!(constants::ORIGIN),
678-
model::Variable::new(url.to_string(), None),
675+
model::Variable::from_expr(url.to_string()),
679676
);
680677

681678
tree
@@ -709,7 +706,7 @@ fn get_tree_fields(value: &Yaml, tree: &mut model::Tree) {
709706
if get_str(&value[constants::URL], &mut url) {
710707
tree.remotes.insert(
711708
tree.default_remote.to_string(),
712-
model::Variable::new(url, None),
709+
model::Variable::from_expr(url),
713710
);
714711
}
715712
}
@@ -806,7 +803,7 @@ fn get_str_variables_map(yaml: &Yaml, remotes: &mut model::VariableMap) {
806803
if let (Some(name_str), Some(value_str)) = (name.as_str(), value.as_str()) {
807804
remotes.insert(
808805
name_str.to_string(),
809-
model::Variable::new(value_str.to_string(), None),
806+
model::Variable::from_expr(value_str.to_string()),
810807
);
811808
}
812809
}

src/model.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,20 @@ impl Clone for Variable {
6969
}
7070

7171
impl Variable {
72-
pub(crate) fn new(expr: String, value: Option<String>) -> Self {
72+
/// Create an un-evaluated variable from an expression string.
73+
pub(crate) fn from_expr(expr: String) -> Self {
7374
Variable {
7475
expr,
75-
value: UnsafeCell::new(value),
76+
value: UnsafeCell::new(None),
77+
evaluating: Cell::new(false),
78+
}
79+
}
80+
81+
/// Create a resolved variable from a string that is both the expression and value.
82+
pub(crate) fn from_resolved_expr(expr: String) -> Self {
83+
Variable {
84+
expr: expr.clone(),
85+
value: UnsafeCell::new(Some(expr)),
7686
evaluating: Cell::new(false),
7787
}
7888
}
@@ -278,13 +288,13 @@ impl Tree {
278288
pub(crate) fn add_builtin_variables(&mut self) {
279289
self.variables.insert(
280290
string!(constants::TREE_NAME),
281-
Variable::new(self.get_name().clone(), None),
291+
Variable::from_expr(self.get_name().clone()),
282292
);
283293

284294
// Register the ${TREE_PATH} variable.
285295
self.variables.insert(
286296
string!(constants::TREE_PATH),
287-
Variable::new(self.get_path().get_expr().clone(), None),
297+
Variable::from_expr(self.get_path().get_expr().clone()),
288298
);
289299
}
290300

@@ -783,7 +793,7 @@ impl Configuration {
783793
}
784794
_ => {
785795
self.override_variables
786-
.insert(name, Variable::new(expr, None));
796+
.insert(name, Variable::from_expr(expr));
787797
}
788798
}
789799
}
@@ -795,7 +805,7 @@ impl Configuration {
795805
let quiet_string = if self.quiet || quiet { "--quiet" } else { "" }.to_string();
796806
self.variables.insert(
797807
string!(constants::GARDEN_CMD_QUIET),
798-
Variable::new(quiet_string.clone(), Some(quiet_string)),
808+
Variable::from_resolved_expr(quiet_string),
799809
);
800810
let verbose = self.verbose + verbose;
801811
let verbose_string = if verbose > 0 {
@@ -805,7 +815,7 @@ impl Configuration {
805815
};
806816
self.variables.insert(
807817
string!(constants::GARDEN_CMD_VERBOSE),
808-
Variable::new(verbose_string.clone(), Some(verbose_string)),
818+
Variable::from_resolved_expr(verbose_string),
809819
);
810820
}
811821

0 commit comments

Comments
 (0)