From 2c94211270c77005f296d2d8691ec13340ab5555 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 11 Mar 2025 16:53:51 +1300 Subject: 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. --- src/value.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/value.rs') 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 { + pub fn as_path_opt(&self) -> Option { 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 { + pub fn as_string_opt(&self) -> Option { self.as_str_opt().map(|s| s.to_string()) } -- cgit v1.2.3-70-g09d2