Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions examples/serde-rust-structs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
node_modules
.wrangler
17 changes: 17 additions & 0 deletions examples/serde-rust-structs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "serde-rust-structs"
version = "0.1.0"
edition = "2021"

[package.metadata.release]
release = false

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

[dependencies]
worker = { version = "0.7", features = ['http'] }
worker-macros = { version = "0.7", features = ['http'] }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is an unused dependency. 🙂

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Will remove it soon!

http = "1.3"
serde = "1.0.228"
serde_json = "1.0.149"

@jakubadamw jakubadamw Feb 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is an unused dependency. This is important in so far as using serde_json in code compiled to Wasm and deployed to Cloudflare Workers is normally discouraged in favour of serde-wasm-bindgen and using the native JSON.*() functions (through, say, js-sys) as that reduces final code bloat. But anyway, your example doesn't actually use serde_json in any way, so this can be removed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for letting me know.

2 changes: 2 additions & 0 deletions examples/serde-rust-structs/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Using Serde to populate Rust structures
Demo of using ```serde::{Serialize, Deserialize}``` to populate structures from an API, and then return a JSON value from a structure.
66 changes: 66 additions & 0 deletions examples/serde-rust-structs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use worker::{ Context, Env, Fetch, Request, Response, Result, RouteContext, Router, event };
use serde::{ Serialize, Deserialize };

#[derive(Serialize, Deserialize, Clone)]
pub struct ImageURL {
pub image_url: String,
}

#[derive(Serialize, Deserialize, Clone)]
pub struct Images {
pub jpg: ImageURL,
}

#[derive(Serialize, Deserialize, Clone)]
pub struct Data {
pub mal_id: u32,
pub url: String,
pub title: String,
pub date: String,
pub author_username: String,
pub author_url: String,
pub forum_url: String,
pub images: Images,
pub comments: u32,
pub excerpt: String,
}

#[derive(Serialize, Deserialize, Clone)]
pub struct Pagination {
pub last_visible_page: u32,
pub has_next_page: bool,
}

// The main struct from the API
#[derive(Serialize, Deserialize, Clone)]
pub struct APIResult {
pub pagination: Pagination,
pub data: Vec<Data>,
}

// Handle route for /anime/:id
async fn get_anime_news(_req: Request, ctx: RouteContext<()>) -> Result<Response> {
let id = ctx.param("id").ok_or_else(|| worker::Error::RustError("Missing 'id' param".into()))?;

let url = format!("https://api.jikan.moe/v4/anime/{}/news", id); //Ex id: 38524

let request = http::Request
::builder()
.method("GET")
.uri(url.as_str())
.header("Content-Type", "application/json")
.header("Accept", "*/*")
.body(String::new())
.unwrap();

let api_res: APIResult = Fetch::Request(worker::Request::try_from(request).unwrap())
.send().await?
.json().await?;

Response::from_json(&api_res)
}

#[event(fetch)]
async fn fetch(req: Request, env: Env, _ctx: Context) -> Result<Response> {
Router::new().get_async("/anime/:id", get_anime_news).run(req, env).await
}
6 changes: 6 additions & 0 deletions examples/serde-rust-structs/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name = "serde-rust-structs"
main = "build/index.js"
compatibility_date = "2026-02-07"

[build]
command = "cargo install -q worker-build@^0.7 && worker-build --release"