From b196a446855d3dd1d51b81574022c64b700013c2 Mon Sep 17 00:00:00 2001 From: cflmy Date: Thu, 22 Oct 2026 15:51:26 +0800 Subject: [PATCH] =?UTF-8?q?refactor(host):=20HostFn=20=E4=BA=94=E5=BC=A0?= =?UTF-8?q?=E8=A1=A8=E5=90=88=E4=B8=80=20=E2=80=94=20HOST=5FFN=5FTABLE=20?= =?UTF-8?q?=E5=8D=95=E4=B8=80=E6=9D=A5=E6=BA=90=EF=BC=88=E2=88=92404=20?= =?UTF-8?q?=E8=A1=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 202 个宿主函数的元数据原本分散在 from_u16 / from_name / name / required_params / optional_params 五个手写 match 里,必须同步维护, 审计已记录过 from_name 与 name() 的漂移。 改动: - 新增 HostFnDef + HOST_FN_TABLE(203 项:id, 变体, 规范名, 必填, 可选) - from_u16 按 id 查表;name / required_params / optional_params 按判别值查表 - call_host 的 202 个一行臂保持不变(只集中元数据,不动分派逻辑) - from_name 保留:它是别名注册表(http_get/中文别名…),语义与规范名不同 等价性证明(快照比对): 1. 先在旧实现导出全部 203 项的 (id,name,required,optional) 到 TSV 2. 合并后用同一段代码再导出 3. diff 两份 → 完全一致;快照测试用后即删 长期守护(4 个不变量测试,上一轮已加): id↔as_u16 往返、name→from_name 往返、规范名唯一、required/optional 不重叠。 验证:cargo build 零警告;cargo test --release 609 passed / 0 failed。 行数:dispatch.rs 2052 → 1648;src 合计 53,040 → 52,668。 文档:doc/design/change-hostfn-table.md --- doc/design/change-hostfn-table.md | 99 ++++ src/host/dispatch.rs | 874 ++++++++---------------------- 2 files changed, 334 insertions(+), 639 deletions(-) create mode 100644 doc/design/change-hostfn-table.md diff --git a/doc/design/change-hostfn-table.md b/doc/design/change-hostfn-table.md new file mode 100644 index 0000000..92e63cb --- /dev/null +++ b/doc/design/change-hostfn-table.md @@ -0,0 +1,99 @@ +# 变更记录:HostFn 五张表合一(`HOST_FN_TABLE` 单一来源) + +| | | +|---|---| +| 状态 | **Implemented** | +| 日期 | 2026-10-22 | +| 来源 | [code-volume-audit.md](code-volume-audit.md) §4 第 4 项 | +| 改动 | `src/host/dispatch.rs`:**−404 行**(639 删 / 235 增) | +| 行为 | **零变化**(203 项逐字节一致) | + +--- + +## 1. 原来有五张并行表 + +202 个宿主函数的元数据分散在五个手写 match 里: + +| 表 | 行数 | 作用 | +|----|------|------| +| `from_u16` | ~215 | id → 变体 | +| `from_name` | ~215 | 名字/别名 → 变体(**保留**,见 §4) | +| `name` | ~210 | 变体 → 规范名 | +| `required_params` | ~145 | 变体 → 必填参数 | +| `optional_params` | ~90 | 变体 → 可选参数 | + +五张表必须同步维护,而 [code-volume-audit.md](code-volume-audit.md) 记录过 +`from_name` 接受 `http_get` 而 `name()` 只输出 `host_http_get` 的一类漂移。 + +## 2. 现在只有一张 + +```rust +pub struct HostFnDef { + pub id: u16, + pub f: HostFn, + pub name: &'static str, + pub required: &'static [&'static str], + pub optional: &'static [&'static str], +} + +pub const HOST_FN_TABLE: &[HostFnDef] = &[ + HostFnDef { id: 1, f: HostFn::ReadText, name: "host_read_text", + required: &["path"], optional: &[] }, + … +]; +``` + +四个函数改为查表(`from_u16` 按 id 查,其余按判别值查); +`call_host` 保留它那 202 个一行臂——**分派逻辑不动**,只把元数据集中。 + +## 3. 怎么保证没改坏 + +迁移采用**快照比对**: + +1. 先在旧实现上导出全部 203 项的 `(id, name, required, optional)` 到 TSV +2. 合并成表后,用同一段代码再导出一份 +3. `diff` 两份文件 → **完全一致** + +``` +$ diff /tmp/host_fns_baseline.tsv /tmp/host_fns_new.tsv && echo IDENTICAL +IDENTICAL +``` + +快照测试是一次性的,比对通过后删除;长期守护交给四个不变量测试: + +| 测试 | 内容 | +|------|------| +| `every_assigned_id_round_trips` | 每个已分配 id → `as_u16()` 往返(id 空间稀疏是**退役 id**,不复用) | +| `every_id_has_a_name_that_round_trips` | `name()` → `from_name()` 回到同一变体 | +| `names_are_unique` | 规范名唯一 | +| `required_and_optional_params_are_disjoint` | 必填/可选不重叠、`all_params` 无重复 | + +## 4. 为什么 `from_name` 没有并进表 + +它是**别名注册表**(`http_get`、`get`、中文别名…),语义与"规范名"不同; +并进表会让 `name()` 与 `from_name` 又变成一对映射。 +现在它保留原样,由上面的往返测试保证不与表漂移。 + +## 5. 收益 + +| 指标 | 前 | 后 | +|------|----|----| +| `dispatch.rs` | 2,052 行 | **1,648 行** | +| `src/` 合计 | 53,040 | **52,668** | +| 需同步维护的表 | 5 | **1** | + +## 6. 验证 + +| 项 | 结果 | +|----|------| +| `cargo build --release` | 零警告 | +| `cargo test --release` | **609 passed / 0 failed** | +| 快照 diff | 203 项逐字节一致 | +| 四个不变量测试 | 全绿 | + +## 7. 变更清单 + +| 文件 | 变更 | +|------|------| +| `src/host/dispatch.rs` | 新增 `HostFnDef` / `HOST_FN_TABLE`(203 项);`from_u16` / `name` / `required_params` / `optional_params` 改为查表;删除对应四个 match | +| `doc/design/change-hostfn-table.md` | 本文件 | diff --git a/src/host/dispatch.rs b/src/host/dispatch.rs index 3e3c418..d763cb0 100644 --- a/src/host/dispatch.rs +++ b/src/host/dispatch.rs @@ -222,215 +222,228 @@ pub enum HostFn { Access = 212, } +/// Single source of truth for every host function. `` slimming +/// +/// Replaces five hand-written matches (id, name, required, optional) that +/// had already drifted; `call_host` keeps its one-line arms. +pub struct HostFnDef { + pub id: u16, + pub f: HostFn, + pub name: &'static str, + pub required: &'static [&'static str], + pub optional: &'static [&'static str], +} + +pub const HOST_FN_TABLE: &[HostFnDef] = &[ + HostFnDef { id: 1, f: HostFn::ReadText, name: "host_read_text", required: &["path"], optional: &[] }, + HostFnDef { id: 2, f: HostFn::WriteText, name: "host_write_text", required: &["path", "text"], optional: &[] }, + HostFnDef { id: 3, f: HostFn::AppendText, name: "host_append_text", required: &["path", "text"], optional: &[] }, + HostFnDef { id: 4, f: HostFn::Exists, name: "host_exists", required: &["path"], optional: &[] }, + HostFnDef { id: 5, f: HostFn::ListDir, name: "host_list_dir", required: &["path"], optional: &[] }, + HostFnDef { id: 6, f: HostFn::MakeDir, name: "host_make_dir", required: &["path"], optional: &[] }, + HostFnDef { id: 7, f: HostFn::Remove, name: "host_remove", required: &["path"], optional: &[] }, + HostFnDef { id: 8, f: HostFn::NowUnix, name: "host_now_unix", required: &[], optional: &[] }, + HostFnDef { id: 9, f: HostFn::NowMs, name: "host_now_ms", required: &[], optional: &[] }, + HostFnDef { id: 10, f: HostFn::FormatTime, name: "host_format_time", required: &["unix", "pattern"], optional: &[] }, + HostFnDef { id: 11, f: HostFn::SleepMs, name: "host_sleep_ms", required: &["ms"], optional: &[] }, + HostFnDef { id: 12, f: HostFn::ParseTime, name: "host_parse_time", required: &["text", "pattern"], optional: &[] }, + HostFnDef { id: 13, f: HostFn::EnvGet, name: "host_env_get", required: &["name"], optional: &[] }, + HostFnDef { id: 14, f: HostFn::EnvSet, name: "host_env_set", required: &["name", "value"], optional: &[] }, + HostFnDef { id: 15, f: HostFn::Args, name: "host_args", required: &[], optional: &[] }, + HostFnDef { id: 16, f: HostFn::Cwd, name: "host_cwd", required: &[], optional: &[] }, + HostFnDef { id: 17, f: HostFn::Exit, name: "host_exit", required: &["code"], optional: &[] }, + HostFnDef { id: 18, f: HostFn::Exec, name: "host_exec", required: &["cmd"], optional: &["args", "capture"] }, + HostFnDef { id: 19, f: HostFn::JsonParse, name: "host_json_parse", required: &["text"], optional: &[] }, + HostFnDef { id: 20, f: HostFn::JsonStringify, name: "host_json_stringify", required: &["value"], optional: &["indent"] }, + HostFnDef { id: 21, f: HostFn::JsonGet, name: "host_json_get", required: &["value", "key"], optional: &[] }, + HostFnDef { id: 22, f: HostFn::JsonKeys, name: "host_json_keys", required: &["value"], optional: &[] }, + HostFnDef { id: 23, f: HostFn::HttpGet, name: "host_http_get", required: &["url"], optional: &["headers"] }, + HostFnDef { id: 24, f: HostFn::HttpPost, name: "host_http_post", required: &["url", "body"], optional: &["content_type", "headers"] }, + HostFnDef { id: 25, f: HostFn::UrlEncode, name: "host_url_encode", required: &["text"], optional: &[] }, + HostFnDef { id: 26, f: HostFn::Pi, name: "host_pi", required: &[], optional: &[] }, + HostFnDef { id: 27, f: HostFn::EConst, name: "host_e", required: &[], optional: &[] }, + HostFnDef { id: 28, f: HostFn::Num, name: "host_num", required: &["value"], optional: &[] }, + HostFnDef { id: 29, f: HostFn::MathAdd, name: "host_math_add", required: &["a", "b"], optional: &[] }, + HostFnDef { id: 30, f: HostFn::MathSub, name: "host_math_sub", required: &["a", "b"], optional: &[] }, + HostFnDef { id: 31, f: HostFn::MathMul, name: "host_math_mul", required: &["a", "b"], optional: &[] }, + HostFnDef { id: 32, f: HostFn::MathDiv, name: "host_math_div", required: &["a", "b"], optional: &[] }, + HostFnDef { id: 33, f: HostFn::MathPow, name: "host_math_pow", required: &["a", "b"], optional: &[] }, + HostFnDef { id: 34, f: HostFn::MathNeg, name: "host_math_neg", required: &["value"], optional: &[] }, + HostFnDef { id: 35, f: HostFn::MathSin, name: "host_math_sin", required: &["value"], optional: &[] }, + HostFnDef { id: 36, f: HostFn::MathCos, name: "host_math_cos", required: &["value"], optional: &[] }, + HostFnDef { id: 37, f: HostFn::MathTan, name: "host_math_tan", required: &["value"], optional: &[] }, + HostFnDef { id: 38, f: HostFn::MathAsin, name: "host_math_asin", required: &["value"], optional: &[] }, + HostFnDef { id: 39, f: HostFn::MathAcos, name: "host_math_acos", required: &["value"], optional: &[] }, + HostFnDef { id: 40, f: HostFn::MathAtan, name: "host_math_atan", required: &["value"], optional: &[] }, + HostFnDef { id: 41, f: HostFn::MathSqrt, name: "host_math_sqrt", required: &["value"], optional: &[] }, + HostFnDef { id: 42, f: HostFn::MathAbs, name: "host_math_abs", required: &["value"], optional: &[] }, + HostFnDef { id: 43, f: HostFn::MathLn, name: "host_math_ln", required: &["value"], optional: &[] }, + HostFnDef { id: 44, f: HostFn::MathExp, name: "host_math_exp", required: &["value"], optional: &[] }, + HostFnDef { id: 45, f: HostFn::MathFloor, name: "host_math_floor", required: &["value"], optional: &[] }, + HostFnDef { id: 46, f: HostFn::MathCeil, name: "host_math_ceil", required: &["value"], optional: &[] }, + HostFnDef { id: 47, f: HostFn::MathMin, name: "host_math_min", required: &["a", "b"], optional: &[] }, + HostFnDef { id: 48, f: HostFn::MathMax, name: "host_math_max", required: &["a", "b"], optional: &[] }, + HostFnDef { id: 49, f: HostFn::Random, name: "host_random", required: &[], optional: &[] }, + HostFnDef { id: 50, f: HostFn::RandomInt, name: "host_random_int", required: &["min", "max"], optional: &[] }, + HostFnDef { id: 51, f: HostFn::Seed, name: "host_seed", required: &["value"], optional: &[] }, + HostFnDef { id: 52, f: HostFn::Formula, name: "host_formula", required: &["value"], optional: &[] }, + HostFnDef { id: 53, f: HostFn::Simplify, name: "host_simplify", required: &["value"], optional: &[] }, + HostFnDef { id: 54, f: HostFn::Expand, name: "host_expand", required: &["value"], optional: &[] }, + HostFnDef { id: 55, f: HostFn::Diff, name: "host_diff", required: &["formula", "var"], optional: &[] }, + HostFnDef { id: 56, f: HostFn::Subs, name: "host_subs", required: &["formula", "var", "value"], optional: &[] }, + HostFnDef { id: 57, f: HostFn::Eval, name: "host_eval", required: &["formula", "var", "value"], optional: &[] }, + HostFnDef { id: 58, f: HostFn::Solve, name: "host_solve", required: &["formula", "var"], optional: &["min", "max"] }, + HostFnDef { id: 59, f: HostFn::Plot, name: "host_plot", required: &["formula", "var", "min", "max"], optional: &["steps", "path", "derivative", "grid"] }, + HostFnDef { id: 60, f: HostFn::PlotPoints, name: "host_plot_points", required: &["xs", "ys"], optional: &["path", "grid"] }, + HostFnDef { id: 61, f: HostFn::PlotConic, name: "host_plot_conic", required: &["kind", "a"], optional: &["b", "h", "k", "path", "grid"] }, + HostFnDef { id: 62, f: HostFn::ForeignSetCmd, name: "host_foreign_set_cmd", required: &["lang", "cmd"], optional: &["args"] }, + HostFnDef { id: 63, f: HostFn::ForeignRun, name: "host_foreign_run", required: &["code"], optional: &["stdin", "env", "cwd", "timeout", "args", "max_output"] }, + HostFnDef { id: 64, f: HostFn::ForeignRunLang, name: "host_foreign_run_lang", required: &["lang", "source"], optional: &["stdin", "env", "cwd", "timeout", "args", "max_output"] }, + HostFnDef { id: 65, f: HostFn::ForeignLangs, name: "host_foreign_langs", required: &[], optional: &[] }, + HostFnDef { id: 66, f: HostFn::DotenvLoad, name: "host_dotenv_load", required: &[], optional: &["path"] }, + HostFnDef { id: 67, f: HostFn::HttpRequest, name: "host_http_request", required: &["method", "url"], optional: &["body", "content_type", "headers"] }, + HostFnDef { id: 68, f: HostFn::JsonQuote, name: "host_json_quote", required: &["text"], optional: &[] }, + HostFnDef { id: 69, f: HostFn::PluginLoad, name: "host_plugin_load", required: &["path"], optional: &[] }, + HostFnDef { id: 70, f: HostFn::PluginUnload, name: "host_plugin_unload", required: &[], optional: &[] }, + HostFnDef { id: 71, f: HostFn::PluginList, name: "host_plugin_list", required: &[], optional: &[] }, + HostFnDef { id: 72, f: HostFn::ExtNativePath, name: "host_ext_native_path", required: &["name"], optional: &[] }, + HostFnDef { id: 76, f: HostFn::MapSet, name: "host_map_set", required: &["map", "key", "value"], optional: &[] }, + HostFnDef { id: 77, f: HostFn::ListAppend, name: "host_list_append", required: &["list", "item"], optional: &[] }, + HostFnDef { id: 84, f: HostFn::WritebackRecord, name: "host_writeback_record", required: &["value"], optional: &["at_end", "key", "line"] }, + HostFnDef { id: 85, f: HostFn::WritebackGet, name: "host_writeback_get", required: &[], optional: &["at_end", "key", "line"] }, + HostFnDef { id: 86, f: HostFn::WritebackClear, name: "host_writeback_clear", required: &[], optional: &["at_end", "key", "line"] }, + HostFnDef { id: 87, f: HostFn::WritebackList, name: "host_writeback_list", required: &[], optional: &[] }, + HostFnDef { id: 88, f: HostFn::SubtaskSpawn, name: "host_subtask_spawn", required: &[], optional: &["path", "fn", "args", "code", "lang", "source", "stdin", "quiet"] }, + HostFnDef { id: 89, f: HostFn::SubtaskPoll, name: "host_subtask_poll", required: &["id"], optional: &[] }, + HostFnDef { id: 90, f: HostFn::SubtaskJoin, name: "host_subtask_join", required: &["id"], optional: &[] }, + HostFnDef { id: 91, f: HostFn::SubtaskKill, name: "host_subtask_kill", required: &["id"], optional: &[] }, + HostFnDef { id: 92, f: HostFn::SubtaskWaitAll, name: "host_subtask_wait_all", required: &[], optional: &[] }, + HostFnDef { id: 93, f: HostFn::WritebackEnsure, name: "host_writeback_ensure", required: &["key"], optional: &["placeholder", "line"] }, + HostFnDef { id: 94, f: HostFn::OuterCallLine, name: "host_outer_call_line", required: &[], optional: &[] }, + HostFnDef { id: 95, f: HostFn::TextPatch, name: "host_text_patch", required: &["path", "find", "replace"], optional: &[] }, + HostFnDef { id: 96, f: HostFn::ApplyPatchBlocks, name: "host_apply_patch_blocks", required: &["path", "text", "soft"], optional: &[] }, + HostFnDef { id: 97, f: HostFn::WritebackScanPath, name: "host_writeback_scan_path", required: &["path"], optional: &[] }, + HostFnDef { id: 98, f: HostFn::HttpPostSse, name: "host_http_post_sse", required: &["url", "body"], optional: &["content_type", "headers", "echo"] }, + HostFnDef { id: 99, f: HostFn::OpenAiSseParse, name: "host_openai_sse_parse", required: &["text"], optional: &["echo"] }, + HostFnDef { id: 100, f: HostFn::StreamPublish, name: "host_stream_publish", required: &["event"], optional: &[] }, + HostFnDef { id: 101, f: HostFn::CollectionPut, name: "host_collection_put", required: &["in", "at", "value"], optional: &[] }, + HostFnDef { id: 102, f: HostFn::ListPrepend, name: "host_list_prepend", required: &["list", "item"], optional: &[] }, + HostFnDef { id: 103, f: HostFn::ListConcat, name: "host_list_concat", required: &["a", "b"], optional: &[] }, + HostFnDef { id: 104, f: HostFn::ListInsert, name: "host_list_insert", required: &["list", "index", "item"], optional: &[] }, + HostFnDef { id: 105, f: HostFn::ListSetAt, name: "host_list_set_at", required: &["list", "index", "item"], optional: &[] }, + HostFnDef { id: 106, f: HostFn::ListRemoveAt, name: "host_list_remove_at", required: &["list", "index"], optional: &[] }, + HostFnDef { id: 107, f: HostFn::ListSlice, name: "host_list_slice", required: &["list", "start"], optional: &["end"] }, + HostFnDef { id: 108, f: HostFn::ListContains, name: "host_list_contains", required: &["list", "item"], optional: &[] }, + HostFnDef { id: 109, f: HostFn::ListIndexOf, name: "host_list_index_of", required: &["list", "item"], optional: &[] }, + HostFnDef { id: 110, f: HostFn::ListReverse, name: "host_list_reverse", required: &["list"], optional: &[] }, + HostFnDef { id: 111, f: HostFn::ListFirst, name: "host_list_first", required: &["list"], optional: &[] }, + HostFnDef { id: 112, f: HostFn::ListLast, name: "host_list_last", required: &["list"], optional: &[] }, + HostFnDef { id: 113, f: HostFn::MapGet, name: "host_map_get", required: &["map", "key"], optional: &[] }, + HostFnDef { id: 114, f: HostFn::MapDelete, name: "host_map_delete", required: &["map", "key"], optional: &[] }, + HostFnDef { id: 115, f: HostFn::MapHas, name: "host_map_has", required: &["map", "key"], optional: &[] }, + HostFnDef { id: 116, f: HostFn::MapKeys, name: "host_map_keys", required: &["map"], optional: &[] }, + HostFnDef { id: 117, f: HostFn::MapValues, name: "host_map_values", required: &["map"], optional: &[] }, + HostFnDef { id: 118, f: HostFn::MapItems, name: "host_map_items", required: &["map"], optional: &[] }, + HostFnDef { id: 119, f: HostFn::MapMerge, name: "host_map_merge", required: &["a", "b"], optional: &[] }, + HostFnDef { id: 120, f: HostFn::MapSize, name: "host_map_size", required: &["map"], optional: &[] }, + HostFnDef { id: 121, f: HostFn::CollectionClear, name: "host_collection_clear", required: &["value"], optional: &[] }, + HostFnDef { id: 122, f: HostFn::CookieParse, name: "host_cookie_parse", required: &["text", "is_response"], optional: &[] }, + HostFnDef { id: 123, f: HostFn::MultipartParse, name: "host_multipart_parse", required: &["body", "boundary"], optional: &[] }, + HostFnDef { id: 124, f: HostFn::MarkdownParse, name: "host_markdown_parse", required: &["text"], optional: &[] }, + HostFnDef { id: 125, f: HostFn::ReIsMatch, name: "host_re_is_match", required: &["text", "pattern"], optional: &[] }, + HostFnDef { id: 126, f: HostFn::ReFind, name: "host_re_find", required: &["text", "pattern"], optional: &[] }, + HostFnDef { id: 127, f: HostFn::ReFindAll, name: "host_re_find_all", required: &["text", "pattern"], optional: &[] }, + HostFnDef { id: 128, f: HostFn::ReReplace, name: "host_re_replace", required: &["text", "pattern", "with"], optional: &["count"] }, + HostFnDef { id: 129, f: HostFn::ReSplit, name: "host_re_split", required: &["text", "pattern"], optional: &[] }, + HostFnDef { id: 130, f: HostFn::EncodingBase64Encode, name: "host_encoding_base64_encode", required: &["text"], optional: &[] }, + HostFnDef { id: 131, f: HostFn::EncodingBase64Decode, name: "host_encoding_base64_decode", required: &["text"], optional: &[] }, + HostFnDef { id: 132, f: HostFn::EncodingHexEncode, name: "host_encoding_hex_encode", required: &["text"], optional: &[] }, + HostFnDef { id: 133, f: HostFn::EncodingHexDecode, name: "host_encoding_hex_decode", required: &["text"], optional: &[] }, + HostFnDef { id: 134, f: HostFn::PathJoin, name: "host_path_join", required: &["a", "b"], optional: &[] }, + HostFnDef { id: 135, f: HostFn::PathSplit, name: "host_path_split", required: &["path"], optional: &[] }, + HostFnDef { id: 136, f: HostFn::PathFileName, name: "host_path_file_name", required: &["path"], optional: &[] }, + HostFnDef { id: 137, f: HostFn::PathParent, name: "host_path_parent", required: &["path"], optional: &[] }, + HostFnDef { id: 138, f: HostFn::PathExtension, name: "host_path_extension", required: &["path"], optional: &[] }, + HostFnDef { id: 139, f: HostFn::PathNormalize, name: "host_path_normalize", required: &["path"], optional: &[] }, + HostFnDef { id: 140, f: HostFn::PathIsAbsolute, name: "host_path_is_absolute", required: &["path"], optional: &[] }, + HostFnDef { id: 141, f: HostFn::FsCopyFile, name: "host_copy_file", required: &["src", "dest"], optional: &[] }, + HostFnDef { id: 142, f: HostFn::FsMove, name: "host_move", required: &["src", "dest"], optional: &[] }, + HostFnDef { id: 143, f: HostFn::FsMakeTemp, name: "host_make_temp", required: &[], optional: &["prefix"] }, + HostFnDef { id: 144, f: HostFn::HashSha256, name: "host_hash_sha256", required: &["text"], optional: &[] }, + HostFnDef { id: 145, f: HostFn::HashSha1, name: "host_hash_sha1", required: &["text"], optional: &[] }, + HostFnDef { id: 146, f: HostFn::HashMd5, name: "host_hash_md5", required: &["text"], optional: &[] }, + HostFnDef { id: 147, f: HostFn::HashHmacSha256, name: "host_hash_hmac_sha256", required: &["key", "text"], optional: &[] }, + HostFnDef { id: 148, f: HostFn::SecretsTokenHex, name: "host_secrets_token_hex", required: &[], optional: &["n"] }, + HostFnDef { id: 149, f: HostFn::SecretsTokenUrlsafe, name: "host_secrets_token_urlsafe", required: &[], optional: &["n"] }, + HostFnDef { id: 150, f: HostFn::TextContains, name: "host_text_contains", required: &["text", "sub"], optional: &[] }, + HostFnDef { id: 151, f: HostFn::TextStartsWith, name: "host_text_starts_with", required: &["text", "prefix"], optional: &[] }, + HostFnDef { id: 152, f: HostFn::TextEndsWith, name: "host_text_ends_with", required: &["text", "suffix"], optional: &[] }, + HostFnDef { id: 153, f: HostFn::TextReplace, name: "host_text_replace", required: &["text", "old", "new"], optional: &["count"] }, + HostFnDef { id: 154, f: HostFn::TextToUpper, name: "host_text_to_upper", required: &["text"], optional: &[] }, + HostFnDef { id: 155, f: HostFn::TextToLower, name: "host_text_to_lower", required: &["text"], optional: &[] }, + HostFnDef { id: 156, f: HostFn::TextRepeat, name: "host_text_repeat", required: &["text", "n"], optional: &[] }, + HostFnDef { id: 157, f: HostFn::TextPad, name: "host_text_pad", required: &["text", "width"], optional: &["fill", "align"] }, + HostFnDef { id: 158, f: HostFn::CsvParse, name: "host_csv_parse", required: &["text"], optional: &[] }, + HostFnDef { id: 159, f: HostFn::CsvStringify, name: "host_csv_stringify", required: &["rows"], optional: &[] }, + HostFnDef { id: 160, f: HostFn::ListSort, name: "host_list_sort", required: &["list"], optional: &[] }, + HostFnDef { id: 161, f: HostFn::ListSortBy, name: "host_list_sort_by", required: &["list", "key"], optional: &[] }, + HostFnDef { id: 162, f: HostFn::ListUnique, name: "host_list_unique", required: &["list"], optional: &[] }, + HostFnDef { id: 163, f: HostFn::ListChunk, name: "host_list_chunk", required: &["list", "size"], optional: &[] }, + HostFnDef { id: 164, f: HostFn::ListZip, name: "host_list_zip", required: &["a", "b"], optional: &[] }, + HostFnDef { id: 165, f: HostFn::ListFlatten, name: "host_list_flatten", required: &["list"], optional: &[] }, + HostFnDef { id: 166, f: HostFn::CliParse, name: "host_cli_parse", required: &[], optional: &["args"] }, + HostFnDef { id: 167, f: HostFn::LogSetLevel, name: "host_log_set_level", required: &["level"], optional: &[] }, + HostFnDef { id: 168, f: HostFn::LogLine, name: "host_log_line", required: &["level", "text"], optional: &["fields"] }, + HostFnDef { id: 169, f: HostFn::UuidV4, name: "host_uuid_v4", required: &[], optional: &[] }, + HostFnDef { id: 170, f: HostFn::DatetimeNow, name: "host_datetime_now", required: &[], optional: &[] }, + HostFnDef { id: 171, f: HostFn::DatetimeFromUnix, name: "host_datetime_from_unix", required: &["unix"], optional: &["zone"] }, + HostFnDef { id: 172, f: HostFn::DatetimeToUnix, name: "host_datetime_to_unix", required: &["dt"], optional: &[] }, + HostFnDef { id: 173, f: HostFn::DatetimeParse, name: "host_datetime_parse", required: &["text"], optional: &["pattern"] }, + HostFnDef { id: 174, f: HostFn::DatetimeFormat, name: "host_datetime_format", required: &["dt"], optional: &["style"] }, + HostFnDef { id: 175, f: HostFn::DatetimeAdd, name: "host_datetime_add", required: &["dt"], optional: &["days", "hours", "minutes", "seconds"] }, + HostFnDef { id: 176, f: HostFn::DatetimeInZone, name: "host_datetime_in_zone", required: &["dt", "zone"], optional: &[] }, + HostFnDef { id: 177, f: HostFn::UrlParse, name: "host_url_parse", required: &["text"], optional: &[] }, + HostFnDef { id: 178, f: HostFn::UrlQueryParse, name: "host_url_query_parse", required: &["text"], optional: &[] }, + HostFnDef { id: 179, f: HostFn::UrlQueryStringify, name: "host_url_query_stringify", required: &["map"], optional: &[] }, + HostFnDef { id: 180, f: HostFn::TomlParse, name: "host_toml_parse", required: &["text"], optional: &[] }, + HostFnDef { id: 181, f: HostFn::HtmlEscape, name: "host_html_escape", required: &["text"], optional: &[] }, + HostFnDef { id: 182, f: HostFn::HtmlUnescape, name: "host_html_unescape", required: &["text"], optional: &[] }, + HostFnDef { id: 183, f: HostFn::EncodingBase32Encode, name: "host_encoding_base32_encode", required: &["text"], optional: &[] }, + HostFnDef { id: 184, f: HostFn::EncodingBase32Decode, name: "host_encoding_base32_decode", required: &["text"], optional: &[] }, + HostFnDef { id: 185, f: HostFn::FsMakeDirs, name: "host_make_dirs", required: &["path"], optional: &[] }, + HostFnDef { id: 186, f: HostFn::FsRemoveTree, name: "host_remove_tree", required: &["path"], optional: &[] }, + HostFnDef { id: 187, f: HostFn::FsStat, name: "host_stat", required: &["path"], optional: &[] }, + HostFnDef { id: 188, f: HostFn::FsWalk, name: "host_walk", required: &["path"], optional: &[] }, + HostFnDef { id: 189, f: HostFn::StatsMean, name: "host_stats_mean", required: &["list"], optional: &[] }, + HostFnDef { id: 190, f: HostFn::StatsMedian, name: "host_stats_median", required: &["list"], optional: &[] }, + HostFnDef { id: 191, f: HostFn::StatsStdev, name: "host_stats_stdev", required: &["list"], optional: &[] }, + HostFnDef { id: 192, f: HostFn::Meta, name: "host_meta", required: &[], optional: &[] }, + HostFnDef { id: 193, f: HostFn::MetaGet, name: "host_meta_get", required: &["key"], optional: &[] }, + HostFnDef { id: 194, f: HostFn::ModuleSource, name: "host_module_source", required: &[], optional: &[] }, + HostFnDef { id: 195, f: HostFn::ArtifactKind, name: "host_artifact_kind", required: &[], optional: &[] }, + HostFnDef { id: 196, f: HostFn::ForeignInvoke, name: "host_foreign_invoke", required: &[], optional: &["code", "lang", "source", "stdin", "input", "env", "cwd", "timeout", "args", "output", "max_output", "cmd", "keep"] }, + HostFnDef { id: 197, f: HostFn::ForeignProbe, name: "host_foreign_probe", required: &[], optional: &["lang"] }, + HostFnDef { id: 198, f: HostFn::ForeignRegister, name: "host_foreign_register", required: &["lang"], optional: &["cmd", "args", "template", "ext", "env", "version_args"] }, + HostFnDef { id: 199, f: HostFn::ForeignUnset, name: "host_foreign_unset", required: &["lang"], optional: &[] }, + HostFnDef { id: 200, f: HostFn::ForeignLoadConfig, name: "host_foreign_load_config", required: &[], optional: &["path"] }, + HostFnDef { id: 201, f: HostFn::ForeignCall, name: "host_foreign_call", required: &[], optional: &["code", "lang", "source", "input", "stdin", "env", "cwd", "timeout", "args"] }, + HostFnDef { id: 202, f: HostFn::ForeignRunMany, name: "host_foreign_run_many", required: &["runs"], optional: &["concurrency"] }, + HostFnDef { id: 203, f: HostFn::ForeignCallMany, name: "host_foreign_call_many", required: &["inputs"], optional: &["code", "lang", "source", "cmd", "env", "cwd", "timeout", "args", "max_output", "concurrency"] }, + HostFnDef { id: 204, f: HostFn::ForeignSessionOpen, name: "host_foreign_session_open", required: &["lang"], optional: &["cmd", "env", "cwd", "timeout"] }, + HostFnDef { id: 205, f: HostFn::ForeignSessionEval, name: "host_foreign_session_eval", required: &["id"], optional: &["code", "source", "input", "stdin", "timeout"] }, + HostFnDef { id: 206, f: HostFn::ForeignSessionSet, name: "host_foreign_session_set", required: &["id", "name", "value"], optional: &[] }, + HostFnDef { id: 207, f: HostFn::ForeignSessionGet, name: "host_foreign_session_get", required: &["id", "name"], optional: &[] }, + HostFnDef { id: 208, f: HostFn::ForeignSessionNames, name: "host_foreign_session_names", required: &["id"], optional: &[] }, + HostFnDef { id: 209, f: HostFn::ForeignSessionClose, name: "host_foreign_session_close", required: &["id"], optional: &[] }, + HostFnDef { id: 210, f: HostFn::ForeignSessionList, name: "host_foreign_session_list", required: &[], optional: &[] }, + HostFnDef { id: 211, f: HostFn::ForeignSessionInterrupt, name: "host_foreign_session_interrupt", required: &["id"], optional: &[] }, + HostFnDef { id: 212, f: HostFn::Access, name: "host_access", required: &["value", "key"], optional: &[] }, +]; + impl HostFn { pub fn from_u16(id: u16) -> Option { - Some(match id { - 1 => Self::ReadText, - 2 => Self::WriteText, - 3 => Self::AppendText, - 4 => Self::Exists, - 5 => Self::ListDir, - 6 => Self::MakeDir, - 7 => Self::Remove, - 8 => Self::NowUnix, - 9 => Self::NowMs, - 10 => Self::FormatTime, - 11 => Self::SleepMs, - 12 => Self::ParseTime, - 13 => Self::EnvGet, - 14 => Self::EnvSet, - 15 => Self::Args, - 16 => Self::Cwd, - 17 => Self::Exit, - 18 => Self::Exec, - 19 => Self::JsonParse, - 20 => Self::JsonStringify, - 21 => Self::JsonGet, - 22 => Self::JsonKeys, - 23 => Self::HttpGet, - 24 => Self::HttpPost, - 25 => Self::UrlEncode, - 26 => Self::Pi, - 27 => Self::EConst, - 28 => Self::Num, - 29 => Self::MathAdd, - 30 => Self::MathSub, - 31 => Self::MathMul, - 32 => Self::MathDiv, - 33 => Self::MathPow, - 34 => Self::MathNeg, - 35 => Self::MathSin, - 36 => Self::MathCos, - 37 => Self::MathTan, - 38 => Self::MathAsin, - 39 => Self::MathAcos, - 40 => Self::MathAtan, - 41 => Self::MathSqrt, - 42 => Self::MathAbs, - 43 => Self::MathLn, - 44 => Self::MathExp, - 45 => Self::MathFloor, - 46 => Self::MathCeil, - 47 => Self::MathMin, - 48 => Self::MathMax, - 49 => Self::Random, - 50 => Self::RandomInt, - 51 => Self::Seed, - 52 => Self::Formula, - 53 => Self::Simplify, - 54 => Self::Expand, - 55 => Self::Diff, - 56 => Self::Subs, - 57 => Self::Eval, - 58 => Self::Solve, - 59 => Self::Plot, - 60 => Self::PlotPoints, - 61 => Self::PlotConic, - 62 => Self::ForeignSetCmd, - 63 => Self::ForeignRun, - 64 => Self::ForeignRunLang, - 65 => Self::ForeignLangs, - 66 => Self::DotenvLoad, - 67 => Self::HttpRequest, - 68 => Self::JsonQuote, - 69 => Self::PluginLoad, - 70 => Self::PluginUnload, - 71 => Self::PluginList, - 72 => Self::ExtNativePath, - 76 => Self::MapSet, - 77 => Self::ListAppend, - 84 => Self::WritebackRecord, - 85 => Self::WritebackGet, - 86 => Self::WritebackClear, - 87 => Self::WritebackList, - 88 => Self::SubtaskSpawn, - 89 => Self::SubtaskPoll, - 90 => Self::SubtaskJoin, - 91 => Self::SubtaskKill, - 92 => Self::SubtaskWaitAll, - 93 => Self::WritebackEnsure, - 94 => Self::OuterCallLine, - 95 => Self::TextPatch, - 96 => Self::ApplyPatchBlocks, - 97 => Self::WritebackScanPath, - 98 => Self::HttpPostSse, - 99 => Self::OpenAiSseParse, - 100 => Self::StreamPublish, - 101 => Self::CollectionPut, - 102 => Self::ListPrepend, - 103 => Self::ListConcat, - 104 => Self::ListInsert, - 105 => Self::ListSetAt, - 106 => Self::ListRemoveAt, - 107 => Self::ListSlice, - 108 => Self::ListContains, - 109 => Self::ListIndexOf, - 110 => Self::ListReverse, - 111 => Self::ListFirst, - 112 => Self::ListLast, - 113 => Self::MapGet, - 114 => Self::MapDelete, - 115 => Self::MapHas, - 116 => Self::MapKeys, - 117 => Self::MapValues, - 118 => Self::MapItems, - 119 => Self::MapMerge, - 120 => Self::MapSize, - 121 => Self::CollectionClear, - 122 => Self::CookieParse, - 123 => Self::MultipartParse, - 124 => Self::MarkdownParse, - 125 => Self::ReIsMatch, - 126 => Self::ReFind, - 127 => Self::ReFindAll, - 128 => Self::ReReplace, - 129 => Self::ReSplit, - 130 => Self::EncodingBase64Encode, - 131 => Self::EncodingBase64Decode, - 132 => Self::EncodingHexEncode, - 133 => Self::EncodingHexDecode, - 134 => Self::PathJoin, - 135 => Self::PathSplit, - 136 => Self::PathFileName, - 137 => Self::PathParent, - 138 => Self::PathExtension, - 139 => Self::PathNormalize, - 140 => Self::PathIsAbsolute, - 141 => Self::FsCopyFile, - 142 => Self::FsMove, - 143 => Self::FsMakeTemp, - 144 => Self::HashSha256, - 145 => Self::HashSha1, - 146 => Self::HashMd5, - 147 => Self::HashHmacSha256, - 148 => Self::SecretsTokenHex, - 149 => Self::SecretsTokenUrlsafe, - 150 => Self::TextContains, - 151 => Self::TextStartsWith, - 152 => Self::TextEndsWith, - 153 => Self::TextReplace, - 154 => Self::TextToUpper, - 155 => Self::TextToLower, - 156 => Self::TextRepeat, - 157 => Self::TextPad, - 158 => Self::CsvParse, - 159 => Self::CsvStringify, - 160 => Self::ListSort, - 161 => Self::ListSortBy, - 162 => Self::ListUnique, - 163 => Self::ListChunk, - 164 => Self::ListZip, - 165 => Self::ListFlatten, - 166 => Self::CliParse, - 167 => Self::LogSetLevel, - 168 => Self::LogLine, - 169 => Self::UuidV4, - 170 => Self::DatetimeNow, - 171 => Self::DatetimeFromUnix, - 172 => Self::DatetimeToUnix, - 173 => Self::DatetimeParse, - 174 => Self::DatetimeFormat, - 175 => Self::DatetimeAdd, - 176 => Self::DatetimeInZone, - 177 => Self::UrlParse, - 178 => Self::UrlQueryParse, - 179 => Self::UrlQueryStringify, - 180 => Self::TomlParse, - 181 => Self::HtmlEscape, - 182 => Self::HtmlUnescape, - 183 => Self::EncodingBase32Encode, - 184 => Self::EncodingBase32Decode, - 185 => Self::FsMakeDirs, - 186 => Self::FsRemoveTree, - 187 => Self::FsStat, - 188 => Self::FsWalk, - 189 => Self::StatsMean, - 190 => Self::StatsMedian, - 191 => Self::StatsStdev, - 192 => Self::Meta, - 193 => Self::MetaGet, - 194 => Self::ModuleSource, - 195 => Self::ArtifactKind, - 196 => Self::ForeignInvoke, - 197 => Self::ForeignProbe, - 198 => Self::ForeignRegister, - 199 => Self::ForeignUnset, - 200 => Self::ForeignLoadConfig, - 201 => Self::ForeignCall, - 202 => Self::ForeignRunMany, - 203 => Self::ForeignCallMany, - 204 => Self::ForeignSessionOpen, - 205 => Self::ForeignSessionEval, - 206 => Self::ForeignSessionSet, - 207 => Self::ForeignSessionGet, - 208 => Self::ForeignSessionNames, - 209 => Self::ForeignSessionClose, - 210 => Self::ForeignSessionList, - 211 => Self::ForeignSessionInterrupt, - 212 => Self::Access, - _ => return None, - }) + HOST_FN_TABLE.iter().find(|d| d.id == id).map(|d| d.f) } pub fn from_name(name: &str) -> Option { @@ -650,445 +663,27 @@ impl HostFn { } pub fn name(self) -> &'static str { - match self { - Self::Access => "host_access", - Self::ReadText => "host_read_text", - Self::WriteText => "host_write_text", - Self::AppendText => "host_append_text", - Self::Exists => "host_exists", - Self::ListDir => "host_list_dir", - Self::MakeDir => "host_make_dir", - Self::Remove => "host_remove", - Self::NowUnix => "host_now_unix", - Self::NowMs => "host_now_ms", - Self::FormatTime => "host_format_time", - Self::SleepMs => "host_sleep_ms", - Self::ParseTime => "host_parse_time", - Self::EnvGet => "host_env_get", - Self::EnvSet => "host_env_set", - Self::Args => "host_args", - Self::Cwd => "host_cwd", - Self::Exit => "host_exit", - Self::Exec => "host_exec", - Self::JsonParse => "host_json_parse", - Self::JsonStringify => "host_json_stringify", - Self::JsonGet => "host_json_get", - Self::JsonKeys => "host_json_keys", - Self::HttpGet => "host_http_get", - Self::HttpPost => "host_http_post", - Self::HttpPostSse => "host_http_post_sse", - Self::OpenAiSseParse => "host_openai_sse_parse", - Self::StreamPublish => "host_stream_publish", - Self::UrlEncode => "host_url_encode", - Self::Pi => "host_pi", - Self::EConst => "host_e", - Self::Num => "host_num", - Self::MathAdd => "host_math_add", - Self::MathSub => "host_math_sub", - Self::MathMul => "host_math_mul", - Self::MathDiv => "host_math_div", - Self::MathPow => "host_math_pow", - Self::MathNeg => "host_math_neg", - Self::MathSin => "host_math_sin", - Self::MathCos => "host_math_cos", - Self::MathTan => "host_math_tan", - Self::MathAsin => "host_math_asin", - Self::MathAcos => "host_math_acos", - Self::MathAtan => "host_math_atan", - Self::MathSqrt => "host_math_sqrt", - Self::MathAbs => "host_math_abs", - Self::MathLn => "host_math_ln", - Self::MathExp => "host_math_exp", - Self::MathFloor => "host_math_floor", - Self::MathCeil => "host_math_ceil", - Self::MathMin => "host_math_min", - Self::MathMax => "host_math_max", - Self::Random => "host_random", - Self::RandomInt => "host_random_int", - Self::Seed => "host_seed", - Self::Formula => "host_formula", - Self::Simplify => "host_simplify", - Self::Expand => "host_expand", - Self::Diff => "host_diff", - Self::Subs => "host_subs", - Self::Eval => "host_eval", - Self::Solve => "host_solve", - Self::Plot => "host_plot", - Self::PlotPoints => "host_plot_points", - Self::PlotConic => "host_plot_conic", - Self::ForeignSetCmd => "host_foreign_set_cmd", - Self::ForeignRun => "host_foreign_run", - Self::ForeignRunLang => "host_foreign_run_lang", - Self::ForeignInvoke => "host_foreign_invoke", - Self::ForeignProbe => "host_foreign_probe", - Self::ForeignRegister => "host_foreign_register", - Self::ForeignUnset => "host_foreign_unset", - Self::ForeignLoadConfig => "host_foreign_load_config", - Self::ForeignCall => "host_foreign_call", - Self::ForeignRunMany => "host_foreign_run_many", - Self::ForeignCallMany => "host_foreign_call_many", - Self::ForeignSessionOpen => "host_foreign_session_open", - Self::ForeignSessionEval => "host_foreign_session_eval", - Self::ForeignSessionSet => "host_foreign_session_set", - Self::ForeignSessionGet => "host_foreign_session_get", - Self::ForeignSessionNames => "host_foreign_session_names", - Self::ForeignSessionClose => "host_foreign_session_close", - Self::ForeignSessionList => "host_foreign_session_list", - Self::ForeignSessionInterrupt => "host_foreign_session_interrupt", - Self::ForeignLangs => "host_foreign_langs", - Self::DotenvLoad => "host_dotenv_load", - Self::HttpRequest => "host_http_request", - Self::JsonQuote => "host_json_quote", - Self::PluginLoad => "host_plugin_load", - Self::PluginUnload => "host_plugin_unload", - Self::PluginList => "host_plugin_list", - Self::ExtNativePath => "host_ext_native_path", - Self::MapSet => "host_map_set", - Self::ListAppend => "host_list_append", - Self::CollectionPut => "host_collection_put", - Self::ListPrepend => "host_list_prepend", - Self::ListConcat => "host_list_concat", - Self::ListInsert => "host_list_insert", - Self::ListSetAt => "host_list_set_at", - Self::ListRemoveAt => "host_list_remove_at", - Self::ListSlice => "host_list_slice", - Self::ListContains => "host_list_contains", - Self::ListIndexOf => "host_list_index_of", - Self::ListReverse => "host_list_reverse", - Self::ListFirst => "host_list_first", - Self::ListLast => "host_list_last", - Self::MapGet => "host_map_get", - Self::MapDelete => "host_map_delete", - Self::MapHas => "host_map_has", - Self::MapKeys => "host_map_keys", - Self::MapValues => "host_map_values", - Self::MapItems => "host_map_items", - Self::MapMerge => "host_map_merge", - Self::MapSize => "host_map_size", - Self::CollectionClear => "host_collection_clear", - Self::CookieParse => "host_cookie_parse", - Self::MultipartParse => "host_multipart_parse", - Self::MarkdownParse => "host_markdown_parse", - Self::ReIsMatch => "host_re_is_match", - Self::ReFind => "host_re_find", - Self::ReFindAll => "host_re_find_all", - Self::ReReplace => "host_re_replace", - Self::ReSplit => "host_re_split", - Self::EncodingBase64Encode => "host_encoding_base64_encode", - Self::EncodingBase64Decode => "host_encoding_base64_decode", - Self::EncodingHexEncode => "host_encoding_hex_encode", - Self::EncodingHexDecode => "host_encoding_hex_decode", - Self::PathJoin => "host_path_join", - Self::PathSplit => "host_path_split", - Self::PathFileName => "host_path_file_name", - Self::PathParent => "host_path_parent", - Self::PathExtension => "host_path_extension", - Self::PathNormalize => "host_path_normalize", - Self::PathIsAbsolute => "host_path_is_absolute", - Self::FsCopyFile => "host_copy_file", - Self::FsMove => "host_move", - Self::FsMakeTemp => "host_make_temp", - Self::HashSha256 => "host_hash_sha256", - Self::HashSha1 => "host_hash_sha1", - Self::HashMd5 => "host_hash_md5", - Self::HashHmacSha256 => "host_hash_hmac_sha256", - Self::SecretsTokenHex => "host_secrets_token_hex", - Self::SecretsTokenUrlsafe => "host_secrets_token_urlsafe", - Self::TextContains => "host_text_contains", - Self::TextStartsWith => "host_text_starts_with", - Self::TextEndsWith => "host_text_ends_with", - Self::TextReplace => "host_text_replace", - Self::TextToUpper => "host_text_to_upper", - Self::TextToLower => "host_text_to_lower", - Self::TextRepeat => "host_text_repeat", - Self::TextPad => "host_text_pad", - Self::CsvParse => "host_csv_parse", - Self::CsvStringify => "host_csv_stringify", - Self::ListSort => "host_list_sort", - Self::ListSortBy => "host_list_sort_by", - Self::ListUnique => "host_list_unique", - Self::ListChunk => "host_list_chunk", - Self::ListZip => "host_list_zip", - Self::ListFlatten => "host_list_flatten", - Self::CliParse => "host_cli_parse", - Self::LogSetLevel => "host_log_set_level", - Self::LogLine => "host_log_line", - Self::UuidV4 => "host_uuid_v4", - Self::DatetimeNow => "host_datetime_now", - Self::DatetimeFromUnix => "host_datetime_from_unix", - Self::DatetimeToUnix => "host_datetime_to_unix", - Self::DatetimeParse => "host_datetime_parse", - Self::DatetimeFormat => "host_datetime_format", - Self::DatetimeAdd => "host_datetime_add", - Self::DatetimeInZone => "host_datetime_in_zone", - Self::UrlParse => "host_url_parse", - Self::UrlQueryParse => "host_url_query_parse", - Self::UrlQueryStringify => "host_url_query_stringify", - Self::TomlParse => "host_toml_parse", - Self::HtmlEscape => "host_html_escape", - Self::HtmlUnescape => "host_html_unescape", - Self::EncodingBase32Encode => "host_encoding_base32_encode", - Self::EncodingBase32Decode => "host_encoding_base32_decode", - Self::FsMakeDirs => "host_make_dirs", - Self::FsRemoveTree => "host_remove_tree", - Self::FsStat => "host_stat", - Self::FsWalk => "host_walk", - Self::StatsMean => "host_stats_mean", - Self::StatsMedian => "host_stats_median", - Self::StatsStdev => "host_stats_stdev", - Self::Meta => "host_meta", - Self::MetaGet => "host_meta_get", - Self::ModuleSource => "host_module_source", - Self::ArtifactKind => "host_artifact_kind", - Self::WritebackRecord => "host_writeback_record", - Self::WritebackGet => "host_writeback_get", - Self::WritebackClear => "host_writeback_clear", - Self::WritebackList => "host_writeback_list", - Self::WritebackEnsure => "host_writeback_ensure", - Self::OuterCallLine => "host_outer_call_line", - Self::TextPatch => "host_text_patch", - Self::ApplyPatchBlocks => "host_apply_patch_blocks", - Self::WritebackScanPath => "host_writeback_scan_path", - Self::SubtaskSpawn => "host_subtask_spawn", - Self::SubtaskPoll => "host_subtask_poll", - Self::SubtaskJoin => "host_subtask_join", - Self::SubtaskKill => "host_subtask_kill", - Self::SubtaskWaitAll => "host_subtask_wait_all", - } + HOST_FN_TABLE + .iter() + .find(|d| d.f.as_u16() == self.as_u16()) + .map(|d| d.name) + .unwrap_or("host_unknown") } pub fn required_params(self) -> &'static [&'static str] { - match self { - Self::Access => &["value", "key"], - Self::ReadText | Self::Exists | Self::ListDir | Self::MakeDir | Self::Remove => { - &["path"] - } - Self::WriteText | Self::AppendText => &["path", "text"], - Self::NowUnix | Self::NowMs | Self::Args | Self::Cwd | Self::Meta | Self::ModuleSource | Self::ArtifactKind => { - &[] - } - Self::FormatTime => &["unix", "pattern"], - Self::ParseTime => &["text", "pattern"], - Self::SleepMs => &["ms"], - Self::EnvGet => &["name"], - Self::MetaGet => &["key"], - Self::EnvSet => &["name", "value"], - Self::Exit => &["code"], - Self::Exec => &["cmd"], - Self::JsonParse | Self::UrlEncode | Self::JsonQuote => &["text"], - Self::JsonStringify | Self::JsonKeys => &["value"], - Self::JsonGet => &["value", "key"], - Self::HttpGet => &["url"], - Self::HttpPost => &["url", "body"], - Self::HttpPostSse => &["url", "body"], - Self::OpenAiSseParse => &["text"], - Self::StreamPublish => &["event"], - Self::HttpRequest => &["method", "url"], - Self::DotenvLoad => &[], - Self::Pi | Self::EConst | Self::Random => &[], - Self::Num | Self::MathNeg | Self::MathSin | Self::MathCos | Self::MathTan - | Self::MathAsin | Self::MathAcos | Self::MathAtan | Self::MathSqrt - | Self::MathAbs | Self::MathLn | Self::MathExp | Self::MathFloor | Self::MathCeil - | Self::Seed | Self::Formula | Self::Simplify | Self::Expand => &["value"], - Self::MathAdd | Self::MathSub | Self::MathMul | Self::MathDiv | Self::MathPow - | Self::MathMin | Self::MathMax => &["a", "b"], - Self::RandomInt => &["min", "max"], - Self::Diff => &["formula", "var"], - Self::Subs | Self::Eval => &["formula", "var", "value"], - Self::Solve => &["formula", "var"], - Self::Plot => &["formula", "var", "min", "max"], - Self::PlotPoints => &["xs", "ys"], - Self::PlotConic => &["kind", "a"], - Self::ForeignSetCmd => &["lang", "cmd"], - Self::ForeignRun => &["code"], - Self::ForeignRunLang => &["lang", "source"], - Self::ForeignInvoke | Self::ForeignCall => &[], - Self::ForeignProbe | Self::ForeignLoadConfig => &[], - Self::ForeignRegister | Self::ForeignUnset => &["lang"], - Self::ForeignRunMany => &["runs"], - Self::ForeignCallMany => &["inputs"], - Self::ForeignSessionOpen => &["lang"], - Self::ForeignSessionEval => &["id"], - Self::ForeignSessionSet => &["id", "name", "value"], - Self::ForeignSessionGet => &["id", "name"], - Self::ForeignSessionNames => &["id"], - Self::ForeignSessionClose => &["id"], - Self::ForeignSessionList => &[], - Self::ForeignSessionInterrupt => &["id"], - Self::ForeignLangs => &[], - Self::PluginLoad => &["path"], - Self::PluginUnload | Self::PluginList => &[], - Self::ExtNativePath => &["name"], - Self::OuterCallLine => &[], - Self::TextPatch => &["path", "find", "replace"], - Self::ApplyPatchBlocks => &["path", "text", "soft"], - Self::WritebackScanPath => &["path"], - Self::MapSet => &["map", "key", "value"], - Self::ListAppend => &["list", "item"], - Self::CollectionPut => &["in", "at", "value"], - Self::ListPrepend => &["list", "item"], - Self::ListConcat => &["a", "b"], - Self::ListInsert => &["list", "index", "item"], - Self::ListSetAt => &["list", "index", "item"], - Self::ListRemoveAt => &["list", "index"], - Self::ListSlice => &["list", "start"], - Self::ListContains => &["list", "item"], - Self::ListIndexOf => &["list", "item"], - Self::ListReverse | Self::ListFirst | Self::ListLast => &["list"], - Self::MapGet | Self::MapDelete | Self::MapHas => &["map", "key"], - Self::MapKeys | Self::MapValues | Self::MapItems | Self::MapSize => &["map"], - Self::MapMerge => &["a", "b"], - Self::CollectionClear => &["value"], - Self::CookieParse => &["text", "is_response"], - Self::MultipartParse => &["body", "boundary"], - Self::MarkdownParse => &["text"], - Self::ReIsMatch | Self::ReFind | Self::ReFindAll | Self::ReSplit => { - &["text", "pattern"] - } - Self::ReReplace => &["text", "pattern", "with"], - Self::EncodingBase64Encode - | Self::EncodingBase64Decode - | Self::EncodingHexEncode - | Self::EncodingHexDecode - | Self::EncodingBase32Encode - | Self::EncodingBase32Decode => &["text"], - Self::PathJoin => &["a", "b"], - Self::PathSplit - | Self::PathFileName - | Self::PathParent - | Self::PathExtension - | Self::PathNormalize - | Self::PathIsAbsolute => &["path"], - Self::FsCopyFile | Self::FsMove => &["src", "dest"], - Self::FsMakeTemp => &[], - Self::HashSha256 | Self::HashSha1 | Self::HashMd5 => &["text"], - Self::HashHmacSha256 => &["key", "text"], - Self::SecretsTokenHex | Self::SecretsTokenUrlsafe => &[], - Self::TextContains => &["text", "sub"], - Self::TextStartsWith => &["text", "prefix"], - Self::TextEndsWith => &["text", "suffix"], - Self::TextReplace => &["text", "old", "new"], - Self::TextToUpper | Self::TextToLower => &["text"], - Self::TextRepeat => &["text", "n"], - Self::TextPad => &["text", "width"], - Self::CsvParse => &["text"], - Self::CsvStringify => &["rows"], - Self::ListSort | Self::ListUnique | Self::ListFlatten => &["list"], - Self::ListSortBy => &["list", "key"], - Self::ListChunk => &["list", "size"], - Self::ListZip => &["a", "b"], - Self::CliParse => &[], - Self::LogSetLevel => &["level"], - Self::LogLine => &["level", "text"], - Self::UuidV4 => &[], - Self::StatsMean | Self::StatsMedian | Self::StatsStdev => &["list"], - Self::DatetimeNow => &[], - Self::DatetimeFromUnix => &["unix"], - Self::DatetimeToUnix => &["dt"], - Self::DatetimeParse => &["text"], - Self::DatetimeFormat => &["dt"], - Self::DatetimeAdd => &["dt"], - Self::DatetimeInZone => &["dt", "zone"], - Self::UrlParse | Self::UrlQueryParse => &["text"], - Self::UrlQueryStringify => &["map"], - Self::TomlParse | Self::HtmlEscape | Self::HtmlUnescape => &["text"], - Self::FsMakeDirs | Self::FsRemoveTree | Self::FsStat | Self::FsWalk => &["path"], - Self::WritebackRecord => &["value"], - Self::WritebackGet | Self::WritebackClear => &[], - Self::WritebackList => &[], - Self::WritebackEnsure => &["key"], - Self::SubtaskSpawn => &[], - Self::SubtaskPoll | Self::SubtaskJoin | Self::SubtaskKill => &["id"], - Self::SubtaskWaitAll => &[], - } + HOST_FN_TABLE + .iter() + .find(|d| d.f.as_u16() == self.as_u16()) + .map(|d| d.required) + .unwrap_or(&[]) } pub fn optional_params(self) -> &'static [&'static str] { - match self { - Self::JsonStringify => &["indent"], - Self::HttpGet => &["headers"], - Self::HttpPost => &["content_type", "headers"], - Self::HttpPostSse => &["content_type", "headers", "echo"], - Self::OpenAiSseParse => &["echo"], - Self::HttpRequest => &["body", "content_type", "headers"], - Self::DotenvLoad => &["path"], - Self::Exec => &["args", "capture"], - Self::Solve => &["min", "max"], - Self::Plot => &["steps", "path", "derivative", "grid"], - Self::PlotPoints => &["path", "grid"], - Self::PlotConic => &["b", "h", "k", "path", "grid"], - Self::ForeignSetCmd => &["args"], - Self::ForeignRun | Self::ForeignRunLang => { - &["stdin", "env", "cwd", "timeout", "args", "max_output"] - } - Self::ForeignInvoke => &[ - "code", - "lang", - "source", - "stdin", - "input", - "env", - "cwd", - "timeout", - "args", - "output", - "max_output", - "cmd", - "keep", - ], - Self::ForeignProbe => &["lang"], - Self::ForeignRegister => &[ - "cmd", - "args", - "template", - "ext", - "env", - "version_args", - ], - Self::ForeignUnset => &[], - Self::ForeignLoadConfig => &["path"], - Self::ForeignCall => &[ - "code", "lang", "source", "input", "stdin", "env", "cwd", "timeout", "args", - ], - Self::ForeignRunMany => &["concurrency"], - Self::ForeignCallMany => &[ - "code", - "lang", - "source", - "cmd", - "env", - "cwd", - "timeout", - "args", - "max_output", - "concurrency", - ], - Self::ForeignSessionOpen => &["cmd", "env", "cwd", "timeout"], - Self::ForeignSessionEval => &["code", "source", "input", "stdin", "timeout"], - Self::ForeignSessionSet - | Self::ForeignSessionGet - | Self::ForeignSessionNames - | Self::ForeignSessionClose - | Self::ForeignSessionList => &[], - Self::WritebackRecord | Self::WritebackGet | Self::WritebackClear => { - &["at_end", "key", "line"] - } - Self::WritebackEnsure => &["placeholder", "line"], - Self::SubtaskSpawn => &["path", "fn", "args", "code", "lang", "source", "stdin", "quiet"], - Self::ListSlice => &["end"], - Self::ReReplace => &["count"], - Self::FsMakeTemp => &["prefix"], - Self::SecretsTokenHex | Self::SecretsTokenUrlsafe => &["n"], - Self::TextReplace => &["count"], - Self::TextPad => &["fill", "align"], - Self::CliParse => &["args"], - Self::DatetimeFromUnix => &["zone"], - Self::DatetimeParse => &["pattern"], - Self::DatetimeFormat => &["style"], - Self::DatetimeAdd => &["days", "hours", "minutes", "seconds"], - Self::LogLine => &["fields"], - _ => &[], - } + HOST_FN_TABLE + .iter() + .find(|d| d.f.as_u16() == self.as_u16()) + .map(|d| d.optional) + .unwrap_or(&[]) } pub fn all_params(self) -> Vec<&'static str> { @@ -2050,3 +1645,4 @@ mod tests { } } } +