lib.rsannotatedlib.rssource44 lines · 1.5 KB · raw

What a site needs from the Workers runtime and from nothing else: where the log goes, and the deploy's variables. Responses are aldebaran-axum's; the Worker family answers over axum too.

4use aldebaran_gate::Switch;
5use aldebaran_log::{Level, Route, Served};

Writes one request's line to the Worker's console at its level.

8pub fn write<R: Route>(served: &Served<R>) {
9    emit(served.level(), &served.to_string());
10}

Something the Worker itself got wrong, in words that carry no request.

13pub fn failure(what: &str) {
14    emit(Level::Error, what);
15}
17#[cfg(target_arch = "wasm32")]
18fn emit(level: Level, line: &str) {
19    match level {
20        Level::Debug => worker::console_debug!("{line}"),
21        Level::Info => worker::console_log!("{line}"),
22        Level::Warn => worker::console_warn!("{line}"),
23        Level::Error => worker::console_error!("{line}"),
24    }
25}

Native builds (the tests) have no console to write to.

28#[cfg(not(target_arch = "wasm32"))]
29fn emit(_level: Level, _line: &str) {}

A variable of the deploy, or None if it is not set.

32pub fn var(env: &worker::Env, name: &str) -> Option<String> {
33    env.var(name).ok().map(|value| value.to_string())
34}

A deploy's on/off switch. A value that is neither is complained about in the console, and the site runs as off.

38pub fn switch(env: &worker::Env, name: &str) -> Switch {
39    let (switch, complaint) = Switch::resolve(name, var(env, name).as_deref());
40    if let Some(complaint) = complaint {
41        failure(&complaint);
42    }
43    switch
44}