Skip to content
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
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
16 changes: 16 additions & 0 deletions examples/serde-rust-structs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[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'] }
http = "1.3"
serde = "1.0.228"
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"