dysql_tpl/simple/
simple_template.rs

1use serde::{Serialize, Deserialize};
2
3use crate::{Content, simple::simple_section::SimpleSection};
4
5use super::{SimpleValue, simple_block::{SimpleBlock, SimpleTag}, SimpleError};
6
7/// 用于在 SQL 中绑定 DTO 值的简化模版
8#[derive(Debug)]
9#[derive(Serialize, Deserialize)]
10pub struct SimpleTemplate {
11    blocks: Vec<SimpleBlock>,
12}
13
14impl SimpleTemplate
15{
16    /// 生成简化模版
17    pub fn new(source: &str) -> Self
18    {
19        let param_names: Vec<&str> = source.split(".").collect();
20        let param_len = param_names.len();
21        let blocks = param_names.iter().enumerate().map(|(i, name)| {
22            let children = param_len - i - 1;
23            let tag = if children > 0 {
24                SimpleTag::Section
25            } else {
26                SimpleTag::Unescaped
27            };
28            
29            SimpleBlock::new(name, tag, children as u32)
30        }).collect();
31
32        Self {
33            blocks,
34        }
35    }
36
37    /// 获取对应参数值
38    pub fn apply<C>(&self, content: &C) -> Result<SimpleValue, SimpleError>
39    where 
40        C: Content,
41    {
42        let section = SimpleSection::new(&self.blocks)
43            .with(content);
44        section.apply()
45    }
46}