Skip to content

Commit

Permalink
WIP: hello world from wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhantk232 committed Mar 27, 2024
1 parent fa14b01 commit a94c0c6
Show file tree
Hide file tree
Showing 10 changed files with 501 additions and 0 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.direnv
.packages
.build
.DS_Store
.idea
ec/target
ec.wasm
276 changes: 276 additions & 0 deletions ec/Cargo.lock

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

16 changes: 16 additions & 0 deletions ec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "ec"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

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

[dependencies]
ft-sdk = "0.1.1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
http = "1.0"
bytes = "1.0"
10 changes: 10 additions & 0 deletions ec/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extern crate self as ec;

mod route;

#[no_mangle]
pub extern "C" fn main_ft() {
let req = ft_sdk::http::current_request();
let resp = route::route(req);
ft_sdk::http::send_response(resp);
}
46 changes: 46 additions & 0 deletions ec/src/route/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
pub fn route(r: http::Request<bytes::Bytes>) -> http::Response<bytes::Bytes> {
use ft_sdk::Layout;

match r.uri().path() {
// site
"/wasm-test/" => Dummy::page::<Hello>(r),
t => ft_sdk::not_found!("no route for {t}"),
}
}

struct Dummy {}

impl ft_sdk::Layout for Dummy {
type Error = ft_sdk::Error;

fn from_in(_in_: ft_sdk::In, _ty: ft_sdk::RequestType) -> Result<Self, Self::Error> {
ft_sdk::println!("from_in 1");

Ok(Self {})
}

fn json(&mut self, page: serde_json::Value) -> Result<serde_json::Value, Self::Error> {
Ok(serde_json::json!({"page": page}))
}

fn render_error(e: Self::Error) -> http::Response<bytes::Bytes> {
ft_sdk::println!("rendering error: {e:?}");
ft_sdk::json_response(serde_json::json!({
"error": e.to_string(),
}))
}
}

#[derive(serde::Serialize)]
pub struct Hello {
msg: String,
}

impl ft_sdk::Page<Dummy, ft_sdk::Error> for Hello {
fn page(_i: &mut Dummy) -> Result<Self, ft_sdk::Error> {
ft_sdk::println!("hello wasm");
Ok(Hello {
msg: "hello from ft_sdk".into(),
})
}
}
Loading

0 comments on commit a94c0c6

Please sign in to comment.