diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-12-30 15:13:28 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-12-30 16:45:41 +0100 |
| commit | f70cea508cd30fa40770ea989fe2a19e715a357b (patch) | |
| tree | 731bb96b375dc8fd0f7e5a2a7e1d1fe5cb2a600e /src/model/dict.rs | |
| parent | fe1f4400693690b68db5a7ec0976ba998624a740 (diff) | |
Remove index syntax in favor of accessor methods
Diffstat (limited to 'src/model/dict.rs')
| -rw-r--r-- | src/model/dict.rs | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/src/model/dict.rs b/src/model/dict.rs index e3c5454e..83c16824 100644 --- a/src/model/dict.rs +++ b/src/model/dict.rs @@ -4,7 +4,7 @@ use std::ops::{Add, AddAssign}; use std::sync::Arc; use super::{Args, Array, Func, Str, Value, Vm}; -use crate::diag::{SourceResult, StrResult}; +use crate::diag::{bail, SourceResult, StrResult}; use crate::syntax::is_ident; use crate::syntax::Spanned; use crate::util::{format_eco, ArcExt, EcoString}; @@ -50,7 +50,7 @@ impl Dict { } /// Borrow the value the given `key` maps to. - pub fn get(&self, key: &str) -> StrResult<&Value> { + pub fn at(&self, key: &str) -> StrResult<&Value> { self.0.get(key).ok_or_else(|| missing_key(key)) } @@ -58,7 +58,7 @@ impl Dict { /// /// This inserts the key with [`None`](Value::None) as the value if not /// present so far. - pub fn get_mut(&mut self, key: Str) -> &mut Value { + pub fn at_mut(&mut self, key: Str) -> &mut Value { Arc::make_mut(&mut self.0).entry(key).or_default() } @@ -108,6 +108,9 @@ impl Dict { /// Transform each pair in the dictionary with a function. pub fn map(&self, vm: &Vm, f: Spanned<Func>) -> SourceResult<Array> { + if f.v.argc().map_or(false, |count| count != 1) { + bail!(f.span, "function must have exactly two parameters"); + } self.iter() .map(|(key, value)| { let args = Args::new(f.span, [Value::Str(key.clone()), value.clone()]); |
