Skip to main content

vantage_csv/
operation.rs

1//! CSV operation trait and condition operation constants.
2//!
3//! Template markers for condition operations. CSV's in-memory evaluator
4//! matches on these to know which operation to apply.
5
6use vantage_expressions::traits::expressive::ExpressiveEnum;
7use vantage_expressions::{Expression, Expressive};
8
9use crate::type_system::AnyCsvType;
10
11pub const OP_EQ: &str = "{} = {}";
12pub const OP_IN: &str = "{} IN ({})";
13
14/// CSV-specific comparison operations.
15///
16/// Blanket-implemented for all `Expressive<AnyCsvType>` so columns, fields,
17/// and expressions all get `eq`, `in_`, etc. for free.
18pub trait CsvOperation: Expressive<AnyCsvType> {
19    /// `field = value`
20    fn eq(&self, value: impl Expressive<AnyCsvType>) -> Expression<AnyCsvType>
21    where
22        Self: Sized,
23    {
24        Expression::new(
25            OP_EQ,
26            vec![
27                ExpressiveEnum::Nested(self.expr()),
28                ExpressiveEnum::Nested(value.expr()),
29            ],
30        )
31    }
32
33    /// `field IN (values_expression)`
34    fn in_(&self, values: impl Expressive<AnyCsvType>) -> Expression<AnyCsvType>
35    where
36        Self: Sized,
37    {
38        Expression::new(
39            OP_IN,
40            vec![
41                ExpressiveEnum::Nested(self.expr()),
42                ExpressiveEnum::Nested(values.expr()),
43            ],
44        )
45    }
46}
47
48impl<S: Expressive<AnyCsvType>> CsvOperation for S {}