feat(stdlib): add streaming IO and console line APIs
This commit is contained in:
1 parent
aed6a80ba7
commit
3b13d8499a
7 files changed
+497
-17
No files matched your search
@@ -406,3 +406,43 @@ fn program_stderr_is_separate_from_stdout_and_diagnostics() {
|
||||
assert_eq!(values[2]["value"], true);
|
||||
assert_eq!(values[3]["value"], true);
|
||||
}
|
||||
|
||||
const CONSOLE_READER: &str = r#"---
|
||||
import:
|
||||
console: "std:io/console"
|
||||
---
|
||||
|
||||
## main
|
||||
|
||||
**第一 = console.读取行()**
|
||||
|
||||
**第二 = console.读取行()**
|
||||
|
||||
**第三 = console.读取行()**
|
||||
|
||||
**输出 = console.打印(第一.value + "|" + 第二.value)**
|
||||
|
||||
*[第一.ok, 第二.ok, 第三.ok, 第三.code, 输出.ok]*
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
fn stdlib_console_reads_lines_and_reports_eof() {
|
||||
let dir = TempDir::new("console");
|
||||
write_file(&dir.path().join("index.mq.md"), CONSOLE_READER);
|
||||
let output = cli_with_stdin(dir.path(), &["run", "index.mq.md", "--json"], b"one\ntwo\n");
|
||||
assert_eq!(
|
||||
output.status.code(),
|
||||
Some(0),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
let report: serde_json::Value = serde_json::from_slice(&output.stdout).expect("json output");
|
||||
assert_eq!(report["success"], true);
|
||||
assert_eq!(report["stdout"], "one|two\n");
|
||||
let values = report["value"]["items"].as_array().expect("list result");
|
||||
assert_eq!(values[0]["value"], true);
|
||||
assert_eq!(values[1]["value"], true);
|
||||
assert_eq!(values[2]["value"], false);
|
||||
assert_eq!(values[3]["value"], "io.eof");
|
||||
assert_eq!(values[4]["value"], true);
|
||||
}
|
||||
+5
-2
@@ -5,8 +5,11 @@ This directory is the official, offline `marqdo-stdlib` bundle consumed by
|
||||
module; a mismatch is a hard error.
|
||||
|
||||
- `format.mq.md` — value formatting (`显示`, list/map/table display, escaping)
|
||||
- `text.mq.md` — UTF-8 encoding into `Memory`
|
||||
- `io.mq.md` — `写文本` / `写值` through `resource.write(stdout)`
|
||||
- `text.mq.md` — compatibility UTF-8 encoding into `Memory`
|
||||
- `text/utf8.mq.md` — `.mq.md` UTF-8 encoder and validating decoder
|
||||
- `io.mq.md` — compatibility facade over `stream` and `console`
|
||||
- `io/stream.mq.md` — `写全部` / `写文本` with short-write and close checking
|
||||
- `io/console.mq.md` — `读取行` / `打印` over `stdio/stdin` and `stdio/stdout`
|
||||
- `demo.mq.md` — side-by-side comparison with builtin `打印`
|
||||
- `data/table.mq.md` — value-returning table select/filter/sort/append/set/join/group/GFM
|
||||
- `net/http.mq.md` — HTTP/1.1 response parsing (headers/chunked) and request builder
|
||||
|
||||
+17
-14
@@ -1,26 +1,21 @@
|
||||
---
|
||||
import:
|
||||
format: "./format.mq.md"
|
||||
text: "./text.mq.md"
|
||||
stream: "./io/stream.mq.md"
|
||||
console: "./io/console.mq.md"
|
||||
---
|
||||
|
||||
## 写文本
|
||||
|
||||
`(文本)`
|
||||
|
||||
把文本编码为 UTF-8 并写入已授权的 stdout 资源流。
|
||||
*stream.写文本(文本)*
|
||||
|
||||
**内存 = memory.new(1, 1)**
|
||||
## 写全部
|
||||
|
||||
**长度 = text.utf8编码(文本, 内存, 0)**
|
||||
`(流, 内存, 起点, 长度)`
|
||||
|
||||
**流 = resource.open("stdio", "stdout", ["write"])**
|
||||
|
||||
> resource.write(流, 内存, 0, 长度)
|
||||
|
||||
> resource.close(流)
|
||||
|
||||
*None*
|
||||
*stream.写全部(流, 内存, 起点, 长度)*
|
||||
|
||||
## 写值
|
||||
|
||||
@@ -28,8 +23,16 @@ import:
|
||||
|
||||
按语言值语义格式化后输出,并追加换行。
|
||||
|
||||
**文字 = format.显示(值) + "\n"**
|
||||
*stream.写文本(format.显示(值) + "\n")*
|
||||
|
||||
> 写文本(文字)
|
||||
## 读取行
|
||||
|
||||
*None*
|
||||
`()`
|
||||
|
||||
*console.读取行()*
|
||||
|
||||
## 打印
|
||||
|
||||
`(值)`
|
||||
|
||||
*console.打印(值)*
|
||||
@@ -0,0 +1,109 @@
|
||||
---
|
||||
import:
|
||||
format: "../format.mq.md"
|
||||
stream: "./stream.mq.md"
|
||||
utf8: "../text/utf8.mq.md"
|
||||
---
|
||||
|
||||
## 读取行
|
||||
|
||||
`()`
|
||||
|
||||
从已授权的 `stdio/stdin` 资源读取一行。按有界块读取并增量解码 UTF-8;
|
||||
EOF 返回 `{ok:false, code:"io.eof"}`,无效 UTF-8 返回可恢复错误。
|
||||
|
||||
**流 = resource.open("stdio", "stdin", ["read"])**
|
||||
|
||||
**内存 = memory.new(1, 1)**
|
||||
|
||||
**字节 = []**
|
||||
|
||||
**结束 = false**
|
||||
|
||||
**错误 = ""**
|
||||
|
||||
- `not 结束`
|
||||
|
||||
**读取 = resource.read(流, 内存, 0, 1)**
|
||||
|
||||
1. `not 读取.ok`
|
||||
|
||||
**结束 = true**
|
||||
|
||||
**错误 = 读取.code**
|
||||
|
||||
2. `读取.value.eof and 读取.value.count == 0`
|
||||
|
||||
**结束 = true**
|
||||
|
||||
3. `内存[0] == 10`
|
||||
|
||||
**结束 = true**
|
||||
|
||||
4. `true`
|
||||
|
||||
**字节 = list.append(字节, 内存[0])**
|
||||
|
||||
**关闭 = resource.close(流)**
|
||||
|
||||
1. `错误 != ""`
|
||||
|
||||
*{ok: false, code: 错误, message: "stdin read failed"}*
|
||||
|
||||
2. `not 关闭.ok`
|
||||
|
||||
*关闭*
|
||||
|
||||
3. `长度(字节) == 0`
|
||||
|
||||
*{ok: false, code: "io.eof", message: "end of input"}*
|
||||
|
||||
4. `true`
|
||||
|
||||
**页 = (长度(字节) + 65535) // 65536**
|
||||
|
||||
**数据 = memory.new(页, 页)**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(字节)`
|
||||
|
||||
**`数据[下标] = 字节[下标]`**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
**解码 = utf8.解码(数据, 0, 长度(字节))**
|
||||
|
||||
1. `not 解码.ok`
|
||||
|
||||
*解码*
|
||||
|
||||
2. `true`
|
||||
|
||||
*{ok: true, value: 解码.value}*
|
||||
|
||||
## 打印
|
||||
|
||||
`(值)`
|
||||
|
||||
**结果 = stream.写文本(format.显示(值) + "\n")**
|
||||
|
||||
1. `not 结果.ok`
|
||||
|
||||
*结果*
|
||||
|
||||
2. `true`
|
||||
|
||||
*{ok: true, value: None}*
|
||||
|
||||
## 写文本
|
||||
|
||||
`(文本)`
|
||||
|
||||
*stream.写文本(文本)*
|
||||
|
||||
## 写全部
|
||||
|
||||
`(流, 内存, 起点, 长度)`
|
||||
|
||||
*stream.写全部(流, 内存, 起点, 长度)*
|
||||
@@ -0,0 +1,101 @@
|
||||
---
|
||||
import:
|
||||
format: "../format.mq.md"
|
||||
utf8: "../text/utf8.mq.md"
|
||||
---
|
||||
|
||||
## 页数
|
||||
|
||||
`(字节)`
|
||||
|
||||
**页 = (字节 + 65535) // 65536**
|
||||
|
||||
1. `页 == 0`
|
||||
|
||||
*1*
|
||||
|
||||
2. `true`
|
||||
|
||||
*页*
|
||||
|
||||
## 写全部
|
||||
|
||||
`(流, 内存, 起点, 长度)`
|
||||
|
||||
把 `长度` 字节从 `内存` 写到资源流。短写只返回实际数量,调用方继续;
|
||||
零进度返回 `io.write_zero`,不静默吞掉失败。
|
||||
|
||||
**已写 = 0**
|
||||
|
||||
**失败 = false**
|
||||
|
||||
**本次 = {ok: true, value: {count: 0}}**
|
||||
|
||||
**错误结果 = {ok: false, code: "io.write_zero", message: "resource.write made no progress"}**
|
||||
|
||||
- `已写 < 长度`
|
||||
|
||||
**剩余 = 长度 - 已写**
|
||||
|
||||
**本次 = resource.write(流, 内存, 起点 + 已写, 剩余)**
|
||||
|
||||
1. `not 本次.ok`
|
||||
|
||||
**失败 = true**
|
||||
|
||||
**错误结果 = 本次**
|
||||
|
||||
**已写 = 长度**
|
||||
|
||||
2. `本次.value.count == 0`
|
||||
|
||||
**失败 = true**
|
||||
|
||||
**已写 = 长度**
|
||||
|
||||
3. `true`
|
||||
|
||||
**已写 = 已写 + 本次.value.count**
|
||||
|
||||
1. `失败`
|
||||
|
||||
*错误结果*
|
||||
|
||||
2. `已写 < 长度`
|
||||
|
||||
*错误结果*
|
||||
|
||||
3. `true`
|
||||
|
||||
*{ok: true, value: {count: 已写}}*
|
||||
|
||||
## 写文本
|
||||
|
||||
`(文本)`
|
||||
|
||||
把文本编码为 UTF-8 并写入已授权的 stdout 资源流。内存按实际字节长度增长,
|
||||
写入循环检查 `{ok,count}`;只有全部字节写完并关闭成功才返回成功。
|
||||
|
||||
**字节数 = format.字节长度(文本)**
|
||||
|
||||
**内存 = memory.new(页数(字节数), 页数(字节数))**
|
||||
|
||||
**长度 = utf8.编码(文本, 内存, 0)**
|
||||
|
||||
**流 = resource.open("stdio", "stdout", ["write"])**
|
||||
|
||||
**写结果 = 写全部(流, 内存, 0, 长度)**
|
||||
|
||||
**关闭 = resource.close(流)**
|
||||
|
||||
1. `not 写结果.ok`
|
||||
|
||||
*写结果*
|
||||
|
||||
2. `not 关闭.ok`
|
||||
|
||||
*关闭*
|
||||
|
||||
3. `true`
|
||||
|
||||
*{ok: true, value: None}*
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "marqdo-stdlib",
|
||||
"version": "0.1.0",
|
||||
"digest": "sha256:832940f35fe5651eabe4ac672e12e2d8f2bf4e649cc430e455425b4f2a983121"
|
||||
"digest": "sha256:2ac71536436fefbb96b3ba9e60713bef82d34c746090ae2ebb53a5e225fd789d"
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
## 成功
|
||||
|
||||
`(值)`
|
||||
|
||||
*{ok: true, value: 值}*
|
||||
|
||||
## 失败
|
||||
|
||||
`(代码, 消息)`
|
||||
|
||||
*{ok: false, code: 代码, message: 消息}*
|
||||
|
||||
## 续字节
|
||||
|
||||
`(字节)`
|
||||
|
||||
*字节 >= 128 and 字节 < 192*
|
||||
|
||||
## 编码
|
||||
|
||||
`(文本, 内存, 基址)`
|
||||
|
||||
把 Unicode 标量序列按 UTF-8 写入 `Memory`,返回字节长度。
|
||||
这是 `.mq.md` 自己实现的编码逻辑,不是 Rust 内建 UTF-8 转换。
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
**偏移 = 0**
|
||||
|
||||
- `下标 < 长度(点)`
|
||||
|
||||
**码点 = 点[下标]**
|
||||
|
||||
1. `码点 < 128`
|
||||
|
||||
**`内存[基址 + 偏移] = 码点`**
|
||||
|
||||
**偏移 = 偏移 + 1**
|
||||
|
||||
2. `码点 < 2048`
|
||||
|
||||
**`内存[基址 + 偏移] = 192 + 码点 // 64`**
|
||||
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 % 64`**
|
||||
|
||||
**偏移 = 偏移 + 2**
|
||||
|
||||
3. `码点 < 65536`
|
||||
|
||||
**`内存[基址 + 偏移] = 224 + 码点 // 4096`**
|
||||
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 64 % 64`**
|
||||
|
||||
**`内存[基址 + 偏移 + 2] = 128 + 码点 % 64`**
|
||||
|
||||
**偏移 = 偏移 + 3**
|
||||
|
||||
4. `true`
|
||||
|
||||
**`内存[基址 + 偏移] = 240 + 码点 // 262144`**
|
||||
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 4096 % 64`**
|
||||
|
||||
**`内存[基址 + 偏移 + 2] = 128 + 码点 // 64 % 64`**
|
||||
|
||||
**`内存[基址 + 偏移 + 3] = 128 + 码点 % 64`**
|
||||
|
||||
**偏移 = 偏移 + 4**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*偏移*
|
||||
|
||||
## 解码2
|
||||
|
||||
`(内存, 基址, 长度, 偏移, 文本)`
|
||||
|
||||
1. `偏移 + 1 >= 长度`
|
||||
|
||||
*[文本, 偏移, "utf8.truncated"]*
|
||||
|
||||
**b1 = 内存[基址 + 偏移 + 1]**
|
||||
|
||||
1. `not 续字节(b1)`
|
||||
|
||||
*[文本, 偏移, "utf8.invalid_continuation"]*
|
||||
|
||||
**码点 = (内存[基址 + 偏移] - 192) * 64 + (b1 - 128)**
|
||||
|
||||
1. `码点 < 128`
|
||||
|
||||
*[文本, 偏移, "utf8.invalid_scalar"]*
|
||||
|
||||
*[文本 + text.from_codepoint(码点), 偏移 + 2, ""]*
|
||||
|
||||
## 解码3
|
||||
|
||||
`(内存, 基址, 长度, 偏移, 文本)`
|
||||
|
||||
1. `偏移 + 2 >= 长度`
|
||||
|
||||
*[文本, 偏移, "utf8.truncated"]*
|
||||
|
||||
**b1 = 内存[基址 + 偏移 + 1]**
|
||||
|
||||
**b2 = 内存[基址 + 偏移 + 2]**
|
||||
|
||||
1. `not (续字节(b1) and 续字节(b2))`
|
||||
|
||||
*[文本, 偏移, "utf8.invalid_continuation"]*
|
||||
|
||||
**码点 = (内存[基址 + 偏移] - 224) * 4096 + (b1 - 128) * 64 + (b2 - 128)**
|
||||
|
||||
1. `码点 < 2048 or (码点 >= 55296 and 码点 <= 57343)`
|
||||
|
||||
*[文本, 偏移, "utf8.invalid_scalar"]*
|
||||
|
||||
*[文本 + text.from_codepoint(码点), 偏移 + 3, ""]*
|
||||
|
||||
## 解码4
|
||||
|
||||
`(内存, 基址, 长度, 偏移, 文本)`
|
||||
|
||||
1. `偏移 + 3 >= 长度`
|
||||
|
||||
*[文本, 偏移, "utf8.truncated"]*
|
||||
|
||||
**b1 = 内存[基址 + 偏移 + 1]**
|
||||
|
||||
**b2 = 内存[基址 + 偏移 + 2]**
|
||||
|
||||
**b3 = 内存[基址 + 偏移 + 3]**
|
||||
|
||||
1. `not (续字节(b1) and 续字节(b2) and 续字节(b3))`
|
||||
|
||||
*[文本, 偏移, "utf8.invalid_continuation"]*
|
||||
|
||||
**码点 = (内存[基址 + 偏移] - 240) * 262144 + (b1 - 128) * 4096 + (b2 - 128) * 64 + (b3 - 128)**
|
||||
|
||||
1. `码点 < 65536 or 码点 > 1114111`
|
||||
|
||||
*[文本, 偏移, "utf8.invalid_scalar"]*
|
||||
|
||||
*[文本 + text.from_codepoint(码点), 偏移 + 4, ""]*
|
||||
|
||||
## 解码
|
||||
|
||||
`(内存, 基址, 长度)`
|
||||
|
||||
**偏移 = 0**
|
||||
|
||||
**文本 = ""**
|
||||
|
||||
**错误 = ""**
|
||||
|
||||
- `偏移 < 长度 and 错误 == ""`
|
||||
|
||||
**首 = 内存[基址 + 偏移]**
|
||||
|
||||
1. `首 < 128`
|
||||
|
||||
**文本 = 文本 + text.from_codepoint(首)**
|
||||
|
||||
**偏移 = 偏移 + 1**
|
||||
|
||||
2. `首 < 194`
|
||||
|
||||
**错误 = "utf8.invalid_lead"**
|
||||
|
||||
3. `首 < 224`
|
||||
|
||||
**结果 = 解码2(内存, 基址, 长度, 偏移, 文本)**
|
||||
|
||||
**文本 = 结果[0]**
|
||||
|
||||
**偏移 = 结果[1]**
|
||||
|
||||
**错误 = 结果[2]**
|
||||
|
||||
4. `首 < 240`
|
||||
|
||||
**结果 = 解码3(内存, 基址, 长度, 偏移, 文本)**
|
||||
|
||||
**文本 = 结果[0]**
|
||||
|
||||
**偏移 = 结果[1]**
|
||||
|
||||
**错误 = 结果[2]**
|
||||
|
||||
5. `首 < 245`
|
||||
|
||||
**结果 = 解码4(内存, 基址, 长度, 偏移, 文本)**
|
||||
|
||||
**文本 = 结果[0]**
|
||||
|
||||
**偏移 = 结果[1]**
|
||||
|
||||
**错误 = 结果[2]**
|
||||
|
||||
6. `true`
|
||||
|
||||
**错误 = "utf8.invalid_lead"**
|
||||
|
||||
1. `错误 == ""`
|
||||
|
||||
*成功(文本)*
|
||||
|
||||
2. `true`
|
||||
|
||||
*失败(错误, "UTF-8 解码失败")*
|
||||
|
||||
## utf8编码
|
||||
|
||||
`(文本, 内存, 基址)`
|
||||
|
||||
*编码(文本, 内存, 基址)*
|
||||
|
||||
## utf8解码
|
||||
|
||||
`(内存, 基址, 长度)`
|
||||
|
||||
*解码(内存, 基址, 长度)*
|
||||
Reference in new issue
Block a user