Rename the CLI crate and executable to cli
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "cli"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
exchange = { path = "../exchange" }
|
||||
serde_json.workspace = true
|
||||
anyhow.workspace = true
|
||||
@@ -0,0 +1,37 @@
|
||||
# cli
|
||||
|
||||
可信操作员的 JSON 行命令入口。启动参数指定数据库文件,程序复用同一个 Exchange 连接。标准输入的每一行对应标准输出的一条 JSON 回执。输入结束时进程退出。
|
||||
|
||||
## 文件依赖
|
||||
|
||||
箭头从调用文件指向提供功能的文件或 crate。
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
main["src/main.rs"] -->|把标准输入输出交给 serve| stream["src/stream.rs"]
|
||||
main -.->|打开数据库| api["exchange crate"]
|
||||
stream -.->|执行命令| api
|
||||
```
|
||||
|
||||
## 一次命令会话
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
actor U as 操作员
|
||||
participant C as main.rs
|
||||
participant S as stream.rs
|
||||
participant E as Exchange
|
||||
U->>C: 启动并指定数据库路径
|
||||
C->>E: 打开数据库
|
||||
C->>S: 传入连接和输入输出流
|
||||
loop 输入一行
|
||||
U->>S: JSON 命令
|
||||
alt JSON 有效
|
||||
S->>E: 执行命令
|
||||
E-->>S: 业务回执或错误
|
||||
else JSON 解析失败
|
||||
S->>S: 构造错误回执
|
||||
end
|
||||
S-->>U: 输出一行 JSON 并刷新
|
||||
end
|
||||
```
|
||||
@@ -0,0 +1,13 @@
|
||||
mod stream;
|
||||
|
||||
use anyhow::{ensure, Context, Result};
|
||||
use exchange::Exchange;
|
||||
use std::{env, io};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut args = env::args_os().skip(1);
|
||||
let path = args.next().context("usage: cli DATABASE")?;
|
||||
ensure!(args.next().is_none(), "usage: cli DATABASE");
|
||||
let mut exchange = Exchange::open(path)?;
|
||||
stream::serve(&mut exchange, io::stdin().lock(), io::stdout().lock())
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
use anyhow::Result;
|
||||
use exchange::Exchange;
|
||||
use serde_json::{json, Value};
|
||||
use std::io::{BufRead, Write};
|
||||
|
||||
pub(crate) fn serve(
|
||||
engine: &mut Exchange,
|
||||
input: impl BufRead,
|
||||
mut output: impl Write,
|
||||
) -> Result<()> {
|
||||
for line in input.lines() {
|
||||
let line = line?;
|
||||
let result = serde_json::from_str::<Value>(&line)
|
||||
.map_err(anyhow::Error::from)
|
||||
.and_then(|v| engine.execute(v));
|
||||
let reply = result.unwrap_or_else(|e| json!({"error":e.to_string()}));
|
||||
serde_json::to_writer(&mut output, &reply)?;
|
||||
writeln!(output)?;
|
||||
output.flush()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn invalid_line_does_not_discard_next_request() {
|
||||
let mut e = Exchange::open(":memory:").unwrap();
|
||||
let mut out = Vec::new();
|
||||
serve(&mut e, &b"broken\n{\"op\":\"state\"}\n"[..], &mut out).unwrap();
|
||||
let replies: Vec<Value> = std::str::from_utf8(&out)
|
||||
.unwrap()
|
||||
.lines()
|
||||
.map(|line| serde_json::from_str(line).unwrap())
|
||||
.collect();
|
||||
assert_eq!(replies.len(), 2);
|
||||
assert!(replies[0]["error"].is_string());
|
||||
assert_eq!(replies[1]["orders"], json!([]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user