feat(stdlib): Mid M2 path algebra and fs copy/move/temp
Add lib/path (EN/ZH) and thicken fs with sandboxed copy_file, move, and make_temp, with golds and public demos. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
838a2b425a
commit
94ae6713aa
22 files changed
+618
-25
No files matched your search
+2
-1
@@ -3,8 +3,9 @@
|
||||
## Unreleased
|
||||
|
||||
### Added
|
||||
- **中层标准库 M2**:`lib/path` / `lib/路径`(join/split/normalize/…)与 `fs`/`文件` 加厚(`copy_file`/`move`/`make_temp`);金样与 public `09-path-fs`。设计 [stdlib-path.md](doc/design/stdlib-path.md) · [stdlib-fs-mid.md](doc/design/stdlib-fs-mid.md)。
|
||||
- **中层标准库 M1**:`lib/re` / `lib/正则`(`is_match`/`find`/`find_all`/`replace`/`split`)与 `lib/encoding` / `lib/编码`(base64/hex);host `host_re_*` + `host_encoding_*`;金样与 public `08-re-encoding`。设计 [stdlib-mid.md](doc/design/stdlib-mid.md) · [stdlib-re.md](doc/design/stdlib-re.md) · [stdlib-encoding.md](doc/design/stdlib-encoding.md)。
|
||||
- **中层标准库(规划)**:通用原语沉 `lib/`(path / hash / csv …),减少「事事 ABI 插件」。路线 [stdlib-mid.md](doc/roadmap/stdlib-mid.md)。
|
||||
- **中层标准库(规划)**:通用原语沉 `lib/`(hash / csv …),减少「事事 ABI 插件」。路线 [stdlib-mid.md](doc/roadmap/stdlib-mid.md)。
|
||||
- **`ext/linalg`**:L0–L6 + 公式文档面 **F1–F3**(中缀、`declare`、展示/`explicit` 默认化简、`T_ascii`、public「如何写公式文档」);示例 `linalg-transpose` · `linalg-svd` · `linalg-least-squares`。设计:[ext-linalg-formula-doc.md](doc/design/ext-linalg-formula-doc.md)。
|
||||
|
||||
### Fixed
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# `lib/fs` 加厚 — 复制 / 移动 / 临时文件(Mid M2)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **已落地(M2)** |
|
||||
| 日期 | 2026-09-07 |
|
||||
| 相关 | [stdlib-path.md](stdlib-path.md) · [stdlib-mid.md](stdlib-mid.md) |
|
||||
| 边界 | **文本与普通文件**;不引入 `bytes` 类型(`read_bytes` 延后) |
|
||||
|
||||
---
|
||||
|
||||
## 1. 新增 API
|
||||
|
||||
| 英文 | 中文 | 形参 | 结果 |
|
||||
|------|------|------|------|
|
||||
| `copy_file` | `复制文件` | `src`, `dest` | `None`(覆盖已存在的目标文件) |
|
||||
| `move` | `移动` | `src`, `dest` | `None`(rename;跨设备失败则报错) |
|
||||
| `make_temp` | `建临时` | 可选 `prefix` | 相对 `cwd`/`fs_root` 的新空文件路径文本 |
|
||||
|
||||
均走现有 `resolve_path` 沙箱;写操作受 `fs_write` 策略约束。
|
||||
|
||||
## 2. 刻意不做
|
||||
|
||||
目录树 `copy_tree`、原子替换、`read_bytes`、权限 chmod。
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **规划中 · M1(re + encoding)已实现** |
|
||||
| 状态 | **规划中 · M1–M2 已实现** |
|
||||
| 日期 | 2026-09-07 |
|
||||
| 相关 | [stdlib.md](stdlib.md) · [stdlib-modules.md](stdlib-modules.md) · [ext-abi.md](ext-abi.md) · [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md) |
|
||||
| 目标 | 在接下来数个发版周期内,把「通用编程」能力沉到 **L0.5 宿主 + L1 `.mq.md`**,减少「凡是稍有能力就必须 ABI 插件」的路径依赖 |
|
||||
@@ -118,19 +118,13 @@ OS / 算法库(精选 crate,默认 features 最小化)
|
||||
|
||||
| 波次 | 模块(英 / 中) | 典型能力 | 主要形态 |
|
||||
|------|-----------------|----------|----------|
|
||||
| **M1** | `re` / `正则` | match / find / replace / split | host + L1 |
|
||||
| **M1** | `encoding` / `编码` | base64 / hex / url 组件(与 `net.url_encode` 协调) | host + L1 |
|
||||
| **M2** | `path` / `路径` | join / split / normalize / stem / 相对化 | host + L1 |
|
||||
| **M2** | `fs` 加厚 | copy / move / read_bytes 边界 / tempfile | 扩展现有 |
|
||||
| **M3** | `hash` / `哈希` | sha256 / sha1 / md5(标识用)/ hmac | host + L1 |
|
||||
| **M3** | `secrets` / `机密` | token_hex / 安全随机(与 `math.random` 分流) | host + L1 |
|
||||
| **M4** | `csv` / `逗号表` | parse / stringify ↔ list-of-maps | host 或纯 mq |
|
||||
| **M4** | `text` 加厚 | upper/lower/starts_with/replace/pad/format | 多可纯 mq + 少量 host |
|
||||
| **M5** | `listx` 或 `table` 加厚 | sort / unique / zip / chunk / group | 优先纯 mq;慢再 host |
|
||||
| **M5** | `cli` / `命令行` | 简单 flags 解析(基于 `sys.args`) | 纯 mq 优先 |
|
||||
| **M6** | `log` / `日志` | level + 结构化一行(stdout/stderr) | 薄 host 或纯 mq |
|
||||
| **M6** | `uuid` / `标识` | v4 | host + L1 |
|
||||
| **可选** | `yaml` / `Toml` | 仅当 public/catalog 强需求;注意依赖体积 | 慎入 |
|
||||
| **M1** | `re` / `正则` · `encoding` / `编码` | match / find / replace;base64 / hex | **done** |
|
||||
| **M2** | `path` / `路径` · `fs` 加厚 | join / normalize;copy / move / temp | **done** |
|
||||
| **M3** | `hash` / `哈希` · `secrets` / `机密` | sha256 / hmac;token | host + L1 |
|
||||
| **M4** | `csv` / `逗号表` · `text` 加厚 | parse / stringify;contains / replace | host 或纯 mq |
|
||||
| **M5** | `listx` 或 `table` 加厚 · `cli` | sort / zip;flags | 优先纯 mq |
|
||||
| **M6** | `log` / `日志` · `uuid` / `标识` | level 日志;v4 | 薄 host 或纯 mq |
|
||||
| **可选** | `yaml` / `Toml` | 仅当 public/catalog 强需求 | 慎入 |
|
||||
|
||||
**刻意延后:** 完整 `datetime` 时区库、连接池、异步 runtime、模板引擎、完整 `collections` 类型系统——等中层前几波用起来再议。
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **核心已落地**;**中层 Mid:M1(re/encoding)已落地** · [stdlib-mid.md](stdlib-mid.md) / [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md) |
|
||||
| 状态 | **核心已落地**;**中层 Mid:M1–M2 已落地** · [stdlib-mid.md](stdlib-mid.md) / [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md) |
|
||||
| 日期 | 2026-09-07 |
|
||||
| 原则 | 全部经 frontmatter **导入**;除 JSON 外中英分文件;内核保持少而精;**通用能力优先 Mid,插件留给领域核** |
|
||||
| 相关 | [stdlib.md](stdlib.md) · [stdlib-i18n.md](stdlib-i18n.md) · [stdlib-mid.md](stdlib-mid.md) · [ext-abi.md](ext-abi.md) · [ext-llm.md](ext-llm.md) · [ext-agent.md](ext-agent.md) · [**module-namespace.md**](module-namespace.md)(导入命名空间;**M1/M2 已落地**:`库.成员` / `库.对象.成员` / `use`) |
|
||||
@@ -18,9 +18,10 @@
|
||||
| 文本 | `lib/text.mq.md` | `lib/文本.mq.md` | 已有 |
|
||||
| **正则** | `lib/re.mq.md` | `lib/正则.mq.md` | **Mid M1** |
|
||||
| **编解码** | `lib/encoding.mq.md` | `lib/编码.mq.md` | **Mid M1** |
|
||||
| **路径** | `lib/path.mq.md` | `lib/路径.mq.md` | **Mid M2** |
|
||||
| 表 | `lib/table.mq.md` | `lib/表.mq.md` | 已有(`put`/`改` + list/map;见 [stdlib-table.md](stdlib-table.md)) |
|
||||
| 浏览器效应 | `lib/browser.mq.md` | `lib/浏览器.mq.md` | WASM 客户端效应助手(GFM + helpers;勿用 `json.set` 链) |
|
||||
| 文件 | `lib/fs.mq.md` | `lib/文件.mq.md` | 已有 |
|
||||
| 文件 | `lib/fs.mq.md` | `lib/文件.mq.md` | 已有 + Mid M2:`copy_file` / `move` / `make_temp` |
|
||||
| 系统 | `lib/sys.mq.md` | `lib/系统.mq.md` | 已有(含 `load_dotenv`) |
|
||||
| 时间 | `lib/time.mq.md` | `lib/时间.mq.md` | 已有 |
|
||||
| JSON | `lib/json.mq.md` | **同文件** | **仅** parse / stringify / quote;集合请用 table / GFM |
|
||||
@@ -33,7 +34,7 @@
|
||||
|
||||
官方可选扩展(**非** stdlib):`ext/llm` · `ext/agent` · `ext/web` · `ext/quantum` · `ext/linalg` — 见各 `ext-*.md`。
|
||||
|
||||
**中层下一波候选(未落地):** `path` / `hash` / `secrets` / `csv` / … — 见 [stdlib-mid.md](stdlib-mid.md)。**M1 已落地:** `re` / `encoding`。
|
||||
**中层下一波候选(未落地):** `hash` / `secrets` / `csv` / … — 见 [stdlib-mid.md](stdlib-mid.md)。**已落地:** M1 `re`/`encoding` · M2 `path` + `fs` 加厚。
|
||||
|
||||
设计细节:[stdlib-writeback.md](stdlib-writeback.md) · [stdlib-subtask.md](stdlib-subtask.md)。
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# `lib/path` — 路径代数(Mid M2)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **已落地(M2)** |
|
||||
| 日期 | 2026-09-07 |
|
||||
| 相关 | [stdlib-mid.md](stdlib-mid.md) · [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md) |
|
||||
| 导入 | `lib/path.mq.md` · `lib/路径.mq.md` |
|
||||
| Host | `host_path_*`(`std::path`;无 I/O、不经 `fs_root`) |
|
||||
|
||||
---
|
||||
|
||||
## 1. API
|
||||
|
||||
| 英文 | 中文 | 形参 | 结果 |
|
||||
|------|------|------|------|
|
||||
| `join` | `拼接` | `a`, `b` | 文本路径 |
|
||||
| `split` | `拆分` | `path` | 组件文本列表(去空段) |
|
||||
| `file_name` | `文件名` | `path` | 最后一段;根路径可为空文本 |
|
||||
| `parent` | `父目录` | `path` | 父路径文本;无父则 `"."` |
|
||||
| `extension` | `扩展名` | `path` | 不含点;无扩展名为 `""` |
|
||||
| `normalize` | `规范化` | `path` | 清理 `.` / `..`(纯字符串,不访问磁盘) |
|
||||
| `is_absolute` | `是绝对` | `path` | 布尔 |
|
||||
|
||||
## 2. 语义
|
||||
|
||||
- 分隔符与绝对路径判定跟随 **宿主 OS**(Linux CI 为 `/`)。
|
||||
- **不做** I/O;逃逸检查仍由 `lib/fs` 在读写时执行。
|
||||
- `join`:若 `b` 为绝对路径,结果为 `b`(同 `Path::join`)。
|
||||
|
||||
## 3. 刻意不做(v1)
|
||||
|
||||
`glob`、符号链接解析、`relative_to`、Windows UNC 专项文档。
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **M1 已落地(re + encoding)· M2–M6 规划中** |
|
||||
| 状态 | **M1–M2 已落地 · M3–M6 规划中** |
|
||||
| 日期 | 2026-09-07 |
|
||||
| 设计 | [stdlib-mid.md](../design/stdlib-mid.md) |
|
||||
| 相关 | [stdlib-modules.md](../design/stdlib-modules.md) · [next-phase.md](next-phase.md) · [ext-cli.md](../design/ext-cli.md) |
|
||||
@@ -65,14 +65,16 @@ M6 可观测与标识 log + uuid(+ 可选 yaml)
|
||||
|
||||
---
|
||||
|
||||
### M2 — 路径与文件中层
|
||||
### M2 — 路径与文件中层 — **done**
|
||||
|
||||
| 交付 | 说明 |
|
||||
|------|------|
|
||||
| `lib/path.mq.md` / `lib/路径.mq.md` | `join` / `split` / `file_name` / `parent` / `extension` / `normalize` / `is_absolute` |
|
||||
| `lib/fs` 加厚 | `copy_file` / `move` / `read_bytes` 或明确「仅文本」边界;`temp_dir` / `make_temp`(若沙箱允许) |
|
||||
| Host | 路径用 Rust `Path`/`PathBuf`;保持现有 cwd/fs_root 沙箱 |
|
||||
| 验收 | 跨 `\`/`/` 行为有金样;相对路径不逃逸 `fs_root` |
|
||||
| `lib/fs` 加厚 | `copy_file` / `move` / `make_temp`(文本边界;无 `read_bytes`) |
|
||||
| Host | `host_path_*`、`host_copy_file` / `host_move` / `host_make_temp` |
|
||||
| 金样 | `path-smoke` · `路径-烟测` · `fs-copy-move` · `文件-复制` |
|
||||
| 文档 | [stdlib-path.md](../design/stdlib-path.md) · [stdlib-fs-mid.md](../design/stdlib-fs-mid.md) · public `09-path-fs` |
|
||||
| 验收 | 规范化 + 沙箱内复制/移动 |
|
||||
|
||||
**刻意不做:** 完整 `shutil`、监视 `inotify`、权限 ACL 全家桶。
|
||||
|
||||
|
||||
@@ -40,6 +40,29 @@ description: English filesystem wrappers
|
||||
|
||||
**> host_remove path=`path`**
|
||||
|
||||
## copy_file
|
||||
+ `src`
|
||||
+ `dest`
|
||||
|
||||
Copy a file (overwrites `dest` if it exists). Paths are sandboxed like other fs ops.
|
||||
|
||||
**> host_copy_file src=`src` dest=`dest`**
|
||||
|
||||
## move
|
||||
+ `src`
|
||||
+ `dest`
|
||||
|
||||
Rename / move within the sandbox.
|
||||
|
||||
**> host_move src=`src` dest=`dest`**
|
||||
|
||||
## make_temp
|
||||
+ `prefix`=None
|
||||
|
||||
Create an empty temp file under the program directory; return its relative path.
|
||||
|
||||
**> host_make_temp prefix=`prefix`**
|
||||
|
||||
## text_patch
|
||||
+ `path`
|
||||
+ `find`
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
---
|
||||
title: lib/path
|
||||
description: Path algebra (Mid M2). No I/O; OS separators.
|
||||
---
|
||||
|
||||
## join
|
||||
+ `a`
|
||||
+ `b`
|
||||
|
||||
**> host_path_join a=`a` b=`b`**
|
||||
|
||||
## split
|
||||
+ `path`
|
||||
|
||||
**> host_path_split path=`path`**
|
||||
|
||||
## file_name
|
||||
+ `path`
|
||||
|
||||
**> host_path_file_name path=`path`**
|
||||
|
||||
## parent
|
||||
+ `path`
|
||||
|
||||
**> host_path_parent path=`path`**
|
||||
|
||||
## extension
|
||||
+ `path`
|
||||
|
||||
**> host_path_extension path=`path`**
|
||||
|
||||
## normalize
|
||||
+ `path`
|
||||
|
||||
**> host_path_normalize path=`path`**
|
||||
|
||||
## is_absolute
|
||||
+ `path`
|
||||
|
||||
**> host_path_is_absolute path=`path`**
|
||||
@@ -40,6 +40,29 @@ description: 中文文件系统包装
|
||||
|
||||
**> host_remove path=`路径`**
|
||||
|
||||
## 复制文件
|
||||
+ `源`
|
||||
+ `目标`
|
||||
|
||||
复制文件(目标已存在则覆盖)。路径沙箱同其他文件操作。
|
||||
|
||||
**> host_copy_file src=`源` dest=`目标`**
|
||||
|
||||
## 移动
|
||||
+ `源`
|
||||
+ `目标`
|
||||
|
||||
在沙箱内重命名 / 移动。
|
||||
|
||||
**> host_move src=`源` dest=`目标`**
|
||||
|
||||
## 建临时
|
||||
+ `前缀`=None
|
||||
|
||||
在程序目录下创建空临时文件,返回相对路径。
|
||||
|
||||
**> host_make_temp prefix=`前缀`**
|
||||
|
||||
## 文本补丁
|
||||
+ `路径`
|
||||
+ `查找`
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
---
|
||||
title: lib/路径
|
||||
description: 路径代数(中层 M2)。无 I/O;分隔符随宿主 OS。
|
||||
---
|
||||
|
||||
## 拼接
|
||||
+ `甲`
|
||||
+ `乙`
|
||||
|
||||
**> host_path_join a=`甲` b=`乙`**
|
||||
|
||||
## 拆分
|
||||
+ `路径`
|
||||
|
||||
**> host_path_split path=`路径`**
|
||||
|
||||
## 文件名
|
||||
+ `路径`
|
||||
|
||||
**> host_path_file_name path=`路径`**
|
||||
|
||||
## 父目录
|
||||
+ `路径`
|
||||
|
||||
**> host_path_parent path=`路径`**
|
||||
|
||||
## 扩展名
|
||||
+ `路径`
|
||||
|
||||
**> host_path_extension path=`路径`**
|
||||
|
||||
## 规范化
|
||||
+ `路径`
|
||||
|
||||
**> host_path_normalize path=`路径`**
|
||||
|
||||
## 是绝对
|
||||
+ `路径`
|
||||
|
||||
**> host_path_is_absolute path=`路径`**
|
||||
+1
-1
@@ -14,7 +14,7 @@
|
||||
|
||||
官方扩展(非 stdlib):`ext/llm`、`ext/agent`、`ext/web`、`ext/quantum`、`ext/linalg` — 见 `features/05-*.mq.md`、`06-*.mq.md`、`07-linalg.mq.md` / `07-线性代数.mq.md` 与 `doc/design/ext-*.md`。
|
||||
|
||||
中层标准库(Mid):`features/08-re-encoding.mq.md` / `08-正则编解码.mq.md`(`lib/re` · `lib/encoding`)。
|
||||
中层标准库(Mid):`features/08-re-encoding.mq.md` / `08-正则编解码.mq.md`;`09-path-fs.mq.md` / `09-路径文件.mq.md`。
|
||||
|
||||
**不放** `errors/`:失败样例只在 `tests/errors/`。
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
---
|
||||
title: Mid stdlib — path and fs
|
||||
description: lib/path + fs copy/move/temp (Mid M2)
|
||||
import path:lib/path.mq.md
|
||||
import fs:lib/fs.mq.md
|
||||
---
|
||||
|
||||
# Path algebra and file mid-layer
|
||||
|
||||
Join and normalize without plugins; copy/move stay sandboxed under the program directory.
|
||||
|
||||
# main
|
||||
|
||||
*`p` = > path.join a="out" b="demo.txt"*
|
||||
*`n` = > path.normalize path="out/./x/../demo.txt"*
|
||||
> fs.write_text path=`p` text="path-m2"
|
||||
> fs.copy_file src=`p` dest="out/demo-copy.txt"
|
||||
*`body` = > fs.read_text path="out/demo-copy.txt"*
|
||||
> print text=`p`
|
||||
> print text=`n`
|
||||
> print text=`body`
|
||||
1. `body` == "path-m2"
|
||||
> print text=mid-m2-ok
|
||||
2. *
|
||||
> print text=mid-m2-fail
|
||||
> fs.remove path=`p`
|
||||
> fs.remove path="out/demo-copy.txt"
|
||||
@@ -0,0 +1,27 @@
|
||||
---
|
||||
title: 中层标准库 — 路径与文件
|
||||
description: lib/路径 + 文件复制/移动/临时(中层 M2)
|
||||
导入 路径:lib/路径.mq.md
|
||||
导入 文件:lib/文件.mq.md
|
||||
---
|
||||
|
||||
# 路径代数与文件中层
|
||||
|
||||
无插件拼接/规范化;复制/移动仍落在程序目录沙箱内。
|
||||
|
||||
# main
|
||||
|
||||
*`p` = > 路径.拼接 甲="out" 乙="demo.txt"*
|
||||
*`n` = > 路径.规范化 路径="out/./x/../demo.txt"*
|
||||
> 文件.写文本 路径=`p` 内容="path-m2"
|
||||
> 文件.复制文件 源=`p` 目标="out/demo-copy.txt"
|
||||
*`body` = > 文件.读文本 路径="out/demo-copy.txt"*
|
||||
> 打印 内容=`p`
|
||||
> 打印 内容=`n`
|
||||
> 打印 内容=`body`
|
||||
1. `body` == "path-m2"
|
||||
> 打印 内容=mid-m2-ok
|
||||
2. *
|
||||
> 打印 内容=mid-m2-fail
|
||||
> 文件.删除 路径=`p`
|
||||
> 文件.删除 路径="out/demo-copy.txt"
|
||||
+63
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::host::{encoding, fs, json, math, net, re, sys, time, writeback, HostContext};
|
||||
use crate::host::{encoding, fs, json, math, net, path, re, sys, time, writeback, HostContext};
|
||||
#[cfg(feature = "exec-host")]
|
||||
use crate::host::{foreign, subtask};
|
||||
#[cfg(feature = "plugin-host")]
|
||||
@@ -137,6 +137,16 @@ pub enum HostFn {
|
||||
EncodingBase64Decode = 131,
|
||||
EncodingHexEncode = 132,
|
||||
EncodingHexDecode = 133,
|
||||
PathJoin = 134,
|
||||
PathSplit = 135,
|
||||
PathFileName = 136,
|
||||
PathParent = 137,
|
||||
PathExtension = 138,
|
||||
PathNormalize = 139,
|
||||
PathIsAbsolute = 140,
|
||||
FsCopyFile = 141,
|
||||
FsMove = 142,
|
||||
FsMakeTemp = 143,
|
||||
}
|
||||
|
||||
|
||||
@@ -267,6 +277,16 @@ impl HostFn {
|
||||
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,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -400,6 +420,16 @@ impl HostFn {
|
||||
"host_encoding_base64_decode" | "encoding_base64_decode" => Self::EncodingBase64Decode,
|
||||
"host_encoding_hex_encode" | "encoding_hex_encode" => Self::EncodingHexEncode,
|
||||
"host_encoding_hex_decode" | "encoding_hex_decode" => Self::EncodingHexDecode,
|
||||
"host_path_join" | "path_join" => Self::PathJoin,
|
||||
"host_path_split" | "path_split" => Self::PathSplit,
|
||||
"host_path_file_name" | "path_file_name" => Self::PathFileName,
|
||||
"host_path_parent" | "path_parent" => Self::PathParent,
|
||||
"host_path_extension" | "path_extension" => Self::PathExtension,
|
||||
"host_path_normalize" | "path_normalize" => Self::PathNormalize,
|
||||
"host_path_is_absolute" | "path_is_absolute" => Self::PathIsAbsolute,
|
||||
"host_copy_file" | "copy_file" => Self::FsCopyFile,
|
||||
"host_move" | "move_path" => Self::FsMove,
|
||||
"host_make_temp" | "make_temp" => Self::FsMakeTemp,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -520,6 +550,16 @@ impl HostFn {
|
||||
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::WritebackRecord => "host_writeback_record",
|
||||
Self::WritebackGet => "host_writeback_get",
|
||||
Self::WritebackClear => "host_writeback_clear",
|
||||
@@ -613,6 +653,15 @@ impl HostFn {
|
||||
| Self::EncodingBase64Decode
|
||||
| Self::EncodingHexEncode
|
||||
| Self::EncodingHexDecode => &["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::WritebackRecord => &["value"],
|
||||
Self::WritebackGet | Self::WritebackClear => &[],
|
||||
Self::WritebackList => &[],
|
||||
@@ -646,6 +695,7 @@ impl HostFn {
|
||||
Self::SubtaskSpawn => &["path", "fn", "args", "code", "lang", "source", "stdin", "quiet"],
|
||||
Self::ListSlice => &["end"],
|
||||
Self::ReReplace => &["count"],
|
||||
Self::FsMakeTemp => &["prefix"],
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
@@ -1029,6 +1079,18 @@ pub fn call_host(
|
||||
HostFn::EncodingBase64Decode => encoding::base64_decode(require(bound, "text")?),
|
||||
HostFn::EncodingHexEncode => encoding::hex_encode(require(bound, "text")?),
|
||||
HostFn::EncodingHexDecode => encoding::hex_decode(require(bound, "text")?),
|
||||
HostFn::PathJoin => path::join(require(bound, "a")?, require(bound, "b")?),
|
||||
HostFn::PathSplit => path::split(require(bound, "path")?),
|
||||
HostFn::PathFileName => path::file_name(require(bound, "path")?),
|
||||
HostFn::PathParent => path::parent(require(bound, "path")?),
|
||||
HostFn::PathExtension => path::extension(require(bound, "path")?),
|
||||
HostFn::PathNormalize => path::normalize(require(bound, "path")?),
|
||||
HostFn::PathIsAbsolute => path::is_absolute(require(bound, "path")?),
|
||||
HostFn::FsCopyFile => {
|
||||
fs::copy_file(ctx, require(bound, "src")?, require(bound, "dest")?)
|
||||
}
|
||||
HostFn::FsMove => fs::move_path(ctx, require(bound, "src")?, require(bound, "dest")?),
|
||||
HostFn::FsMakeTemp => fs::make_temp(ctx, bound.get("prefix")),
|
||||
HostFn::OuterCallLine => Ok(Value::Int(
|
||||
ctx.call_site_lines
|
||||
.first()
|
||||
|
||||
@@ -107,6 +107,61 @@ pub fn remove(ctx: &HostContext, path: &Value) -> Result<Value, String> {
|
||||
Ok(Value::None)
|
||||
}
|
||||
|
||||
pub fn copy_file(ctx: &HostContext, src: &Value, dest: &Value) -> Result<Value, String> {
|
||||
if !ctx.allow_fs_write() {
|
||||
return Err("copy_file denied by host policy".into());
|
||||
}
|
||||
let from = ctx.resolve_path(text_path(src)?)?;
|
||||
let to = ctx.resolve_path(text_path(dest)?)?;
|
||||
if let Some(parent) = to.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| format!("copy_file mkdir: {e}"))?;
|
||||
}
|
||||
fs::copy(&from, &to).map_err(|e| format!("copy_file {} -> {}: {e}", from.display(), to.display()))?;
|
||||
Ok(Value::None)
|
||||
}
|
||||
|
||||
pub fn move_path(ctx: &HostContext, src: &Value, dest: &Value) -> Result<Value, String> {
|
||||
if !ctx.allow_fs_write() {
|
||||
return Err("move denied by host policy".into());
|
||||
}
|
||||
let from = ctx.resolve_path(text_path(src)?)?;
|
||||
let to = ctx.resolve_path(text_path(dest)?)?;
|
||||
if let Some(parent) = to.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| format!("move mkdir: {e}"))?;
|
||||
}
|
||||
fs::rename(&from, &to).map_err(|e| format!("move {} -> {}: {e}", from.display(), to.display()))?;
|
||||
Ok(Value::None)
|
||||
}
|
||||
|
||||
/// Create an empty temp file under the program cwd / fs_root; return relative path text.
|
||||
pub fn make_temp(ctx: &HostContext, prefix: Option<&Value>) -> Result<Value, String> {
|
||||
if !ctx.allow_fs_write() {
|
||||
return Err("make_temp denied by host policy".into());
|
||||
}
|
||||
let pref = match prefix {
|
||||
None | Some(Value::None) => "mqtmp".to_string(),
|
||||
Some(v) => text_path(v)?.to_string(),
|
||||
};
|
||||
if pref.contains('/') || pref.contains('\\') || pref.contains("..") {
|
||||
return Err("make_temp: prefix must be a simple name".into());
|
||||
}
|
||||
let stamp = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_nanos())
|
||||
.unwrap_or(0);
|
||||
let name = format!("{pref}-{stamp}");
|
||||
let p = ctx.resolve_path(&name)?;
|
||||
if let Some(parent) = p.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| format!("make_temp mkdir: {e}"))?;
|
||||
}
|
||||
fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(&p)
|
||||
.map_err(|e| format!("make_temp {}: {e}", p.display()))?;
|
||||
Ok(Value::Text(name))
|
||||
}
|
||||
|
||||
/// Exact UTF-8 FIND→REPLACE on a file. `find` must occur exactly once.
|
||||
pub fn text_patch(ctx: &HostContext, path: &Value, find: &Value, replace: &Value) -> Result<Value, String> {
|
||||
if !ctx.allow_fs_write() {
|
||||
|
||||
@@ -15,6 +15,7 @@ mod fs;
|
||||
pub(crate) mod json;
|
||||
pub mod math;
|
||||
mod net;
|
||||
mod path;
|
||||
mod re;
|
||||
#[cfg(feature = "plugin-host")]
|
||||
pub mod plugin;
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
//! Path algebra for Mid M2 (`lib/path`) — no I/O.
|
||||
|
||||
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() {
|
||||
".".into()
|
||||
} else {
|
||||
p.to_string_lossy().into_owned()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn join(a: &Value, b: &Value) -> Result<Value, String> {
|
||||
let a = as_text(a, "a")?;
|
||||
let b = as_text(b, "b")?;
|
||||
let out = Path::new(a).join(b);
|
||||
Ok(Value::Text(path_to_text(&out)))
|
||||
}
|
||||
|
||||
pub fn split(path: &Value) -> Result<Value, String> {
|
||||
let p = as_text(path, "path")?;
|
||||
let mut parts = Vec::new();
|
||||
for c in Path::new(p).components() {
|
||||
match c {
|
||||
Component::RootDir | Component::CurDir | Component::ParentDir => {
|
||||
parts.push(Value::Text(c.as_os_str().to_string_lossy().into_owned()));
|
||||
}
|
||||
Component::Prefix(prefix) => {
|
||||
parts.push(Value::Text(prefix.as_os_str().to_string_lossy().into_owned()));
|
||||
}
|
||||
Component::Normal(s) => parts.push(Value::Text(s.to_string_lossy().into_owned())),
|
||||
}
|
||||
}
|
||||
Ok(Value::List(parts))
|
||||
}
|
||||
|
||||
pub fn file_name(path: &Value) -> Result<Value, String> {
|
||||
let p = as_text(path, "path")?;
|
||||
Ok(Value::Text(
|
||||
Path::new(p)
|
||||
.file_name()
|
||||
.map(|s| s.to_string_lossy().into_owned())
|
||||
.unwrap_or_default(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn parent(path: &Value) -> Result<Value, String> {
|
||||
let p = as_text(path, "path")?;
|
||||
let parent = Path::new(p).parent();
|
||||
Ok(Value::Text(match parent {
|
||||
Some(par) if !par.as_os_str().is_empty() => path_to_text(par),
|
||||
_ => ".".into(),
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn extension(path: &Value) -> Result<Value, String> {
|
||||
let p = as_text(path, "path")?;
|
||||
Ok(Value::Text(
|
||||
Path::new(p)
|
||||
.extension()
|
||||
.map(|s| s.to_string_lossy().into_owned())
|
||||
.unwrap_or_default(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn normalize(path: &Value) -> Result<Value, String> {
|
||||
let p = as_text(path, "path")?;
|
||||
let mut out = PathBuf::new();
|
||||
for c in Path::new(p).components() {
|
||||
match c {
|
||||
Component::CurDir => {}
|
||||
Component::ParentDir => {
|
||||
match out.components().next_back() {
|
||||
Some(Component::Normal(_)) => {
|
||||
out.pop();
|
||||
}
|
||||
Some(Component::RootDir) | Some(Component::Prefix(_)) => {}
|
||||
_ => out.push(".."),
|
||||
}
|
||||
}
|
||||
other => out.push(other.as_os_str()),
|
||||
}
|
||||
}
|
||||
Ok(Value::Text(path_to_text(&out)))
|
||||
}
|
||||
|
||||
pub fn is_absolute(path: &Value) -> Result<Value, String> {
|
||||
let p = as_text(path, "path")?;
|
||||
Ok(Value::Bool(Path::new(p).is_absolute()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn join_and_name() {
|
||||
let j = join(
|
||||
&Value::Text("a/b".into()),
|
||||
&Value::Text("c.txt".into()),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(j, Value::Text("a/b/c.txt".into()));
|
||||
assert_eq!(
|
||||
file_name(&Value::Text("a/b/c.txt".into())).unwrap(),
|
||||
Value::Text("c.txt".into())
|
||||
);
|
||||
assert_eq!(
|
||||
extension(&Value::Text("c.txt".into())).unwrap(),
|
||||
Value::Text("txt".into())
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1066,6 +1066,35 @@ fn lib_fs_read() {
|
||||
assert_out("tests/lib/fs-read.mq.md", "exists\nhello-lib");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lib_path_smoke() {
|
||||
assert_out(
|
||||
"tests/lib/path-smoke.mq.md",
|
||||
"dir/sub/f.txt\nf.txt\ntxt\ndir/sub\na/c\nTrue\na|b|c",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lib_path_smoke_zh() {
|
||||
assert_out(
|
||||
"tests/lib/路径-烟测.mq.md",
|
||||
"dir/sub/f.txt\nf.txt\ntxt\na/c",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lib_fs_copy_move() {
|
||||
assert_out(
|
||||
"tests/lib/fs-copy-move.mq.md",
|
||||
"m2-body\nmoved\ntemp-ok",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lib_fs_copy_zh() {
|
||||
assert_out("tests/lib/文件-复制.mq.md", "zh-body");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lib_re_smoke() {
|
||||
assert_out(
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
---
|
||||
title: fs copy/move/temp
|
||||
import fs:lib/fs.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
> fs.write_text path="m2-src.txt" text="m2-body"
|
||||
|
||||
> fs.copy_file src="m2-src.txt" dest="m2-copy.txt"
|
||||
|
||||
*t = > fs.read_text path="m2-copy.txt"*
|
||||
> print text=`t`
|
||||
|
||||
> fs.move src="m2-copy.txt" dest="m2-moved.txt"
|
||||
|
||||
*ok = > fs.exists path="m2-moved.txt"*
|
||||
1. `ok`
|
||||
> print text=moved
|
||||
2. *
|
||||
> print text=missing
|
||||
|
||||
*tmp = > fs.make_temp prefix="m2"*
|
||||
> fs.write_text path=`tmp` text=temp-ok
|
||||
*tt = > fs.read_text path=`tmp`*
|
||||
> print text=`tt`
|
||||
|
||||
> fs.remove path="m2-src.txt"
|
||||
> fs.remove path="m2-moved.txt"
|
||||
> fs.remove path=`tmp`
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: lib/path smoke
|
||||
import path:lib/path.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*j = > path.join a="dir/sub" b="f.txt"*
|
||||
> print text=`j`
|
||||
|
||||
*name = > path.file_name path=`j`*
|
||||
> print text=`name`
|
||||
|
||||
*ext = > path.extension path=`j`*
|
||||
> print text=`ext`
|
||||
|
||||
*par = > path.parent path=`j`*
|
||||
> print text=`par`
|
||||
|
||||
*norm = > path.normalize path="a/./b/../c"*
|
||||
> print text=`norm`
|
||||
|
||||
*abs = > path.is_absolute path="/tmp/x"*
|
||||
> print text=`abs`
|
||||
|
||||
*parts = > path.split path="a/b/c"*
|
||||
*pj = > join value=`parts` sep="|"*
|
||||
> print text=`pj`
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
title: 文件 复制移动
|
||||
导入 文件:lib/文件.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
> 文件.写文本 路径="m2-zh-src.txt" 内容="zh-body"
|
||||
> 文件.复制文件 源="m2-zh-src.txt" 目标="m2-zh-copy.txt"
|
||||
*t = > 文件.读文本 路径="m2-zh-copy.txt"*
|
||||
> 打印 内容=`t`
|
||||
> 文件.删除 路径="m2-zh-src.txt"
|
||||
> 文件.删除 路径="m2-zh-copy.txt"
|
||||
@@ -0,0 +1,18 @@
|
||||
---
|
||||
title: lib/路径 烟测
|
||||
导入 路径:lib/路径.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*j = > 路径.拼接 甲="dir/sub" 乙="f.txt"*
|
||||
> 打印 内容=`j`
|
||||
|
||||
*name = > 路径.文件名 路径=`j`*
|
||||
> 打印 内容=`name`
|
||||
|
||||
*ext = > 路径.扩展名 路径=`j`*
|
||||
> 打印 内容=`ext`
|
||||
|
||||
*norm = > 路径.规范化 路径="a/./b/../c"*
|
||||
> 打印 内容=`norm`
|
||||
Reference in new issue
Block a user