Skip to main content

camel_cli/template/
bean.rs

1use super::TemplateFile;
2
3pub fn bean_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(plugin_name),
12        },
13        TemplateFile {
14            path: "Camel.plugin.toml".to_string(),
15            content: plugin_toml(plugin_name),
16        },
17        TemplateFile {
18            path: "README.md".to_string(),
19            content: readme_md(plugin_name),
20        },
21        TemplateFile {
22            path: ".gitignore".to_string(),
23            content: gitignore().to_string(),
24        },
25        TemplateFile {
26            path: "wit/camel-bean.wit".to_string(),
27            content: camel_bean_wit().to_string(),
28        },
29        TemplateFile {
30            path: "wit/camel-plugin.wit".to_string(),
31            content: camel_plugin_wit().to_string(),
32        },
33    ]
34}
35
36fn cargo_toml(plugin_name: &str) -> String {
37    format!(
38        "[package]\nname = \"{plugin_name}\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[workspace]\n\n[lib]\ncrate-type = [\"cdylib\"]\n\n[dependencies]\nwit-bindgen = \"0.57\"\n"
39    )
40}
41
42fn lib_rs(plugin_name: &str) -> String {
43    let plugin_type = to_pascal_case(plugin_name);
44    format!(
45        "use bindings::camel::plugin::types::{{WasmBody, WasmError, WasmExchange}};\nuse bindings::Guest;\n\nmod bindings {{\n    wit_bindgen::generate!({{\n        world: \"bean\",\n        path: \"../wit\",\n    }});\n}}\n\nstruct {plugin_type};\n\nimpl Guest for {plugin_type} {{\n    fn init() -> Result<(), String> {{\n        Ok(())\n    }}\n\n    fn methods() -> Vec<String> {{\n        vec![\"hello\".into()]\n    }}\n\n    fn invoke(method: String, mut exchange: WasmExchange) -> Result<WasmExchange, WasmError> {{\n        match method.as_str() {{\n            \"hello\" => {{\n                let text = match &exchange.input.body {{\n                    WasmBody::Text(s) => s.clone(),\n                    _ => String::new(),\n                }};\n                exchange.input.body = WasmBody::Text(format!(\"Hello from {plugin_name}: {{text}}\"));\n                Ok(exchange)\n            }}\n            _ => Err(WasmError::ProcessorError(format!(\"unknown method: {{method}}\"))),\n        }}\n    }}\n}}\n\nbindings::export!({plugin_type} with_types_in bindings);\n"
46    )
47}
48
49fn plugin_toml(plugin_name: &str) -> String {
50    format!(
51        "name = \"{plugin_name}\"\nversion = \"0.1.0\"\ntype = \"bean\"\nentry = \"{plugin_name}.wasm\"\n"
52    )
53}
54
55fn readme_md(plugin_name: &str) -> String {
56    format!(
57        "# {plugin_name}\n\nWASM bean plugin for Camel.\n\n## Build\n\n```bash\ncamel plugin build\n```\n\n## Files\n\n- `src/lib.rs`: plugin entrypoint implementing bean Guest methods\n- `wit/`: WIT definitions used for guest bindings generation\n- `Camel.plugin.toml`: plugin metadata for Camel\n"
58    )
59}
60
61fn gitignore() -> &'static str {
62    "target\n"
63}
64
65fn camel_bean_wit() -> &'static str {
66    camel_wit::BEAN_WIT
67}
68
69fn camel_plugin_wit() -> &'static str {
70    camel_wit::PLUGIN_WIT
71}
72
73fn to_pascal_case(name: &str) -> String {
74    name.split(|c: char| !c.is_ascii_alphanumeric())
75        .filter(|part| !part.is_empty())
76        .map(|part| {
77            let mut chars = part.chars();
78            let mut out = String::new();
79            if let Some(first) = chars.next() {
80                out.extend(first.to_uppercase());
81                out.extend(chars.flat_map(|c| c.to_lowercase()));
82            }
83            out
84        })
85        .collect()
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[test]
93    fn bean_files_contains_expected_paths() {
94        let files = bean_files("acme-bean");
95        let paths: Vec<&str> = files.iter().map(|f| f.path.as_str()).collect();
96
97        assert_eq!(files.len(), 7);
98        assert!(paths.contains(&"Cargo.toml"));
99        assert!(paths.contains(&"src/lib.rs"));
100        assert!(paths.contains(&"Camel.plugin.toml"));
101        assert!(paths.contains(&"README.md"));
102        assert!(paths.contains(&".gitignore"));
103        assert!(paths.contains(&"wit/camel-bean.wit"));
104        assert!(paths.contains(&"wit/camel-plugin.wit"));
105    }
106
107    #[test]
108    fn cargo_template_contains_workspace_and_wit_bindgen() {
109        let cargo = cargo_toml("acme-bean");
110
111        assert!(cargo.contains("name = \"acme-bean\""));
112        assert!(cargo.contains("crate-type = [\"cdylib\"]"));
113        assert!(cargo.contains("[workspace]"));
114        assert!(cargo.contains("wit-bindgen = \"0.57\""));
115        assert!(!cargo.contains("camel-wasm-sdk"));
116    }
117
118    #[test]
119    fn lib_template_uses_wit_bindgen_and_bean_world() {
120        let lib = lib_rs("acme-bean");
121
122        assert!(lib.contains("wit_bindgen::generate!"));
123        assert!(lib.contains("world: \"bean\""));
124        assert!(lib.contains("path: \"../wit\""));
125        assert!(lib.contains("impl Guest for AcmeBean"));
126        assert!(lib.contains("fn methods() -> Vec<String>"));
127        assert!(lib.contains("fn invoke("));
128        assert!(lib.contains("Hello from acme-bean: {text}"));
129        assert!(lib.contains("bindings::export!(AcmeBean with_types_in bindings);"));
130        assert!(!lib.contains("camel_wasm_sdk"));
131    }
132
133    #[test]
134    fn plugin_toml_contains_expected_keys() {
135        let plugin = plugin_toml("acme-bean");
136
137        assert!(plugin.contains("type = \"bean\""));
138        assert!(plugin.contains("entry = \"acme-bean.wasm\""));
139    }
140
141    #[test]
142    fn wit_templates_include_expected_worlds() {
143        let bean_wit = camel_bean_wit();
144        let plugin_wit = camel_plugin_wit();
145
146        assert!(bean_wit.contains("world bean"));
147        assert!(bean_wit.contains("import host;"));
148        assert!(plugin_wit.contains("interface types"));
149        assert!(plugin_wit.contains("interface host"));
150    }
151}