diff --git a/src/catalog.rs b/src/catalog.rs index e9fa35e..5c2cc26 100644 --- a/src/catalog.rs +++ b/src/catalog.rs @@ -3,6 +3,9 @@ use std::fs; use std::path::{Path, PathBuf}; +// `` slimming: these were byte-identical copies of `knowledge::util`. +use crate::knowledge::util::{extract_fm_field, extract_fm_list, extract_nested_by, yaml_escape}; + use anyhow::{Context, Result}; use crate::lex::{classify_source, LineKind}; @@ -318,111 +321,10 @@ fn extract_frontmatter_title(source: &str) -> Option { extract_fm_field(source, "title") } -fn extract_fm_field(source: &str, key: &str) -> Option { - let source = source.strip_prefix('\u{feff}').unwrap_or(source); - let lines: Vec<&str> = source.lines().collect(); - if lines.first().map(|l| l.trim()) != Some("---") { - return None; - } - let prefix = format!("{key}:"); - for line in lines.iter().skip(1) { - let t = line.trim(); - if t == "---" { - break; - } - if let Some(rest) = t.strip_prefix(&prefix) { - return Some(rest.trim().trim_matches('"').to_string()); - } - } - None -} /// `verified:\n by: foo` → Some("foo") -fn extract_nested_by(source: &str, block: &str) -> Option { - let source = source.strip_prefix('\u{feff}').unwrap_or(source); - let lines: Vec<&str> = source.lines().collect(); - if lines.first().map(|l| l.trim()) != Some("---") { - return None; - } - let mut in_block = false; - for line in lines.iter().skip(1) { - let t = line.trim_end(); - if t.trim() == "---" { - break; - } - if t.trim() == format!("{block}:") || t.trim().starts_with(&format!("{block}:")) { - // inline `verified: { by: x }` not supported; block form: - if t.trim() == format!("{block}:") { - in_block = true; - continue; - } - } - if in_block { - let trimmed = t.trim(); - if trimmed.starts_with("by:") { - return Some( - trimmed - .trim_start_matches("by:") - .trim() - .trim_matches('"') - .to_string(), - ); - } - if !trimmed.is_empty() && !line.starts_with(' ') && !line.starts_with('\t') { - break; - } - } - } - None -} -fn extract_fm_list(source: &str, key: &str) -> Vec { - let source = source.strip_prefix('\u{feff}').unwrap_or(source); - let lines: Vec<&str> = source.lines().collect(); - if lines.first().map(|l| l.trim()) != Some("---") { - return Vec::new(); - } - let mut out = Vec::new(); - let mut in_list = false; - let header = format!("{key}:"); - for line in lines.iter().skip(1) { - let t = line.trim_end(); - if t.trim() == "---" { - break; - } - if t.trim() == header || t.trim() == format!("{key}: []") { - if t.trim().ends_with("[]") { - return Vec::new(); - } - in_list = true; - continue; - } - if in_list { - let trimmed = t.trim(); - if let Some(rest) = trimmed.strip_prefix("- ") { - out.push(rest.trim().trim_matches('"').to_string()); - } else if !trimmed.is_empty() && !line.starts_with(' ') && !line.starts_with('\t') { - break; - } - } - } - out -} -fn yaml_escape(s: &str) -> String { - if s.is_empty() - || s.contains(':') - || s.contains('#') - || s.contains('"') - || s.contains('\'') - || s.contains('\n') - || s.starts_with(' ') - { - format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\"")) - } else { - s.to_string() - } -} fn import_path(imp: &str) -> &str { imp.split(" as ").next().unwrap_or(imp).trim() diff --git a/src/host/csv.rs b/src/host/csv.rs index 749fd23..5b04f67 100644 --- a/src/host/csv.rs +++ b/src/host/csv.rs @@ -1,13 +1,8 @@ //! Minimal RFC4180 CSV for Mid M4 (`lib/csv`). +use crate::host::as_text; use crate::value::Value; -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} needs text")), - } -} /// Parse CSV text into a list of maps (first row = headers). pub fn parse(text: &Value) -> Result { diff --git a/src/host/datetime.rs b/src/host/datetime.rs index e690f68..4a14996 100644 --- a/src/host/datetime.rs +++ b/src/host/datetime.rs @@ -3,6 +3,7 @@ //! Moments are plain maps: `{unix, zone, iso}`. Zone is a fixed offset (`+08:00`) //! or alias (`UTC`); a few fixed-offset IANA names are accepted. Full tzdb is out of scope. +use crate::host::as_text; use chrono::{DateTime, Duration, FixedOffset, NaiveDate, NaiveDateTime, TimeZone, Utc}; use crate::value::Value; @@ -14,12 +15,6 @@ fn as_i64(v: &Value, label: &str) -> Result { } } -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} must be text")), - } -} fn optional_i64(v: Option<&Value>, label: &str) -> Result { match v { diff --git a/src/host/encoding.rs b/src/host/encoding.rs index 5ce6a2a..9a635d6 100644 --- a/src/host/encoding.rs +++ b/src/host/encoding.rs @@ -1,14 +1,9 @@ //! Base64 / hex encoding for Mid M1 (`lib/encoding`). //! No third-party crates — standard alphabet + padding. +use crate::host::as_text; use crate::value::Value; -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} needs text")), - } -} const B64: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; diff --git a/src/host/foreign.rs b/src/host/foreign.rs index d4cfeba..1122715 100644 --- a/src/host/foreign.rs +++ b/src/host/foreign.rs @@ -3,6 +3,7 @@ //! F0: safe one-shot execution + structured `invoke` records. //! Later phases add language registry, JSON bridge, parallel fan-out and sessions. +use crate::host::as_text; use std::collections::{HashMap, VecDeque}; use std::io::{Read, Write}; use std::panic::{catch_unwind, AssertUnwindSafe}; @@ -26,12 +27,6 @@ const MAX_OUTPUT_LIMIT: usize = 64 * 1024 * 1024; const INPUT_ENV_CAP: usize = 8 * 1024; const FAILURE_TAIL: usize = 4096; -fn as_text<'a>(v: &'a Value, what: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{what} must be text")), - } -} fn opt_text(v: Option<&Value>) -> Result, String> { match v { diff --git a/src/host/foreign_session.rs b/src/host/foreign_session.rs index 2d3d303..e60f912 100644 --- a/src/host/foreign_session.rs +++ b/src/host/foreign_session.rs @@ -3,6 +3,7 @@ //! Protocol: one JSON request per stdin line, one JSON response per original //! stdout line. User stdout/stderr are captured inside the bootstrap. +use crate::host::as_text; use std::io::{BufRead, BufReader, Read, Write}; use std::process::{Child, Command, Stdio}; use std::sync::mpsc::{self, Receiver}; @@ -171,12 +172,6 @@ impl Drop for Session { } } -fn as_text<'a>(v: &'a Value, what: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{what} must be text")), - } -} fn opt_text(v: Option<&Value>) -> Result, String> { match v { diff --git a/src/host/hash.rs b/src/host/hash.rs index b520181..1d3b801 100644 --- a/src/host/hash.rs +++ b/src/host/hash.rs @@ -1,5 +1,6 @@ //! Digest / HMAC host primitives for Mid M3 (`lib/hash`). +use crate::host::as_text; use hmac::{Hmac, Mac}; use md5::Md5; use sha1::Sha1; @@ -9,12 +10,6 @@ use crate::value::Value; type HmacSha256 = Hmac; -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} needs text")), - } -} fn to_hex(bytes: &[u8]) -> String { bytes.iter().map(|b| format!("{b:02x}")).collect() diff --git a/src/host/html_ops.rs b/src/host/html_ops.rs index 785a938..07781cb 100644 --- a/src/host/html_ops.rs +++ b/src/host/html_ops.rs @@ -1,13 +1,8 @@ //! HTML escape / unescape for Mid2 M8 (`lib/html`). +use crate::host::as_text; use crate::value::Value; -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} must be text")), - } -} pub fn escape(text: &Value) -> Result { let s = as_text(text, "text")?; diff --git a/src/host/log.rs b/src/host/log.rs index 32b8fa3..c0f54c1 100644 --- a/src/host/log.rs +++ b/src/host/log.rs @@ -1,5 +1,6 @@ //! Log level gate for Mid M6 (`lib/log`). +use crate::host::as_text; use crate::host::HostContext; use crate::value::Value; @@ -32,12 +33,6 @@ impl Level { } } -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} needs text")), - } -} pub fn set_level(ctx: &mut HostContext, level: &Value) -> Result { let s = as_text(level, "level")?; diff --git a/src/host/mod.rs b/src/host/mod.rs index 8d64986..52f78a8 100644 --- a/src/host/mod.rs +++ b/src/host/mod.rs @@ -49,6 +49,17 @@ mod sys; pub mod writeback; pub use dispatch::{call_host, HostFn}; + +/// Shared `text` argument extraction for host modules. +/// +/// `` slimming: this used to be copy-pasted in 17 host modules with two +/// different wordings; one implementation keeps messages consistent. +pub fn as_text<'a>(v: &'a crate::value::Value, label: &str) -> Result<&'a str, String> { + match v { + crate::value::Value::Text(s) => Ok(s.as_str()), + _ => Err(format!("{label} needs text")), + } +} pub use fs::path_under_root; pub use plugin::PluginState; diff --git a/src/host/net.rs b/src/host/net.rs index 354a138..732497c 100644 --- a/src/host/net.rs +++ b/src/host/net.rs @@ -1,5 +1,6 @@ //! HTTP host primitives (HTTP and HTTPS via ureq). +use crate::host::as_text; use std::io::{Read, Write}; #[cfg(feature = "net-host")] use std::io::{BufRead, BufReader}; @@ -10,12 +11,6 @@ use crate::value::Value; #[cfg(feature = "net-host")] use crate::host::HostContext; -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} must be text")), - } -} #[cfg(feature = "net-host")] fn headers_from_value(headers: Option<&Value>) -> Result, String> { diff --git a/src/host/path.rs b/src/host/path.rs index a2d384b..c6cb872 100644 --- a/src/host/path.rs +++ b/src/host/path.rs @@ -1,15 +1,10 @@ //! Path algebra for Mid M2 (`lib/path`) — no I/O. +use crate::host::as_text; use std::path::{Component, Path, PathBuf}; use crate::value::Value; -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} needs text")), - } -} fn path_to_text(p: &Path) -> String { if p.as_os_str().is_empty() { diff --git a/src/host/plugin.rs b/src/host/plugin.rs index 69b33de..ae3cafa 100644 --- a/src/host/plugin.rs +++ b/src/host/plugin.rs @@ -1,5 +1,6 @@ //! Native plugin loader (C ABI v1/v2). Shared libs are not linked into marqdo. +use crate::host::as_text; use std::cell::{Cell, RefCell}; use std::collections::HashMap; use std::ffi::{CStr, CString}; @@ -170,12 +171,6 @@ impl PluginState { } } -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} must be text")), - } -} extern "C" { fn malloc(n: usize) -> *mut c_void; diff --git a/src/host/re.rs b/src/host/re.rs index 18fe2ea..c6ba830 100644 --- a/src/host/re.rs +++ b/src/host/re.rs @@ -1,14 +1,9 @@ //! Regex host primitives for Mid M1 (`lib/re`). +use crate::host::as_text; use crate::value::Value; use regex::Regex; -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} needs text")), - } -} fn compile(pattern: &str) -> Result { Regex::new(pattern).map_err(|e| format!("invalid regex: {e}")) diff --git a/src/host/subtask.rs b/src/host/subtask.rs index f7e850c..6cf1844 100644 --- a/src/host/subtask.rs +++ b/src/host/subtask.rs @@ -4,6 +4,7 @@ //! Quiet file children get `MARQDO_EVENT_FORWARD` so LLM stream events published in the //! child process are tailed into the parent EventBus while `wait` blocks (view SSE). +use crate::host::as_text; use std::collections::HashMap; use std::io::{Read, Seek, SeekFrom}; use std::path::PathBuf; @@ -136,12 +137,6 @@ pub(crate) struct FunctionTask { handle: Option>, } -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} must be text")), - } -} fn as_code(v: &Value) -> Result<&CodeBlock, String> { match v { diff --git a/src/host/sys.rs b/src/host/sys.rs index 2b73a17..16ac2fc 100644 --- a/src/host/sys.rs +++ b/src/host/sys.rs @@ -1,17 +1,12 @@ //! System / process host primitives. +use crate::host::as_text; #[cfg(feature = "exec-host")] use std::process::Command; use crate::host::HostContext; use crate::value::Value; -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} must be text")), - } -} fn as_i64(v: &Value, label: &str) -> Result { match v { diff --git a/src/host/text_ops.rs b/src/host/text_ops.rs index 12264ea..cc914ad 100644 --- a/src/host/text_ops.rs +++ b/src/host/text_ops.rs @@ -1,13 +1,8 @@ //! Text helpers for Mid M4 (`lib/text` thicken). +use crate::host::as_text; use crate::value::Value; -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} needs text")), - } -} fn as_usize(v: &Value, label: &str) -> Result { match v { diff --git a/src/host/time.rs b/src/host/time.rs index 5f521c9..3174a22 100644 --- a/src/host/time.rs +++ b/src/host/time.rs @@ -1,5 +1,6 @@ //! Time host primitives. +use crate::host::as_text; use std::time::Duration; #[cfg(not(target_arch = "wasm32"))] use std::time::{SystemTime, UNIX_EPOCH}; @@ -47,12 +48,6 @@ fn as_i64(v: &Value, label: &str) -> Result { } } -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} must be text")), - } -} pub fn format_time(unix: &Value, pattern: &Value) -> Result { let secs = as_i64(unix, "unix")?; diff --git a/src/host/toml_ops.rs b/src/host/toml_ops.rs index 5794ed7..1ca8384 100644 --- a/src/host/toml_ops.rs +++ b/src/host/toml_ops.rs @@ -2,15 +2,10 @@ //! Subset: comments, keys, strings, ints, floats, bools, arrays of scalars, //! `[table]` / dotted tables. No datetime, inline tables, or array-of-tables. +use crate::host::as_text; use crate::host::json::json_to_value; use crate::value::Value; -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} must be text")), - } -} pub fn parse(text: &Value) -> Result { let raw = as_text(text, "text")?; diff --git a/src/host/url_ops.rs b/src/host/url_ops.rs index d571230..95a6538 100644 --- a/src/host/url_ops.rs +++ b/src/host/url_ops.rs @@ -2,14 +2,9 @@ //! Distinct from `net::url_encode` (single-segment form encoding). //! Hand-rolled subset (no `url` crate): scheme://[userinfo@]host[:port]/path[?query][#frag]. +use crate::host::as_text; use crate::value::Value; -fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { - match v { - Value::Text(s) => Ok(s.as_str()), - _ => Err(format!("{label} must be text")), - } -} fn encode_component(s: &str) -> String { let mut out = String::new(); diff --git a/src/view/mod.rs b/src/view/mod.rs index 7f0f5f9..b3d3616 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -168,6 +168,59 @@ fn is_mq_md(path: &Path) -> bool { .unwrap_or(false) } + +/// POST API route tables. `` slimming: 39 hand-written match arms across the +/// two handlers collapsed into tables; each arm repeated the `api_debug_post` wrapper. +type NotebookFn = fn(&std::path::Path, &str) -> serde_json::Value; +type DebugFn = fn(&str) -> serde_json::Value; + +#[derive(Clone, Copy)] +enum ApiRoute { + Notebook(NotebookFn), + Debug(DebugFn), + Action(crate::debug::DebugAction), +} + +const API_ROUTES: &[(&str, ApiRoute)] = &[ + ("/api/session/open", ApiRoute::Notebook(notebook_api::api_open)), + ("/api/session/reload", ApiRoute::Notebook(notebook_api::api_reload)), + ("/api/session/close", ApiRoute::Notebook(notebook_api::api_close)), + ("/api/session/run_document", ApiRoute::Notebook(notebook_api::api_run_document)), + ("/api/session/snapshot/export", ApiRoute::Notebook(notebook_api::api_export_snapshot)), + ("/api/session/snapshot/import", ApiRoute::Notebook(notebook_api::api_import_snapshot)), + ("/api/session/snapshot/replay", ApiRoute::Notebook(notebook_api::api_replay_snapshot)), + ("/api/session/plan/assign", ApiRoute::Notebook(notebook_api::api_plan_assign)), + ("/api/session/plan/add", ApiRoute::Notebook(notebook_api::api_plan_add)), + ("/api/session/plan/remove", ApiRoute::Notebook(notebook_api::api_plan_remove)), + ("/api/session/plan/value", ApiRoute::Notebook(notebook_api::api_plan_value)), + ("/api/session/cell/persist", ApiRoute::Notebook(notebook_api::api_persist_cell)), + ("/api/session/interrupt", ApiRoute::Notebook(notebook_api::api_interrupt)), + ("/api/session/cell/clear", ApiRoute::Notebook(notebook_api::api_clear_cell)), + ("/api/session/clear", ApiRoute::Notebook(notebook_api::api_clear)), + ("/api/session/cell/run", ApiRoute::Notebook(notebook_api::api_cell_run)), + ("/api/session/cell/step", ApiRoute::Notebook(notebook_api::api_cell_step)), + ("/api/session/run_all", ApiRoute::Notebook(notebook_api::api_run_all)), + ("/api/session/kernel/restart", ApiRoute::Notebook(notebook_api::api_kernel_restart)), + ("/api/session/kernel/assign", ApiRoute::Notebook(notebook_api::api_assign)), + ("/api/session/state/list", ApiRoute::Notebook(notebook_api::api_state_list)), + ("/api/session/state/push", ApiRoute::Notebook(notebook_api::api_state_push)), + ("/api/session/state/pull", ApiRoute::Notebook(notebook_api::api_state_pull)), + ("/api/session/journal", ApiRoute::Notebook(notebook_api::api_journal)), + ("/api/debug/start", ApiRoute::Notebook(debug_api::api_debug_start)), + ("/api/debug/continue", ApiRoute::Action(crate::debug::DebugAction::Continue)), + ("/api/debug/step", ApiRoute::Action(crate::debug::DebugAction::Step)), + ("/api/debug/breakpoints", ApiRoute::Debug(debug_api::api_debug_set_breakpoints)), + ("/api/debug/stop", ApiRoute::Debug(debug_api::api_debug_stop)), +]; + +const API_ROUTES_DEBUG: &[(&str, ApiRoute)] = &[ + ("/api/debug/start", ApiRoute::Notebook(debug_api::api_debug_start)), + ("/api/debug/continue", ApiRoute::Action(crate::debug::DebugAction::Continue)), + ("/api/debug/step", ApiRoute::Action(crate::debug::DebugAction::Step)), + ("/api/debug/breakpoints", ApiRoute::Debug(debug_api::api_debug_set_breakpoints)), + ("/api/debug/stop", ApiRoute::Debug(debug_api::api_debug_stop)), +]; + fn handle(root: &ViewRoot, request: Request) -> Result<()> { let url = request.url().to_string(); let method = request.method().clone(); @@ -177,150 +230,14 @@ fn handle(root: &ViewRoot, request: Request) -> Result<()> { match path_part { "/api/foreign-run" => return api_foreign_run(root, request), "/api/run" => return api_run(root, request), - "/api/session/open" => { - return api_debug_post(request, |body| { - notebook_api::api_open(&root.root, body) - }) - } - "/api/session/reload" => { - return api_debug_post(request, |body| { - notebook_api::api_reload(&root.root, body) - }) - } - "/api/session/close" => { - return api_debug_post(request, |body| { - notebook_api::api_close(&root.root, body) - }) - } - "/api/session/run_document" => { - return api_debug_post(request, |body| { - notebook_api::api_run_document(&root.root, body) - }) - } - "/api/session/snapshot/export" => { - return api_debug_post(request, |body| { - notebook_api::api_export_snapshot(&root.root, body) - }) - } - "/api/session/snapshot/import" => { - return api_debug_post(request, |body| { - notebook_api::api_import_snapshot(&root.root, body) - }) - } - "/api/session/snapshot/replay" => { - return api_debug_post(request, |body| { - notebook_api::api_replay_snapshot(&root.root, body) - }) - } - "/api/session/plan/assign" => { - return api_debug_post(request, |body| { - notebook_api::api_plan_assign(&root.root, body) - }) - } - "/api/session/plan/add" => { - return api_debug_post(request, |body| { - notebook_api::api_plan_add(&root.root, body) - }) - } - "/api/session/plan/remove" => { - return api_debug_post(request, |body| { - notebook_api::api_plan_remove(&root.root, body) - }) - } - "/api/session/plan/value" => { - return api_debug_post(request, |body| { - notebook_api::api_plan_value(&root.root, body) - }) - } - "/api/session/cell/persist" => { - return api_debug_post(request, |body| { - notebook_api::api_persist_cell(&root.root, body) - }) - } - "/api/session/interrupt" => { - return api_debug_post(request, |body| { - notebook_api::api_interrupt(&root.root, body) - }) - } - "/api/session/cell/clear" => { - return api_debug_post(request, |body| { - notebook_api::api_clear_cell(&root.root, body) - }) - } - "/api/session/clear" => { - return api_debug_post(request, |body| { - notebook_api::api_clear(&root.root, body) - }) - } - "/api/session/cell/run" => { - return api_debug_post(request, |body| { - notebook_api::api_cell_run(&root.root, body) - }) - } - "/api/session/cell/step" => { - return api_debug_post(request, |body| { - notebook_api::api_cell_step(&root.root, body) - }) - } - "/api/session/run_all" => { - return api_debug_post(request, |body| { - notebook_api::api_run_all(&root.root, body) - }) - } - "/api/session/kernel/restart" => { - return api_debug_post(request, |body| { - notebook_api::api_kernel_restart(&root.root, body) - }) - } - "/api/session/kernel/assign" => { - return api_debug_post(request, |body| { - notebook_api::api_assign(&root.root, body) - }) - } - "/api/session/state/list" => { - return api_debug_post(request, |body| { - notebook_api::api_state_list(&root.root, body) - }) - } - "/api/session/state/push" => { - return api_debug_post(request, |body| { - notebook_api::api_state_push(&root.root, body) - }) - } - "/api/session/state/pull" => { - return api_debug_post(request, |body| { - notebook_api::api_state_pull(&root.root, body) - }) - } - "/api/session/journal" => { - return api_debug_post(request, |body| { - notebook_api::api_journal(&root.root, body) - }) - } - "/api/debug/start" => { - return api_debug_post(request, |body| { - debug_api::api_debug_start(&root.root, body) - }) - } - "/api/debug/continue" => { - return api_debug_post(request, |body| { - debug_api::api_debug_action(body, crate::debug::DebugAction::Continue) - }) - } - "/api/debug/step" => { - return api_debug_post(request, |body| { - debug_api::api_debug_action(body, crate::debug::DebugAction::Step) - }) - } - "/api/debug/stop" => { - return api_debug_post(request, |body| debug_api::api_debug_stop(body)) - } - "/api/debug/breakpoints" => { - return api_debug_post(request, |body| { - debug_api::api_debug_set_breakpoints(body) - }) - } - _ => { + other => { + if let Some((_, route)) = API_ROUTES.iter().find(|(p, _)| *p == other) { + return api_debug_post(request, |body| match route { + ApiRoute::Notebook(f) => f(&root.root, body), + ApiRoute::Debug(f) => f(body), + ApiRoute::Action(a) => debug_api::api_debug_action(body, *a), + }); + } let resp = Response::from_string("method not allowed").with_status_code(StatusCode(405)); let _ = request.respond(resp); @@ -405,30 +322,14 @@ fn handle_debug(root: &ViewRoot, request: Request) -> Result<()> { let (path_part, _) = split_url(&url); match path_part { "/api/foreign-run" => return api_foreign_run(root, request), - "/api/debug/start" => { - return api_debug_post(request, |body| { - debug_api::api_debug_start(&root.root, body) - }) - } - "/api/debug/continue" => { - return api_debug_post(request, |body| { - debug_api::api_debug_action(body, crate::debug::DebugAction::Continue) - }) - } - "/api/debug/step" => { - return api_debug_post(request, |body| { - debug_api::api_debug_action(body, crate::debug::DebugAction::Step) - }) - } - "/api/debug/stop" => { - return api_debug_post(request, |body| debug_api::api_debug_stop(body)) - } - "/api/debug/breakpoints" => { - return api_debug_post(request, |body| { - debug_api::api_debug_set_breakpoints(body) - }) - } - _ => { + other => { + if let Some((_, route)) = API_ROUTES_DEBUG.iter().find(|(p, _)| *p == other) { + return api_debug_post(request, |body| match route { + ApiRoute::Notebook(f) => f(&root.root, body), + ApiRoute::Debug(f) => f(body), + ApiRoute::Action(a) => debug_api::api_debug_action(body, *a), + }); + } let resp = Response::from_string("method not allowed").with_status_code(StatusCode(405)); let _ = request.respond(resp);