//! The owner's acts on a domain, from a device: what a name costs, register//! it, hand it to another database. Each is a signed call to the serving Mac//! (`cotton-server` `routes/domains.rs`), which holds the Cloudflare token//! and does the act; this device sends the row's id and what the owner//! answered, and then pulls once so the row it draws is the one the Mac wrote.//!//! They run only as the Domains page's commands (`COMMAND_METHODS`), so the//! price Register sends is the price on the row the owner was shown, bound by//! the build's own copy of the definition and never typed by a caller.use serde_json::{json, Value};usecrate::handle::CottonHandle;usecrate::phone::owner;usecrate::methods::MethodError;pubconst METHODS: [&str; 3] = ["domains.checkPrice", "domains.register", "domains.handOver"];/// The server's path for one act on the row `id`.fn path(id: &str, act: &str) -> String {let segment: String = id .bytes() .map(|byte| match byte { b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => (byte as char).to_string(), other => format!("%{other:02X}"), }) .collect(); format!("/v1/domains/{segment}/{act}")}/// The request one method sends: its path and its body. `None` for a method/// that is not one of these, or a request without the row's id.pubfn request_for(method: &str, request: &Value) -> Option<(String, Value)> {let text = |key: &str| request.get(key).and_then(Value::as_str).map(str::trim).filter(|text| !text.is_empty());let id = text("id")?;match method {"domains.checkPrice" => Some((path(id, "check-price"), json!({}))),"domains.register" => Some((path(id, "register"), json!({ "price": text("price") }))),"domains.handOver" => Some((path(id, "hand-over"), json!({ "database": text("database") }))), _ => None, }}pubfn call(handle: &CottonHandle, method: &str, request: &Value) -> Result<Value, MethodError> {let (path, body) = request_for(method, request) .ok_or_else(|| MethodError::new("bad_request", format!("`{method}` needs the domain's row")))?;let answer = owner::with_fetch(handle, |fetch| owner::send(fetch, owner::post(path, &body)))?;// The Mac wrote the row (a price, a registration, where it went): one// pull, best effort, so the page that asked draws it.let _ = crate::methods::sync::now(handle, &Value::Null);Ok(answer)}#[cfg(test)]mod tests {usesuper::*;/// RULE: Each domain act is one signed POST naming the row, and Register sends only the price it was handed — the one on the row. #[test]fn each_act_is_one_post_naming_the_row() {let (path, body) = request_for("domains.checkPrice", &json!({"id": "row 1"})).unwrap(); assert_eq!(path, "/v1/domains/row%201/check-price"); assert_eq!(body, json!({}));let (path, body) = request_for("domains.register", &json!({"id": "r1", "price": "USD 10.11"})).unwrap(); assert_eq!(path, "/v1/domains/r1/register"); assert_eq!(body, json!({"price": "USD 10.11"}));let (path, body) = request_for("domains.handOver", &json!({"id": "r1", "database": "db-2"})).unwrap(); assert_eq!(path, "/v1/domains/r1/hand-over"); assert_eq!(body, json!({"database": "db-2"})); assert!(request_for("domains.register", &json!({"price": "USD 10.11"})).is_none(), "no row, no call"); assert!(request_for("domains.delete", &json!({"id": "r1"})).is_none()); }}