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
use crate::rules::{
    path_value::{Path, PathAwareValue},
    QueryResult,
};

pub(crate) fn count(args: &[QueryResult]) -> PathAwareValue {
    let count = args
        .iter()
        .filter(|query| !matches!(query, QueryResult::UnResolved(_)))
        .count();

    match args.is_empty() {
        true => PathAwareValue::Int((Path::root(), 0)),
        false => {
            let path = match &args[0] {
                QueryResult::Literal(val) | QueryResult::Resolved(val) => val.self_path().clone(),
                QueryResult::UnResolved(val) => val.traversed_to.self_path().clone(),
            };

            PathAwareValue::Int((path, count as i64))
        }
    }
}

#[cfg(test)]
#[path = "collections_tests.rs"]
mod collections_tests;