diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-03-18 18:19:13 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-03-18 18:27:22 +0100 |
| commit | a16726ae6652a795ff24f368ca25f93bae673366 (patch) | |
| tree | cb9de3573b2d094ddc52cff9f24bd4ba6e3c5474 /src | |
| parent | 533d4d57c6b65469e39d7b5d2289df1adb9cfb64 (diff) | |
Architecture description
Diffstat (limited to 'src')
| -rw-r--r-- | src/eval/array.rs | 23 | ||||
| -rw-r--r-- | src/model/styles.rs | 16 |
2 files changed, 11 insertions, 28 deletions
diff --git a/src/eval/array.rs b/src/eval/array.rs index e42fd28d..fa71ff1a 100644 --- a/src/eval/array.rs +++ b/src/eval/array.rs @@ -5,7 +5,7 @@ use std::ops::{Add, AddAssign}; use ecow::{eco_format, EcoString, EcoVec}; use super::{ops, Args, Func, Value, Vm}; -use crate::diag::{bail, At, SourceResult, StrResult}; +use crate::diag::{At, SourceResult, StrResult}; use crate::util::pretty_array_like; /// Create a new [`Array`] from values. @@ -139,9 +139,6 @@ impl Array { /// Return the first matching element. pub fn find(&self, vm: &mut Vm, func: Func) -> SourceResult<Option<Value>> { - if func.argc().map_or(false, |count| count != 1) { - bail!(func.span(), "function must have exactly one parameter"); - } for item in self.iter() { let args = Args::new(func.span(), [item.clone()]); if func.call_vm(vm, args)?.cast::<bool>().at(func.span())? { @@ -153,9 +150,6 @@ impl Array { /// Return the index of the first matching element. pub fn position(&self, vm: &mut Vm, func: Func) -> SourceResult<Option<i64>> { - if func.argc().map_or(false, |count| count != 1) { - bail!(func.span(), "function must have exactly one parameter"); - } for (i, item) in self.iter().enumerate() { let args = Args::new(func.span(), [item.clone()]); if func.call_vm(vm, args)?.cast::<bool>().at(func.span())? { @@ -169,9 +163,6 @@ impl Array { /// Return a new array with only those elements for which the function /// returns true. pub fn filter(&self, vm: &mut Vm, func: Func) -> SourceResult<Self> { - if func.argc().map_or(false, |count| count != 1) { - bail!(func.span(), "function must have exactly one parameter"); - } let mut kept = EcoVec::new(); for item in self.iter() { let args = Args::new(func.span(), [item.clone()]); @@ -184,9 +175,6 @@ impl Array { /// Transform each item in the array with a function. pub fn map(&self, vm: &mut Vm, func: Func) -> SourceResult<Self> { - if func.argc().map_or(false, |count| !(1..=2).contains(&count)) { - bail!(func.span(), "function must have one or two parameters"); - } let enumerate = func.argc() == Some(2); self.iter() .enumerate() @@ -203,9 +191,6 @@ impl Array { /// Fold all of the array's elements into one with a function. pub fn fold(&self, vm: &mut Vm, init: Value, func: Func) -> SourceResult<Value> { - if func.argc().map_or(false, |count| count != 2) { - bail!(func.span(), "function must have exactly two parameters"); - } let mut acc = init; for item in self.iter() { let args = Args::new(func.span(), [acc, item.clone()]); @@ -216,9 +201,6 @@ impl Array { /// Whether any element matches. pub fn any(&self, vm: &mut Vm, func: Func) -> SourceResult<bool> { - if func.argc().map_or(false, |count| count != 1) { - bail!(func.span(), "function must have exactly one parameter"); - } for item in self.iter() { let args = Args::new(func.span(), [item.clone()]); if func.call_vm(vm, args)?.cast::<bool>().at(func.span())? { @@ -231,9 +213,6 @@ impl Array { /// Whether all elements match. pub fn all(&self, vm: &mut Vm, func: Func) -> SourceResult<bool> { - if func.argc().map_or(false, |count| count != 1) { - bail!(func.span(), "function must have exactly one parameter"); - } for item in self.iter() { let args = Args::new(func.span(), [item.clone()]); if !func.call_vm(vm, args)?.cast::<bool>().at(func.span())? { diff --git a/src/model/styles.rs b/src/model/styles.rs index 8cccb5f6..7b725af9 100644 --- a/src/model/styles.rs +++ b/src/model/styles.rs @@ -343,12 +343,7 @@ impl Debug for Transform { cast_from_value! { Transform, content: Content => Self::Content(content), - func: Func => { - if func.argc().map_or(false, |count| count != 1) { - Err("function must have exactly one parameter")? - } - Self::Func(func) - }, + func: Func => Self::Func(func), } /// A chain of style maps, similar to a linked list. @@ -494,6 +489,15 @@ impl<'a> StyleChain<'a> { }) } + /// Convert to a style map. + pub fn to_map(self) -> StyleMap { + let mut suffix = StyleMap::new(); + for link in self.links() { + suffix.0.splice(0..0, link.iter().cloned()); + } + suffix + } + /// Iterate over the entries of the chain. fn entries(self) -> Entries<'a> { Entries { inner: [].as_slice().iter(), links: self.links() } |
