diff options
author | Ben Bridle <ben@derelict.engineering> | 2025-03-11 16:53:51 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2025-03-11 16:53:57 +1300 |
commit | 2c94211270c77005f296d2d8691ec13340ab5555 (patch) | |
tree | fc1f69902d51af4e1b3a14eaf2f90a7213c4eed2 | |
parent | 0724fda47e7146ff682eef101b5260331cb11d26 (diff) | |
download | switchboard-2c94211270c77005f296d2d8691ec13340ab5555.zip |
Don't consume values when using .get() method
This didn't make much sense, there are valid reasons to read the same
value multiple times.
-rw-r--r-- | src/switchboard.rs | 4 | ||||
-rw-r--r-- | src/value.rs | 20 |
2 files changed, 12 insertions, 12 deletions
diff --git a/src/switchboard.rs b/src/switchboard.rs index 1c3ed81..7aff7fd 100644 --- a/src/switchboard.rs +++ b/src/switchboard.rs @@ -130,8 +130,8 @@ impl Switchboard { std::process::exit(1); } - pub fn get(&mut self, name: &str) -> QueriedValue { - match self.values.remove(name) { + pub fn get(&self, name: &str) -> &QueriedValue { + match self.values.get(name) { Some(value) => value, None => panic!("Name has not been defined: {name:?}"), } diff --git a/src/value.rs b/src/value.rs index 7725490..0c70b47 100644 --- a/src/value.rs +++ b/src/value.rs @@ -6,10 +6,10 @@ use std::path::PathBuf; macro_rules! as_number { ($type:ty, $name:expr, $error:ty) => { paste::paste! { - pub fn [< as_ $type >](&mut self) -> $type { + pub fn [< as_ $type >](&self) -> $type { self.[< as_ $type _opt >]().unwrap_or_else(|| self.missing("number")) } - pub fn [< as_ $type _opt>](&mut self) -> Option<$type> { + pub fn [< as_ $type _opt>](&self) -> Option<$type> { self.value().as_ref().map(|v| v.trim().parse().unwrap_or_else( |e: $error| self.error(&v, $name, e.to_string()))) } @@ -39,8 +39,8 @@ impl QueriedValue { } } - pub fn as_bool(self) -> bool { - if let Some(value) = self.value { + pub fn as_bool(&self) -> bool { + if let Some(value) = &self.value { if let Some(value) = value { match value.to_lowercase().as_str() { "y"|"yes"|"t"|"true" => true, @@ -55,10 +55,10 @@ impl QueriedValue { } } - pub fn as_path(&mut self) -> PathBuf { + pub fn as_path(&self) -> PathBuf { self.as_path_opt().unwrap_or_else(|| self.missing("path")) } - pub fn as_path_opt(&mut self) -> Option<PathBuf> { + pub fn as_path_opt(&self) -> Option<PathBuf> { if let Some(value) = self.value() { if !value.is_empty() { return Some(PathBuf::from(value)); @@ -67,17 +67,17 @@ impl QueriedValue { return None; } - pub fn as_str(&mut self) -> &str { + pub fn as_str(&self) -> &str { self.value().map(|v| v.as_str()).unwrap_or_else(|| self.missing("string")) } - pub fn as_str_opt(&mut self) -> Option<&str> { + pub fn as_str_opt(&self) -> Option<&str> { self.value().map(|v| v.as_str()) } - pub fn as_string(&mut self) -> String { + pub fn as_string(&self) -> String { self.as_str().to_string() } - pub fn as_string_opt(&mut self) -> Option<String> { + pub fn as_string_opt(&self) -> Option<String> { self.as_str_opt().map(|s| s.to_string()) } |