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/// Sentinel for a full-table search request. CSV has no query engine, so the
15/// evaluator rejects this marker with an `Unsupported` error rather than
16/// filtering — search of loaded data is the Lens/Diorama layer's job.
17pub const OP_SEARCH: &str = "SEARCH";
18
19/// CSV-specific comparison operations.
20///
21/// Blanket-implemented for all `Expressive<AnyCsvType>` so columns, fields,
22/// and expressions all get `eq`, `in_`, etc. for free.
23pub trait CsvOperation: Expressive<AnyCsvType> {
24 /// `field = value`
25 fn eq(&self, value: impl Expressive<AnyCsvType>) -> Expression<AnyCsvType>
26 where
27 Self: Sized,
28 {
29 Expression::new(
30 OP_EQ,
31 vec![
32 ExpressiveEnum::Nested(self.expr()),
33 ExpressiveEnum::Nested(value.expr()),
34 ],
35 )
36 }
37
38 /// `field IN (values_expression)`
39 fn in_(&self, values: impl Expressive<AnyCsvType>) -> Expression<AnyCsvType>
40 where
41 Self: Sized,
42 {
43 Expression::new(
44 OP_IN,
45 vec![
46 ExpressiveEnum::Nested(self.expr()),
47 ExpressiveEnum::Nested(values.expr()),
48 ],
49 )
50 }
51}
52
53impl<S: Expressive<AnyCsvType>> CsvOperation for S {}