Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templates: add a Rust template for non-trigger components #2970

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions templates/component-rust/content/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
.spin/
16 changes: 16 additions & 0 deletions templates/component-rust/content/Cargo.toml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "{{project-name | kebab_case}}"
authors = ["{{authors}}"]
description = "{{project-description}}"
version = "0.1.0"
rust-version = "1.78"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the status of the wasm32-wasip2 target in 1.78?

edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
anyhow = "1"
wit-bindgen = "0.36.0"

[workspace]
9 changes: 9 additions & 0 deletions templates/component-rust/content/component.wit
Original file line number Diff line number Diff line change
@@ -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}};
}
13 changes: 13 additions & 0 deletions templates/component-rust/content/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
spin_manifest_version = 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels extremely awkward to create a spin.toml for a library component. Why does a dependency need a Spin manifest file? It can't be referenced by a trigger. It can't be referenced by its component ID (although we could allow that of course). Most of its settings are misleading e.g. setting allowed outbound hosts looks like it grants access but actually does nothing. Is this just to opt it into getting included spin build?

I feel like the ask here is for the manifest to provide for building and referencing library components but I am not sold that mixing them in with "application" components is the way to go right now: perhaps we should step back and ask how we should represent library components in the manifest, then create a template as part of that?


[application]
name = "{{project-name | kebab_case}}"
version = "0.1.0"
authors = ["{{authors}}"]
description = "{{project-description}}"

[component.{{project-name | kebab_case}}]
source = "target/wasm32-wasip2/release/{{project-name | snake_case}}.wasm"
[component.{{project-name | kebab_case}}.build]
command = "cargo build --target wasm32-wasip2 --release"
watch = ["src/**/*.rs", "Cargo.toml"]
14 changes: 14 additions & 0 deletions templates/component-rust/content/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 6 additions & 0 deletions templates/component-rust/metadata/snippets/component.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[component.{{project-name | kebab_case}}]
source = "{{ output-path }}/target/wasm32-wasip2/release/{{project-name | snake_case}}.wasm"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use wasip1 everywhere else - is p2 stable enough that we should be moving to it?

[component.{{project-name | kebab_case}}.build]
command = "cargo build --target wasm32-wasip2 --release"
workdir = "{{ output-path }}"
watch = ["src/**/*.rs", "Cargo.toml"]
12 changes: 12 additions & 0 deletions templates/component-rust/metadata/spin-template.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
manifest_version = "1"
id = "component-rust"
description = "Pure Rust component to be used as a dependency for other component"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove "pure" e.g.

Suggested change
description = "Pure Rust component to be used as a dependency for other component"
description = "Library (dependency) component using Rust"

or something like that?

tags = ["rust"]

[add_component]
skip_files = ["spin.toml"]
[add_component.snippets]
component = "component.txt"

[parameters]
project-description = { type = "string", prompt = "Description", default = "" }
Loading