Skip to content

Commit

Permalink
test-programs: merge all wasi content generated by wit into the same mod
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Jul 11, 2024
1 parent 0e5048e commit d16672c
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 39 deletions.
12 changes: 8 additions & 4 deletions crates/test-programs/src/bin/api_proxy.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use test_programs::wasi::http::types::{
Headers, IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam,
use test_programs::wasi::{
exports::http::incoming_handler,
http::{
proxy,
types::{Headers, IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam},
},
};

struct T;

test_programs::proxy::export!(T);
proxy::export!(T);

impl test_programs::proxy::exports::wasi::http::incoming_handler::Guest for T {
impl incoming_handler::Guest for T {
fn handle(request: IncomingRequest, outparam: ResponseOutparam) {
assert!(request.scheme().is_some());
assert!(request.authority().is_some());
Expand Down
12 changes: 8 additions & 4 deletions crates/test-programs/src/bin/api_proxy_forward_request.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use test_programs::wasi::http::types::{
Headers, IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam,
use test_programs::wasi::{
exports::http::incoming_handler,
http::{
proxy,
types::{Headers, IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam},
},
};

struct T;

test_programs::proxy::export!(T);
proxy::export!(T);

impl test_programs::proxy::exports::wasi::http::incoming_handler::Guest for T {
impl incoming_handler::Guest for T {
fn handle(request: IncomingRequest, outparam: ResponseOutparam) {
let res = test_programs::http::request(
request.method(),
Expand Down
16 changes: 11 additions & 5 deletions crates/test-programs/src/bin/api_proxy_streaming.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
use anyhow::{anyhow, bail, Result};
use futures::{future, stream, Future, SinkExt, StreamExt, TryStreamExt};
use test_programs::wasi::http::types::{
Fields, IncomingRequest, IncomingResponse, Method, OutgoingBody, OutgoingRequest,
OutgoingResponse, ResponseOutparam, Scheme,
use test_programs::wasi::{
exports::http::incoming_handler,
http::{
proxy,
types::{
Fields, IncomingRequest, IncomingResponse, Method, OutgoingBody, OutgoingRequest,
OutgoingResponse, ResponseOutparam, Scheme,
},
},
};
use url::Url;

const MAX_CONCURRENCY: usize = 16;

struct Handler;

test_programs::proxy::export!(Handler);
proxy::export!(Handler);

impl test_programs::proxy::exports::wasi::http::incoming_handler::Guest for Handler {
impl incoming_handler::Guest for Handler {
fn handle(request: IncomingRequest, response_out: ResponseOutparam) {
executor::run(async move {
handle_request(request, response_out).await;
Expand Down
11 changes: 7 additions & 4 deletions crates/test-programs/src/bin/cli_serve_authority_and_scheme.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use test_programs::proxy;
use test_programs::wasi::http::types::{
Fields, IncomingRequest, OutgoingResponse, ResponseOutparam, Scheme,
use test_programs::wasi::{
exports::http::incoming_handler,
http::{
proxy,
types::{Fields, IncomingRequest, OutgoingResponse, ResponseOutparam, Scheme},
},
};

struct T;

proxy::export!(T);

impl proxy::exports::wasi::http::incoming_handler::Guest for T {
impl incoming_handler::Guest for T {
fn handle(request: IncomingRequest, outparam: ResponseOutparam) {
let authority = request.authority();
let scheme = request.scheme();
Expand Down
11 changes: 7 additions & 4 deletions crates/test-programs/src/bin/cli_serve_echo_env.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use test_programs::proxy;
use test_programs::wasi::http::types::{
Fields, IncomingRequest, OutgoingResponse, ResponseOutparam,
use test_programs::wasi::{
exports::http::incoming_handler,
http::{
proxy,
types::{Fields, IncomingRequest, OutgoingResponse, ResponseOutparam},
},
};

struct T;

proxy::export!(T);

impl proxy::exports::wasi::http::incoming_handler::Guest for T {
impl incoming_handler::Guest for T {
fn handle(request: IncomingRequest, outparam: ResponseOutparam) {
let headers = request.headers();
let header_key = "env".to_string();
Expand Down
11 changes: 7 additions & 4 deletions crates/test-programs/src/bin/cli_serve_with_print.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use std::io::Write;
use test_programs::proxy;
use test_programs::wasi::http::types::{
Fields, IncomingRequest, OutgoingResponse, ResponseOutparam,
use test_programs::wasi::{
exports::http::incoming_handler,
http::{
proxy,
types::{Fields, IncomingRequest, OutgoingResponse, ResponseOutparam},
},
};

struct T;

proxy::export!(T);

impl proxy::exports::wasi::http::incoming_handler::Guest for T {
impl incoming_handler::Guest for T {
fn handle(_request: IncomingRequest, outparam: ResponseOutparam) {
print!("this is half a print ");
std::io::stdout().flush().unwrap();
Expand Down
46 changes: 37 additions & 9 deletions crates/test-programs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ pub mod nn;
pub mod preview1;
pub mod sockets;

wit_bindgen::generate!({
world: "test-command",
path: "../wasi/wit",
generate_all,
});
mod cli {
wit_bindgen::generate!({
path: "../wasi/wit",
world: "wasi:cli/command",
default_bindings_module: "test_programs::wasi",
generate_all,
});
}

pub mod proxy {
mod proxy {
wit_bindgen::generate!({
path: "../wasi-http/wit",
world: "wasi:http/proxy",
default_bindings_module: "test_programs::proxy",
default_bindings_module: "test_programs::wasi",
pub_export_macro: true,
export_macro_name: "_export_proxy",
with: {
"wasi:http/[email protected]": crate::wasi::http::types,
"wasi:http/[email protected]": crate::wasi::http::outgoing_handler,
"wasi:random/[email protected]": crate::wasi::random::random,
"wasi:io/[email protected]": crate::wasi::io::error,
"wasi:io/[email protected]": crate::wasi::io::poll,
Expand All @@ -30,3 +32,29 @@ pub mod proxy {
},
});
}

pub mod wasi {
pub use crate::cli::wasi::*;

pub mod http {
pub use crate::proxy::wasi::http::*;
pub mod proxy {
pub use crate::proxy::_export_proxy as export;
}
}

pub mod exports {
// This is required by the `export!` macros of this crate which assume that
// the types it's referring to show up as `exports::wasi::...`.
#[doc(hidden)]
pub mod wasi {
pub use crate::proxy::exports::wasi::*;
}

pub use crate::proxy::exports::wasi::http;
}

// These macros are used by recursive invocations of the macro
#[doc(hidden)]
pub use crate::proxy::_export_proxy;
}
4 changes: 1 addition & 3 deletions crates/wasi-http/wit/test.wit
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ world test-reactor {
}

world test-command {
include wasi:cli/imports@0.2.0;
import wasi:http/types@0.2.0;
import wasi:http/outgoing-handler@0.2.0;
include wasi:http/proxy@0.2.0;
}
2 changes: 0 additions & 2 deletions crates/wasi/wit/test.wit
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ world test-reactor {

world test-command {
include wasi:cli/imports@0.2.0;
import wasi:http/types@0.2.0;
import wasi:http/outgoing-handler@0.2.0;
}

0 comments on commit d16672c

Please sign in to comment.