1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use crate::compiler::prelude::*;
use std::collections::{BTreeMap, HashMap};

fn tally(value: Value) -> Resolved {
    let value = value.try_array()?;
    #[allow(clippy::mutable_key_type)] // false positive due to bytes::Bytes
    let mut map: HashMap<Bytes, usize> = HashMap::new();
    for value in value {
        if let Value::Bytes(value) = value {
            *map.entry(value).or_insert(0) += 1;
        } else {
            return Err(format!("all values must be strings, found: {value:?}").into());
        }
    }
    let map: BTreeMap<_, _> = map
        .into_iter()
        .map(|(k, v)| (String::from_utf8_lossy(&k).into_owned(), Value::from(v)))
        .collect();
    Ok(map.into())
}

#[derive(Clone, Copy, Debug)]
pub struct Tally;

impl Function for Tally {
    fn identifier(&self) -> &'static str {
        "tally"
    }

    fn examples(&self) -> &'static [Example] {
        &[Example {
            title: "tally",
            source: r#"tally!(["foo", "bar", "foo", "baz"])"#,
            result: Ok(r#"{"foo": 2, "bar": 1, "baz": 1}"#),
        }]
    }

    fn compile(
        &self,
        _state: &state::TypeState,
        _ctx: &mut FunctionCompileContext,
        arguments: ArgumentList,
    ) -> Compiled {
        let value = arguments.required("value");

        Ok(TallyFn { value }.as_expr())
    }

    fn parameters(&self) -> &'static [Parameter] {
        &[Parameter {
            keyword: "value",
            kind: kind::ARRAY,
            required: true,
        }]
    }
}

#[derive(Debug, Clone)]
pub(crate) struct TallyFn {
    value: Box<dyn Expression>,
}

impl FunctionExpression for TallyFn {
    fn resolve(&self, ctx: &mut Context) -> Resolved {
        let value = self.value.resolve(ctx)?;
        tally(value)
    }

    fn type_def(&self, _: &state::TypeState) -> TypeDef {
        TypeDef::object(Collection::from_unknown(Kind::integer())).fallible()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::value;

    test_function![
        tally => Tally;

        default {
            args: func_args![
                value: value!(["bar", "foo", "baz", "foo"]),
            ],
            want: Ok(value!({"bar": 1, "foo": 2, "baz": 1})),
            tdef: TypeDef::object(Collection::from_unknown(Kind::integer())).fallible(),
        }

        non_string_values {
            args: func_args![
                value: value!(["foo", [1,2,3], "123abc", 1, true, [1,2,3], "foo", true, 1]),
            ],
            want: Err("all values must be strings, found: Array([Integer(1), Integer(2), Integer(3)])"),
            tdef: TypeDef::object(Collection::from_unknown(Kind::integer())).fallible(),
        }
    ];
}