diff --git a/templates/component-rust/content/.gitignore b/templates/component-rust/content/.gitignore new file mode 100644 index 000000000..386474fa5 --- /dev/null +++ b/templates/component-rust/content/.gitignore @@ -0,0 +1,2 @@ +target/ +.spin/ diff --git a/templates/component-rust/content/Cargo.toml.tmpl b/templates/component-rust/content/Cargo.toml.tmpl new file mode 100644 index 000000000..82b2092bb --- /dev/null +++ b/templates/component-rust/content/Cargo.toml.tmpl @@ -0,0 +1,16 @@ +[package] +name = "{{project-name | kebab_case}}" +authors = ["{{authors}}"] +description = "{{component-description}}" +version = "0.1.0" +rust-version = "1.82" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +anyhow = "1" +wit-bindgen = "0.37.0" + +[workspace] diff --git a/templates/component-rust/content/component.wit b/templates/component-rust/content/component.wit new file mode 100644 index 000000000..8f2a310d5 --- /dev/null +++ b/templates/component-rust/content/component.wit @@ -0,0 +1,9 @@ +package component:{{project-name | kebab_case}}; + +interface {{project-name | kebab_case}} { + hello: func() -> string; +} + +world component { + export {{project-name | kebab_case}}; +} diff --git a/templates/component-rust/content/src/lib.rs b/templates/component-rust/content/src/lib.rs new file mode 100644 index 000000000..2ed1d1855 --- /dev/null +++ b/templates/component-rust/content/src/lib.rs @@ -0,0 +1,14 @@ +impl Guest for Exports { + fn hello() -> String { + "Hello from {{project-name | snake_case}}".to_string() + } +} + +// Boilerplate below here +use crate::exports::component::{{project-name | snake_case}}::{{project-name | snake_case}}::Guest; +wit_bindgen::generate!({ + world: "component", + path: "component.wit", +}); +struct Exports; +export!(Exports); diff --git a/templates/component-rust/metadata/snippets/component.txt b/templates/component-rust/metadata/snippets/component.txt new file mode 100644 index 000000000..de861a892 --- /dev/null +++ b/templates/component-rust/metadata/snippets/component.txt @@ -0,0 +1,6 @@ +[component.{{project-name | kebab_case}}] +source = "{{ output-path }}/target/wasm32-wasip2/release/{{project-name | snake_case}}.wasm" +[component.{{project-name | kebab_case}}.build] +command = "cargo build --target wasm32-wasip2 --release" +workdir = "{{ output-path }}" +watch = ["src/**/*.rs", "Cargo.toml"] diff --git a/templates/component-rust/metadata/spin-template.toml b/templates/component-rust/metadata/spin-template.toml new file mode 100644 index 000000000..e6cb8c2de --- /dev/null +++ b/templates/component-rust/metadata/spin-template.toml @@ -0,0 +1,13 @@ +manifest_version = "1" +id = "component-rust" +description = "Library (dependency) component using Rust" +tags = ["rust"] + +[new_application] +supported = false + +[add_component.snippets] +component = "component.txt" + +[parameters] +component-description = { type = "string", prompt = "Description", default = "" }