Skip to content

Commit

Permalink
Use serde-wasm-bindgen, introduce proper name mapping to kebab-case…
Browse files Browse the repository at this point in the history
… and add the rest of the properties.
  • Loading branch information
jakubadamw committed Jul 2, 2023
1 parent 0a2f45c commit 38ca3a0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 90 deletions.
2 changes: 1 addition & 1 deletion worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ js-sys = "0.3.63"
matchit = "0.4.6"
pin-project = "1.1.0"
serde = { version = "1.0.164", features = ["derive"] }
serde-wasm-bindgen = "0.5.0"
serde_json = "1.0.96"
tokio = { version = "1.28", default-features = false }
url = "2.4.0"
wasm-bindgen = "=0.2.86"
wasm-bindgen-futures = "0.4.36"
serde-wasm-bindgen = "0.5.0"
wasm-streams = "0.3.0"
worker-kv = "0.6.0"
worker-macros = { path = "../worker-macros", version = "0.0.9" }
Expand Down
163 changes: 74 additions & 89 deletions worker/src/request_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::headers::Headers;
use crate::http::Method;

use js_sys::{self, Object};
use wasm_bindgen::{prelude::*, JsValue};
use serde::Serialize;
use wasm_bindgen::JsValue;

/// Optional options struct that contains settings to apply to the `Request`.
pub struct RequestInit {
Expand Down Expand Up @@ -199,7 +200,10 @@ impl From<&CfProperties> for JsValue {
set_prop(
&obj,
&JsValue::from("minify"),
&JsValue::from(props.minify.unwrap_or(defaults.minify.unwrap_or_default())),
&serde_wasm_bindgen::to_value(
&props.minify.unwrap_or(defaults.minify.unwrap_or_default()),
)
.unwrap(),
);

set_prop(
Expand All @@ -208,11 +212,12 @@ impl From<&CfProperties> for JsValue {
&JsValue::from(props.mirage.unwrap_or(defaults.mirage.unwrap_or_default())),
);

let polish_val: &str = props
.polish
.unwrap_or(defaults.polish.unwrap_or_default())
.into();
set_prop(&obj, &JsValue::from("polish"), &JsValue::from(polish_val));
let polish_val = props.polish.unwrap_or(defaults.polish.unwrap_or_default());
set_prop(
&obj,
&JsValue::from("polish"),
&serde_wasm_bindgen::to_value(&polish_val).unwrap(),
);

set_prop(
&obj,
Expand All @@ -235,24 +240,11 @@ impl From<&CfProperties> for JsValue {
),
);

// TODO shouldn't the above calls to set_prop also use a pattern like the one below,
// such as to avoid needless work when those properties are not actually set in Rust
// code and thus should also not be passed on to the JS side;
// there may also not be a clear default for each (sub-)property, but even if there
// officially is, there may be discrepancies between official docs, official JS and
// Rust libraries introduced because they behave differently -- I'm assuming here
// that the JS library / API will simply not set default values for all fields
// that are simply not provided by the user, thus allowing the underlying runtime
// to set the true defaults, whether or not the docs agree what those are
// side benefit being that a lot less work needs to be done, such as constructing
// defaults, cloning & copying, calls across the wasm/JS bridge
if let Some(image) = &props.image {
set_prop(
&obj,
&JsValue::from("image"),
&JsValue::from(
image.clone(),
),
&serde_wasm_bindgen::to_value(&image).unwrap(),
);
}

Expand Down Expand Up @@ -295,8 +287,8 @@ impl Default for CfProperties {

/// Configuration options for Cloudflare's minification features:
/// <https://www.cloudflare.com/website-optimization/>
#[wasm_bindgen]
#[derive(Clone, Copy, Default)]
#[derive(Clone, Copy, Default, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct MinifyConfig {
pub js: bool,
pub html: bool,
Expand All @@ -305,72 +297,65 @@ pub struct MinifyConfig {

/// Configuration options for Cloudflare's image optimization feature:
/// <https://blog.cloudflare.com/introducing-polish-automatic-image-optimizati/>
#[wasm_bindgen]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Default, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum PolishConfig {
#[default]
Off,
Lossy,
Lossless,
}

impl Default for PolishConfig {
fn default() -> Self {
Self::Off
}
}

impl From<PolishConfig> for &str {
fn from(conf: PolishConfig) -> Self {
match conf {
PolishConfig::Off => "off",
PolishConfig::Lossy => "lossy",
PolishConfig::Lossless => "lossless",
}
}
#[derive(Clone, serde::Serialize)]
#[serde(untagged)]
pub enum ResizeBorder {
Uniform {
color: String,
width: usize,
},
Varying {
color: String,
top: usize,
right: usize,
bottom: usize,
left: usize,
},
}

/// Configuration options for Cloudflare's image resizing feature:
/// <https://developers.cloudflare.com/images/image-resizing/>
#[wasm_bindgen]
#[derive(Clone, Default)]
#[derive(Clone, Default, serde::Serialize)]
pub struct ResizeConfig {
pub anim: Option<bool>,
#[wasm_bindgen(skip)]
pub background: Option<String>,
#[wasm_bindgen(skip)]
pub blur: Option<String>,
pub blur: Option<u8>,
pub border: Option<ResizeBorder>,
pub brightness: Option<f64>,
pub compression: Option<ResizeCompression>,
pub contrast: Option<f64>,
pub dpr: Option<f64>,
pub fit: Option<ResizeFit>,
pub format: Option<ResizeFormat>,
pub gamma: Option<f64>,
// TODO
// #[wasm_bindgen(skip)]
// pub gravity: Option<ResizeGravity>,
pub gravity: Option<ResizeGravity>,
pub height: Option<usize>,
pub metadata: Option<ResizeMetadata>,
pub onerror: Option<ResizeOnerror>,
pub quality: Option<usize>,
pub rotate: Option<usize>,
pub sharpen: Option<usize>,
pub trim: Option<ResizeTrim>,
pub width: Option<usize>,
}

#[wasm_bindgen]
impl ResizeConfig {
#[wasm_bindgen(getter)]
pub fn background(&self) -> Option<String> {
self.background.clone()
}
#[wasm_bindgen(getter)]
pub fn blur(&self) -> Option<String> {
self.blur.clone()
}
#[derive(Clone, Copy, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ResizeCompression {
Fast,
}

#[wasm_bindgen]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ResizeFit {
ScaleDown,
Contain,
Expand All @@ -379,67 +364,67 @@ pub enum ResizeFit {
Pad,
}

#[wasm_bindgen]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ResizeFormat {
Auto,
Avif,
Webp,
Json,
}

// TODO implement in a wbg-compatible way
// #[wasm_bindgen]
// #[derive(Clone)]
// pub enum ResizeGravity {
// Auto,
// // TODO maybe enum top/left/bottom/right?
// Side(String),
// Coords(f64, f64),
// }

#[wasm_bindgen]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ResizeGravitySide {
Auto,
Left,
Right,
Top,
Bottom,
}

#[derive(Clone, Copy, Serialize)]
#[serde(rename_all = "kebab-case")]
#[serde(untagged)]
pub enum ResizeGravity {
Side(ResizeGravitySide),
Coords { x: f64, y: f64 },
}

#[derive(Clone, Copy, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ResizeMetadata {
Keep,
Copyright,
None,
}

#[wasm_bindgen]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ResizeOnerror {
Redirect,
}

#[wasm_bindgen]
#[derive(Clone, Copy, Default)]
#[derive(Clone, Copy, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct ResizeTrim {
pub top: usize,
pub bottom: usize,
pub left: usize,
pub right: usize,
pub width: usize,
pub height: usize,
}

#[wasm_bindgen]
#[derive(Default, Clone, Copy)]
#[derive(Clone, Copy, Default, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum RequestRedirect {
Error,
#[default]
Follow,
Manual,
}

impl From<RequestRedirect> for &str {
fn from(redirect: RequestRedirect) -> Self {
match redirect {
RequestRedirect::Error => "error",
RequestRedirect::Follow => "follow",
RequestRedirect::Manual => "manual",
}
}
}

impl From<RequestRedirect> for web_sys::RequestRedirect {
fn from(redir: RequestRedirect) -> Self {
match redir {
Expand Down

0 comments on commit 38ca3a0

Please sign in to comment.