archetect_core/actions/
load.rs

1use crate::actions::exec::ExecAction;
2use linked_hash_map::LinkedHashMap;
3
4#[derive(Debug, Serialize, Deserialize)]
5pub struct LoadAction {
6    into: String,
7    #[serde(flatten)]
8    options: LoadOptions,
9    #[serde(skip_serializing_if = "Option::is_none")]
10    render: Option<bool>,
11}
12
13#[derive(Debug, Serialize, Deserialize)]
14pub enum LoadOptions {
15    #[serde(rename = "file")]
16    File(String),
17    #[serde(rename = "http")]
18    Http {
19        url: String,
20        #[serde(skip_serializing_if = "Option::is_none")]
21        headers: Option<LinkedHashMap<String, String>>,
22    },
23    #[serde(rename = "exec")]
24    Exec(ExecAction),
25    #[serde(rename = "inline")]
26    Inline(String),
27}
28
29#[cfg(test)]
30mod tests {
31    use indoc::indoc;
32    use serde_json;
33    use serde_yaml;
34
35    use crate::actions::exec::ExecAction;
36    use crate::actions::load::{LoadAction, LoadOptions};
37
38    #[test]
39    fn test_serialize_from_file() {
40        let action = LoadAction {
41            into: "schema".to_string(),
42            options: LoadOptions::File("{{ archetype.local_path }}/schema.json".to_owned()),
43            render: Some(false),
44        };
45
46        let yaml = serde_yaml::to_string(&action).unwrap();
47        println!("{}", yaml);
48    }
49
50    #[test]
51    fn test_serialize_from_http() {
52        let action = LoadAction {
53            into: "schema".to_string(),
54            options: LoadOptions::Http {
55                url: "http://www.example.com/schema".to_owned(),
56                headers: None,
57            },
58            render: None,
59        };
60
61        let yaml = serde_yaml::to_string(&action).unwrap();
62        println!("{}", yaml);
63    }
64
65    #[test]
66    fn test_serialize_inline() {
67        let action = LoadAction {
68            into: "schema".to_string(),
69            options: LoadOptions::Inline(
70                indoc!(
71                    r#"
72                {
73                  "into": "schema",
74                  "exec": {
75                    "command": "python",
76                    "args": [
77                      "read_schema.py"
78                    ]
79                  },
80                  "render": true
81                }
82            "#
83                )
84                .to_string(),
85            ),
86            render: None,
87        };
88
89        let yaml = serde_yaml::to_string(&action).unwrap();
90        println!("{}", yaml);
91    }
92
93    #[test]
94    fn test_deserialize_inline() {
95        let yaml = indoc!(
96            r#"
97            ---
98            into: schema
99            inline: |
100                {
101                  "into": "schema",
102                  "exec": {
103                    "command": "python",
104                    "args": [
105                      "read_schema.py"
106                    ]
107                  },
108                  "render": true
109                }   
110        "#
111        );
112
113        let action: LoadAction = serde_yaml::from_str(&yaml).unwrap();
114        if let LoadOptions::Inline(json) = action.options {
115            println!("{}", json);
116        }
117    }
118
119    #[test]
120    fn test_serialize_from_exec() {
121        let action = LoadAction {
122            into: "schema".to_string(),
123            options: LoadOptions::Exec(ExecAction::new("python").with_arg("read_schema.py")),
124            render: Some(true),
125        };
126
127        let yaml = serde_yaml::to_string(&action).unwrap();
128
129        println!("{}", yaml);
130
131        let json = serde_json::to_string_pretty(&action).unwrap();
132        println!("{}", json);
133    }
134}