refactor: 精简三处重复 — as_text×18、catalog 助手×4、view 路由 34 臂
按审计结论清理冗余(不改变行为):
1. as_text:17 个 host 模块各自复制同一函数(两种措辞),合并为
crate::host::as_text(统一 "{label} needs text",无测试依赖旧措辞)
2. catalog.rs:extract_fm_field / extract_nested_by / extract_fm_list /
yaml_escape 与 knowledge::util 逐字相同,改为直接引用
3. view/mod.rs:handle 29 臂 + handle_debug 5 臂折叠成两张路由表
(Notebook/Debug/Action 三种处理器),每臂原本重复同一 api_debug_post 包装
实测:
- cargo build 零警告;cargo test --release 605 passed / 0 failed
- view 服务实测:GET / → 200;POST /api/debug/continue → 200(真实响应
{"error":"unknown session"});POST /api/nope → 405
行数:view/mod.rs -99(168 删 69 增);src 合计由 53,276 降至约 53,000
(as_text 18 文件各约 6 行、catalog 4 函数约 70 行)
期间发现并修正一处真实风险:路由臂分布在两个 handler(handle / handle_debug),
首次只替换了一个导致 debug 路由 405;已按 handler 分别建表并由实测覆盖。
This commit is contained in:
1 parent
6ae0d39e5a
commit
8b0ea9ed7a
21 files changed
+101
-377
No files matched your search
+3
-101
@@ -3,6 +3,9 @@
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
// `<TAB>` 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<String> {
|
||||
extract_fm_field(source, "title")
|
||||
}
|
||||
|
||||
fn extract_fm_field(source: &str, key: &str) -> Option<String> {
|
||||
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<String> {
|
||||
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<String> {
|
||||
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()
|
||||
|
||||
+1
-6
@@ -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<Value, String> {
|
||||
|
||||
@@ -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<i64, String> {
|
||||
}
|
||||
}
|
||||
|
||||
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<i64, String> {
|
||||
match v {
|
||||
|
||||
@@ -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+/";
|
||||
|
||||
|
||||
+1
-6
@@ -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<Option<String>, String> {
|
||||
match v {
|
||||
|
||||
@@ -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<Option<String>, String> {
|
||||
match v {
|
||||
|
||||
+1
-6
@@ -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<Sha256>;
|
||||
|
||||
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()
|
||||
|
||||
@@ -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<Value, String> {
|
||||
let s = as_text(text, "text")?;
|
||||
|
||||
+1
-6
@@ -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<Value, String> {
|
||||
let s = as_text(level, "level")?;
|
||||
|
||||
@@ -49,6 +49,17 @@ mod sys;
|
||||
pub mod writeback;
|
||||
|
||||
pub use dispatch::{call_host, HostFn};
|
||||
|
||||
/// Shared `text` argument extraction for host modules.
|
||||
///
|
||||
/// `<TAB>` 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;
|
||||
|
||||
|
||||
+1
-6
@@ -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<Vec<(String, String)>, String> {
|
||||
|
||||
+1
-6
@@ -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() {
|
||||
|
||||
+1
-6
@@ -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;
|
||||
|
||||
+1
-6
@@ -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, String> {
|
||||
Regex::new(pattern).map_err(|e| format!("invalid regex: {e}"))
|
||||
|
||||
+1
-6
@@ -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<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
+1
-6
@@ -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<i64, String> {
|
||||
match v {
|
||||
|
||||
@@ -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<usize, String> {
|
||||
match v {
|
||||
|
||||
+1
-6
@@ -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<i64, String> {
|
||||
}
|
||||
}
|
||||
|
||||
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<Value, String> {
|
||||
let secs = as_i64(unix, "unix")?;
|
||||
|
||||
@@ -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<Value, String> {
|
||||
let raw = as_text(text, "text")?;
|
||||
|
||||
+1
-6
@@ -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();
|
||||
|
||||
+69
-168
@@ -168,6 +168,59 @@ fn is_mq_md(path: &Path) -> bool {
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
|
||||
/// POST API route tables. `<TAB>` 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);
|
||||
|
||||
Reference in new issue
Block a user