perspective_client/config/
aggregates.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use std::fmt::Display;
14
15use serde::{Deserialize, Serialize};
16use ts_rs::TS;
17
18use crate::proto::view_config;
19
20#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize, TS)]
21#[serde(untagged)]
22pub enum Aggregate {
23    SingleAggregate(String),
24    MultiAggregate(String, Vec<String>),
25}
26
27impl From<&'static str> for Aggregate {
28    fn from(value: &'static str) -> Self {
29        if value.contains(" by ") {
30            let mut parts = value.split(" by ");
31            Aggregate::MultiAggregate(
32                parts.next().unwrap().into(),
33                parts.map(|x| x.into()).collect(),
34            )
35        } else {
36            Aggregate::SingleAggregate(value.into())
37        }
38    }
39}
40
41impl Display for Aggregate {
42    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
43        match self {
44            Self::SingleAggregate(x) => write!(fmt, "{x}")?,
45            Self::MultiAggregate(agg, x) => write!(fmt, "{} by {}", agg, x.join(", "))?,
46        };
47        Ok(())
48    }
49}
50
51impl From<Aggregate> for view_config::AggList {
52    fn from(value: Aggregate) -> Self {
53        view_config::AggList {
54            aggregations: match value {
55                Aggregate::SingleAggregate(x) => vec![format!("{}", x)],
56                Aggregate::MultiAggregate(x, y) => {
57                    vec![format!("{}", x), format!("{}", y.join(","))]
58                },
59            },
60        }
61    }
62}
63
64impl From<view_config::AggList> for Aggregate {
65    fn from(value: view_config::AggList) -> Self {
66        if value.aggregations.len() == 1 {
67            Aggregate::SingleAggregate(value.aggregations.first().unwrap().clone())
68        } else {
69            Aggregate::MultiAggregate(
70                value.aggregations.first().unwrap().clone(),
71                value
72                    .aggregations
73                    .get(1)
74                    .unwrap()
75                    .split(",")
76                    .map(|x| x.to_owned())
77                    .collect(),
78            )
79        }
80    }
81}