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

ref(sdk): use the macro the generate bindings #1348

Merged
merged 1 commit into from
Apr 3, 2023
Merged
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: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions examples/http-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions sdk/rust/macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ http = "0.2"
proc-macro2 = "1"
quote = "1.0"
syn = { version = "1.0", features = [ "full" ]}
wit-bindgen-gen-rust-wasm = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "cb871cfa1ee460b51eb1d144b175b9aab9c50aba" }
wit-bindgen-gen-core = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "cb871cfa1ee460b51eb1d144b175b9aab9c50aba" }
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "cb871cfa1ee460b51eb1d144b175b9aab9c50aba" }
20 changes: 4 additions & 16 deletions sdk/rust/macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
use proc_macro::TokenStream;
use quote::quote;
use wit_bindgen_gen_core::{wit_parser::Interface, Direction, Files, Generator};
use wit_bindgen_gen_rust_wasm::RustWasm;

/// The entrypoint to a Spin HTTP component written in Rust.
#[proc_macro_attribute]
pub fn http_component(_attr: TokenStream, item: TokenStream) -> TokenStream {
const HTTP_COMPONENT_WIT_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/wit/spin-http.wit");
const HTTP_COMPONENT_WIT: &str =
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/wit/spin-http.wit"));

let func = syn::parse_macro_input!(item as syn::ItemFn);
let func_name = &func.sig.ident;

let iface = Interface::parse_file(HTTP_COMPONENT_WIT_PATH)
.expect("cannot parse Spin HTTP interface file");

let mut files = Files::default();
RustWasm::new().generate_one(&iface, Direction::Export, &mut files);
let (_, contents) = files.iter().next().unwrap();
let iface_tokens: TokenStream = std::str::from_utf8(contents)
.expect("cannot parse UTF-8 from Spin HTTP interface file")
.parse()
.expect("cannot parse Spin HTTP interface file");
let iface = syn::parse_macro_input!(iface_tokens as syn::ItemMod);

quote!(
#iface
wit_bindgen_rust::export!({src["spin_http"]: #HTTP_COMPONENT_WIT});

struct SpinHttp;

impl spin_http::SpinHttp for SpinHttp {
// Implement the `handler` entrypoint for Spin HTTP components.
fn handle_http_request(req: spin_http::Request) -> spin_http::Response {
Expand Down
2 changes: 0 additions & 2 deletions tests/http/headers-env-routes-test/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions tests/http/simple-spin-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/http/vault-config-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ http = "0.2"
# The Spin SDK.
spin-sdk = { path = "../../../sdk/rust"}
# Crate that generates Rust Wasm bindings from a WebAssembly interface.
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "e9c7c0a3405845cecd3fe06f3c20ab413302fc73" }
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "cb871cfa1ee460b51eb1d144b175b9aab9c50aba" }

[workspace]