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
use cel_interpreter::{CelContext, CelMap, CelValue};
use std::collections::HashMap;

use super::param_definition::{ParamDataType, ParamDefinition};
use crate::error::SqlxLedgerError;

#[derive(Debug)]
pub struct TxParams {
    values: HashMap<String, CelValue>,
}

impl TxParams {
    pub fn new() -> Self {
        Self {
            values: HashMap::new(),
        }
    }

    pub fn insert(&mut self, k: impl Into<String>, v: impl Into<CelValue>) {
        self.values.insert(k.into(), v.into());
    }

    pub fn to_context(
        mut self,
        defs: Option<&Vec<ParamDefinition>>,
    ) -> Result<CelContext, SqlxLedgerError> {
        let mut ctx = CelContext::new();
        if let Some(defs) = defs {
            let mut cel_map = CelMap::new();
            for d in defs {
                if let Some(v) = self.values.remove(&d.name) {
                    match ParamDataType::try_from(&v) {
                        Ok(t) if t == d.r#type => {
                            cel_map.insert(d.name.clone(), v);
                            continue;
                        }
                        _ => return Err(SqlxLedgerError::TxParamTypeMismatch(d.r#type.clone())),
                    }
                }
                if let Some(expr) = d.default_expr() {
                    cel_map.insert(d.name.clone(), expr.evaluate(&ctx)?);
                }
            }
            ctx.add_variable("params", cel_map);
        }

        if !self.values.is_empty() {
            return Err(SqlxLedgerError::TooManyParameters);
        }

        Ok(ctx)
    }
}

impl Default for TxParams {
    fn default() -> Self {
        Self::new()
    }
}