worker 0.8.2

A Rust SDK for writing Cloudflare Workers.
Documentation
use wasm_bindgen::JsCast;

use crate::{EnvBinding, Result};

// The full email surface comes from the auto-generated `crate::email`
// module (`chomp build:types` from `types/email.d.ts`). This file only
// adds the [`EnvBinding`] trait impl on top of the auto-gen `SendEmail`
// extern type so `Env::send_email("EMAIL")` resolves cleanly.
//
// `EmailMessage` is the constructor class imported from
// `cloudflare:email`, used to build raw-MIME messages and pass to
// `SendEmail.send(message)`. It also doubles as the instance-shape
// type referenced by `ForwardableEmailMessage` and `reply()` — same JS
// object collapsed to a single Rust type, see the d.ts EDIT note for
// the naming rationale.
pub use crate::email::email::EmailMessage;
pub use crate::email::{
    EmailAddress, EmailAttachment, EmailSendResult, SendEmail, SendEmailBuilder,
};

impl EnvBinding for SendEmail {
    const TYPE_NAME: &'static str = "SendEmail";

    // `SendEmail` is a TypeScript interface, not a class — the runtime
    // doesn't expose a `SendEmail` global for the default
    // `constructor.name` check to match against. The TS types are
    // authoritative: if `env.EMAIL` is bound to a SendEmail per
    // `wrangler.toml`, the runtime hands us the right shape, so we
    // skip the check and `unchecked_into`.
    fn get(val: wasm_bindgen::JsValue) -> Result<Self> {
        Ok(val.unchecked_into())
    }
}

#[cfg(test)]
mod send_check {
    // `SendEmail` is `Send` automatically — wasm-bindgen makes `JsValue`
    // `Send + Sync` and every extern `pub type` in `email.rs` carries that
    // through. This compile-time check guards against an upstream
    // regression.
    use super::SendEmail;
    fn _assert_send<T: Send>() {}
    #[allow(dead_code)]
    fn _check() {
        _assert_send::<SendEmail>();
    }
}