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
pub mod field_type;
pub mod properties;

use crate::mapping::field_type::keyword::KeywordFieldType;
use crate::mapping::field_type::text::TextFieldType;
use crate::mapping::properties::MappingProperties;
use crate::util::UtilMap;
use serde_json::Value;

pub trait MappingTrait {
    fn build(&self) -> Value;
    fn query_name(&self) -> String;
}

pub struct MappingBuilder {
    properties: MappingProperties,
}

impl MappingBuilder {
    pub fn new() -> MappingBuilder {
        MappingBuilder {
            properties: MappingProperties::new(),
        }
    }
    pub fn add_property<T>(&mut self, key: &str, value: T) -> &mut MappingBuilder
    where
        T: MappingTrait + 'static,
    {
        self.properties.add_property(key, value);
        self
    }
    pub fn set_properties(&mut self, properties: MappingProperties) {
        self.properties = properties;
    }
    pub fn build(self) -> Value {
        let mut map = UtilMap::new();
        map.append_value("mappings", self.properties.build());
        map.build()
    }
}

#[test]
fn test() {
    let mut mapping = MappingBuilder::new();
    mapping
        .add_property("title", KeywordFieldType::new())
        .add_property("content", TextFieldType::new());
    println!("{}", mapping.build().to_string())
}