Skip to main content

camel_cli/template/
processor.rs

1use super::{TemplateFile, cargo_toml, gitignore, plugin_toml, readme_md};
2
3pub fn processor_files(plugin_name: &str) -> Vec<TemplateFile> {
4    vec![
5        TemplateFile {
6            path: "Cargo.toml".to_string(),
7            content: cargo_toml(plugin_name),
8        },
9        TemplateFile {
10            path: "src/lib.rs".to_string(),
11            content: lib_rs().to_string(),
12        },
13        TemplateFile {
14            path: "Camel.plugin.toml".to_string(),
15            content: plugin_toml(plugin_name, "processor"),
16        },
17        TemplateFile {
18            path: "README.md".to_string(),
19            content: readme_md(plugin_name, "processor"),
20        },
21        TemplateFile {
22            path: ".gitignore".to_string(),
23            content: gitignore().to_string(),
24        },
25        TemplateFile {
26            path: "wit/camel-plugin.wit".to_string(),
27            content: camel_wit::PLUGIN_WIT.to_string(),
28        },
29    ]
30}
31
32fn lib_rs() -> &'static str {
33    "use bindings::camel::plugin::types::{WasmBody, WasmError, WasmExchange};\nuse bindings::Guest;\n\n#[allow(clippy::too_many_arguments)]\nmod bindings {\n    wit_bindgen::generate!({\n        world: \"plugin\",\n        path: \"../wit\",\n    });\n}\n\nstruct Processor;\n\nimpl Guest for Processor {\n    fn init() -> Result<(), String> {\n        Ok(())\n    }\n\n    fn process(mut exchange: WasmExchange) -> Result<WasmExchange, WasmError> {\n        exchange.input.body = match &exchange.input.body {\n            WasmBody::Text(s) => WasmBody::Text(format!(\"processed: {s}\")),\n            other => other.clone(),\n        };\n        Ok(exchange)\n    }\n}\n\nbindings::export!(Processor with_types_in bindings);\n"
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn processor_files_contains_expected_paths() {
42        let files = processor_files("acme-processor");
43        let paths: Vec<&str> = files.iter().map(|f| f.path.as_str()).collect();
44
45        assert_eq!(files.len(), 6);
46        assert!(paths.contains(&"Cargo.toml"));
47        assert!(paths.contains(&"src/lib.rs"));
48        assert!(paths.contains(&"Camel.plugin.toml"));
49        assert!(paths.contains(&"README.md"));
50        assert!(paths.contains(&".gitignore"));
51        assert!(paths.contains(&"wit/camel-plugin.wit"));
52    }
53
54    #[test]
55    fn cargo_template_contains_cdylib() {
56        let cargo = cargo_toml("acme-processor");
57
58        assert!(cargo.contains("name = \"acme-processor\""));
59        assert!(cargo.contains("crate-type = [\"cdylib\"]"));
60        assert!(cargo.contains("wit-bindgen = \"0.57\""));
61        assert!(cargo.contains("[workspace]"));
62        assert!(!cargo.contains("camel-wasm-sdk"));
63    }
64
65    #[test]
66    fn lib_template_generates_bindings() {
67        let lib = lib_rs();
68
69        assert!(lib.contains("use bindings::Guest;"));
70        assert!(lib.contains("bindings::export!(Processor with_types_in bindings);"));
71        assert!(lib.contains("impl Guest for Processor"));
72        assert!(lib.contains("fn process("));
73        assert!(lib.contains("fn init()"));
74        assert!(lib.contains("wit_bindgen::generate!"));
75        assert!(lib.contains("mod bindings"));
76    }
77
78    #[test]
79    fn plugin_toml_contains_expected_keys() {
80        let plugin = plugin_toml("acme-processor", "processor");
81
82        assert!(plugin.contains("type = \"processor\""));
83        assert!(plugin.contains("entry = \"acme-processor.wasm\""));
84    }
85
86    #[test]
87    fn gitignore_contains_target_and_plugins() {
88        let files = processor_files("acme-processor");
89        let gitignore = files
90            .iter()
91            .find(|f| f.path == ".gitignore")
92            .expect("gitignore");
93        assert!(gitignore.content.contains("target"));
94        assert!(gitignore.content.contains("plugins/"));
95    }
96}