diff options
author | Ben Bridle <ben@derelict.engineering> | 2025-03-04 11:13:14 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2025-03-04 11:13:14 +1300 |
commit | 0724fda47e7146ff682eef101b5260331cb11d26 (patch) | |
tree | c6d89876e6058eb5b1bc4541634d23418573514c | |
parent | 1a307ff335c9723b287c42ec73580a30af2a0152 (diff) | |
download | switchboard-0724fda47e7146ff682eef101b5260331cb11d26.zip |
Add as_str and as_str_opt methods for values
These are borrowing versions of the as_string and as_string_opt methods.
-rw-r--r-- | src/value.rs | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/src/value.rs b/src/value.rs index 99468fa..7725490 100644 --- a/src/value.rs +++ b/src/value.rs @@ -67,11 +67,18 @@ impl QueriedValue { return None; } + pub fn as_str(&mut self) -> &str { + self.value().map(|v| v.as_str()).unwrap_or_else(|| self.missing("string")) + } + pub fn as_str_opt(&mut self) -> Option<&str> { + self.value().map(|v| v.as_str()) + } + pub fn as_string(&mut self) -> String { - self.as_string_opt().unwrap_or_else(|| self.missing("string")) + self.as_str().to_string() } pub fn as_string_opt(&mut self) -> Option<String> { - self.value().cloned() + self.as_str_opt().map(|s| s.to_string()) } as_number!{ f32, "f32" , std::num::ParseFloatError } |