src/site_switch.rs
//! **The administrator's switch for holger.rs's public face** (owner decision 2026-10-26, item
//! 5), on the Admin page.
//!
//! The switch exists only in a console BUILT with `public-site` (holger-ui-server's route
//! [`ROUTE`]): a default (airgap) console has no such route and answers it with the console's
//! own page, so this pane draws nothing ([`shown`]). The pane decides nothing: holger keeps the
//! position as its generic setting, refuses anyone but an administrator, and audits each flip.
#![cfg_attr(not(target_arch = "wasm32"), allow(dead_code))]
/// holger-ui-server's route for the switch (holger-public-site `setting::ROUTE`).
pub const ROUTE: &str = "/api/public-site";
/// Pure: **is the switch drawn, and in which position**: only where the console answered its
/// route with a JSON position; the console's own page (a default build), a refusal or anything
/// else draws nothing.
pub fn shown(status: u16, body: &str) -> Option<bool> {
if status != 200 {
return None;
}
serde_json::from_str::<serde_json::Value>(body).ok()?.get("on").and_then(serde_json::Value::as_bool)
}
/// The body that asks for `on`.
pub fn flip_body(on: bool) -> String {
serde_json::json!({ "on": on }).to_string()
}
/// The button's words for a switch now `on`.
pub fn button(on: bool) -> &'static str {
if on { "Switch the public site off" } else { "Switch the public site on" }
}
/// The words for a flip's answer.
pub fn said(status: u16, body: &str) -> Option<String> {
if status == 200 {
return None;
}
let words = serde_json::from_str::<serde_json::Value>(body).ok().and_then(|v| v["error"].as_str().map(str::to_owned)).unwrap_or_else(|| body.to_owned());
Some(if words.trim().is_empty() { format!("the switch was not flipped ({status})") } else { words })
}
#[cfg(test)]
mod tests {
use super::*;
/// ★ A default build answers the route with the console's own page: no switch is drawn.
#[test]
fn the_switch_is_drawn_only_where_the_console_answers_its_position() {
assert_eq!(shown(200, r#"{"on":true}"#), Some(true));
assert_eq!(shown(200, r#"{"on":false}"#), Some(false));
assert_eq!(shown(200, "<!doctype html><html>…"), None, "the console's index.html: a default build");
assert_eq!(shown(404, ""), None);
assert_eq!(shown(403, r#"{"on":true}"#), None);
}
#[test]
fn a_flip_asks_for_the_other_position_and_a_refusal_is_said_in_holgers_words() {
assert_eq!(flip_body(true), r#"{"on":true}"#);
assert_eq!(button(true), "Switch the public site off");
assert_eq!(said(200, r#"{"on":true}"#), None);
assert_eq!(said(403, r#"{"error":"only an administrator switches the public site"}"#).as_deref(), Some("only an administrator switches the public site"));
assert_eq!(said(502, "").as_deref(), Some("the switch was not flipped (502)"));
}
}