summaryrefslogtreecommitdiff
path: root/src/library/utility.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-08-14 15:24:59 +0200
committerLaurenz <laurmaedje@gmail.com>2021-08-14 15:55:39 +0200
commit6ae6d86b9c6fefe6c5379ac1b20ea90634c09c81 (patch)
tree2504c3b46807be148b9efbadf9b23e57bb77b8f3 /src/library/utility.rs
parentfcb4e451186067cdf6efe3c14cbfa7561b366a6c (diff)
Separate type for string values
Diffstat (limited to 'src/library/utility.rs')
-rw-r--r--src/library/utility.rs44
1 files changed, 19 insertions, 25 deletions
diff --git a/src/library/utility.rs b/src/library/utility.rs
index 20d10830..84f1d7ab 100644
--- a/src/library/utility.rs
+++ b/src/library/utility.rs
@@ -2,37 +2,34 @@ use std::cmp::Ordering;
use std::str::FromStr;
use crate::color::{Color, RgbaColor};
-use crate::pretty::pretty;
use super::*;
/// `type`: The name of a value's type.
-pub fn type_(_: &mut EvalContext, args: &mut FuncArgs) -> TypResult<Value> {
- let value = args.expect::<Value>("value")?;
- Ok(value.type_name().into())
+pub fn type_(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
+ Ok(args.expect::<Value>("value")?.type_name().into())
}
/// `repr`: The string representation of a value.
-pub fn repr(_: &mut EvalContext, args: &mut FuncArgs) -> TypResult<Value> {
- let value = args.expect::<Value>("value")?;
- Ok(pretty(&value).into())
+pub fn repr(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
+ Ok(args.expect::<Value>("value")?.to_string().into())
}
/// `len`: The length of a string, an array or a dictionary.
-pub fn len(_: &mut EvalContext, args: &mut FuncArgs) -> TypResult<Value> {
+pub fn len(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
let Spanned { v, span } = args.expect("collection")?;
- Ok(match v {
- Value::Str(v) => Value::Int(v.len() as i64),
- Value::Array(v) => Value::Int(v.len()),
- Value::Dict(v) => Value::Int(v.len()),
+ Ok(Value::Int(match v {
+ Value::Str(v) => v.len(),
+ Value::Array(v) => v.len(),
+ Value::Dict(v) => v.len(),
_ => bail!(span, "expected string, array or dictionary"),
- })
+ }))
}
/// `rgb`: Create an RGB(A) color.
-pub fn rgb(_: &mut EvalContext, args: &mut FuncArgs) -> TypResult<Value> {
+pub fn rgb(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
Ok(Value::Color(Color::Rgba(
- if let Some(string) = args.eat::<Spanned<EcoString>>() {
+ if let Some(string) = args.eat::<Spanned<Str>>() {
match RgbaColor::from_str(&string.v) {
Ok(color) => color,
Err(_) => bail!(string.span, "invalid color"),
@@ -49,35 +46,32 @@ pub fn rgb(_: &mut EvalContext, args: &mut FuncArgs) -> TypResult<Value> {
}
/// `min`: The minimum of a sequence of values.
-pub fn min(_: &mut EvalContext, args: &mut FuncArgs) -> TypResult<Value> {
+pub fn min(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
minmax(args, Ordering::Less)
}
/// `max`: The maximum of a sequence of values.
-pub fn max(_: &mut EvalContext, args: &mut FuncArgs) -> TypResult<Value> {
+pub fn max(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
minmax(args, Ordering::Greater)
}
/// Find the minimum or maximum of a sequence of values.
-fn minmax(args: &mut FuncArgs, goal: Ordering) -> TypResult<Value> {
- let span = args.span;
-
+fn minmax(args: &mut Arguments, goal: Ordering) -> TypResult<Value> {
let mut extremum = args.expect::<Value>("value")?;
- for value in args.all::<Value>() {
- match value.partial_cmp(&extremum) {
+ for Spanned { v, span } in args.all::<Spanned<Value>>() {
+ match v.partial_cmp(&extremum) {
Some(ordering) => {
if ordering == goal {
- extremum = value;
+ extremum = v;
}
}
None => bail!(
span,
"cannot compare {} with {}",
extremum.type_name(),
- value.type_name(),
+ v.type_name(),
),
}
}
-
Ok(extremum)
}