Skip to content

Commit dd8ad41

Browse files
committed
Templates: add a Rust template for non-trigger components
This new template makes it easy to create a component that can be used as a dependency in another component. Until now, that required fully manually setting up a component and adding it to the Spin manifest, etc. Signed-off-by: Till Schneidereit <[email protected]>
1 parent 2b4a982 commit dd8ad41

File tree

7 files changed

+72
-0
lines changed

7 files changed

+72
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target/
2+
.spin/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "{{project-name | kebab_case}}"
3+
authors = ["{{authors}}"]
4+
description = "{{project-description}}"
5+
version = "0.1.0"
6+
rust-version = "1.78"
7+
edition = "2021"
8+
9+
[lib]
10+
crate-type = ["cdylib"]
11+
12+
[dependencies]
13+
anyhow = "1"
14+
wit-bindgen = "0.36.0"
15+
16+
[workspace]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package component:{{project-name | kebab_case}};
2+
3+
interface {{project-name | kebab_case}} {
4+
hello: func() -> string;
5+
}
6+
7+
world component {
8+
export {{project-name | kebab_case}};
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
spin_manifest_version = 2
2+
3+
[application]
4+
name = "{{project-name | kebab_case}}"
5+
version = "0.1.0"
6+
authors = ["{{authors}}"]
7+
description = "{{project-description}}"
8+
9+
[component.{{project-name | kebab_case}}]
10+
source = "target/wasm32-wasip2/release/{{project-name | snake_case}}.wasm"
11+
[component.{{project-name | kebab_case}}.build]
12+
command = "cargo build --target wasm32-wasip2 --release"
13+
watch = ["src/**/*.rs", "Cargo.toml"]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Guest for Exports {
2+
fn hello() -> String {
3+
"Hello from {{project-name | snake_case}}".to_string()
4+
}
5+
}
6+
7+
// Boilerplate below here
8+
use crate::exports::component::{{project-name | snake_case}}::{{project-name | snake_case}}::Guest;
9+
wit_bindgen::generate!({
10+
world: "component",
11+
path: "component.wit",
12+
});
13+
struct Exports;
14+
export!(Exports);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[component.{{project-name | kebab_case}}]
2+
source = "{{ output-path }}/target/wasm32-wasip2/release/{{project-name | snake_case}}.wasm"
3+
[component.{{project-name | kebab_case}}.build]
4+
command = "cargo build --target wasm32-wasip2 --release"
5+
workdir = "{{ output-path }}"
6+
watch = ["src/**/*.rs", "Cargo.toml"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
manifest_version = "1"
2+
id = "component-rust"
3+
description = "Pure Rust component to be used as a dependency for other component"
4+
tags = ["rust"]
5+
6+
[add_component]
7+
skip_files = ["spin.toml"]
8+
[add_component.snippets]
9+
component = "component.txt"
10+
11+
[parameters]
12+
project-description = { type = "string", prompt = "Description", default = "" }

0 commit comments

Comments
 (0)