calnexus 0.2.0

A command-line math expression evaluator with 11 core and 3 optional (time/unit/fx) computation domains plus a numerical-linalg feature, symbolic calculus, REPL, batch processing, HTTP and MCP servers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Copyright (c) 2026 Kirky.X🌠
// SPDX-License-Identifier: MIT

//! 批量处理:从文件或 stdin 并行求值表达式(rayon)。
//!
//! 约束:单条 ≤ 4096 字符、总条数 ≤ 1000;超限返回错误并标明行号。

use crate::cli::format_result;
use crate::core::MAX_EXPR_LEN;
use crate::core::evaluate;
use crate::core::{EvalContext, EvalResult};
use crate::i18n::I18n;
use rayon::prelude::*;
use std::io::{self, BufRead, Read};
use std::time::Instant;

const MAX_BATCH_COUNT: usize = 1000;

/// 批量处理器。
pub struct BatchProcessor;

impl BatchProcessor {
    /// 执行批量求值(便捷入口,默认缓存)。
    ///
    /// 返回退出码:0=全部成功,1=部分失败,2=系统错误。
    #[allow(dead_code)] // 纯 build(非 --all-targets)下仅测试使用
    pub fn run(path: &str, ctx: &EvalContext, json: bool, i18n: &I18n) -> i32 {
        Self::run_with_cache(path, ctx, json, i18n, crate::CacheManager::new())
    }

    /// 以注入缓存运行批量求值(CLI `--cache-size` 预算)。
    pub fn run_with_cache(
        path: &str,
        ctx: &EvalContext,
        json: bool,
        i18n: &I18n,
        cache: crate::CacheManager,
    ) -> i32 {
        let start = Instant::now();

        let entries = match read_and_validate_entries(path, i18n) {
            Ok(e) => e,
            Err(code) => return code,
        };

        let results = evaluate_entries(&entries, ctx, &cache);

        output_results(&results, json, i18n);
        print_summary(&results, start.elapsed(), i18n);

        let err_count = results.iter().filter(|r| r.result.is_err()).count();
        if err_count > 0 { 1 } else { 0 }
    }
}

/// 读取并验证批量条目:跳过注释/空行,校验长度与数量上限;出错返回 `Err(2)`。
fn read_and_validate_entries(path: &str, i18n: &I18n) -> Result<Vec<BatchEntry>, i32> {
    let lines = match read_lines(path) {
        Ok(lines) => lines,
        Err(e) => {
            eprintln!(
                "{}",
                i18n.tf("batch.read_failed", &[("error", &e.to_string())])
            );
            return Err(2);
        }
    };

    let mut entries: Vec<BatchEntry> = Vec::new();
    for (line_no, raw) in lines.iter() {
        let trimmed = raw.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }
        if trimmed.len() > MAX_EXPR_LEN {
            eprintln!(
                "{}",
                i18n.tf(
                    "batch.line_too_long",
                    &[
                        ("line", &line_no.to_string()),
                        ("max", &MAX_EXPR_LEN.to_string()),
                        ("actual", &trimmed.len().to_string())
                    ]
                )
            );
            return Err(2);
        }
        entries.push(BatchEntry {
            line_no: *line_no,
            expr: trimmed.to_string(),
        });
    }

    if entries.is_empty() {
        eprintln!("{}", i18n.t("batch.no_expressions"));
        return Err(2);
    }
    if entries.len() > MAX_BATCH_COUNT {
        eprintln!(
            "{}",
            i18n.tf(
                "batch.count_exceeds",
                &[
                    ("count", &entries.len().to_string()),
                    ("max", &MAX_BATCH_COUNT.to_string())
                ]
            )
        );
        return Err(2);
    }

    Ok(entries)
}

/// 并行求值所有条目:每个表达式独立走全链路,结果顺序与输入一致。
fn evaluate_entries(
    entries: &[BatchEntry],
    ctx: &EvalContext,
    cache: &crate::CacheManager,
) -> Vec<BatchResult> {
    entries
        .par_iter()
        .map(|entry| {
            let result = evaluate(&entry.expr, ctx, None, cache);
            BatchResult {
                line_no: entry.line_no,
                expr: entry.expr.clone(),
                result,
            }
        })
        .collect()
}

/// 输出结果:JSON 数组或文本行,保持原始顺序(JSON 键名保留英文,DP-4 契约)。
fn output_results(results: &[BatchResult], json: bool, i18n: &I18n) {
    if json {
        // serde_json 统一构造:转义由 serde_json 处理,控制字符/引号安全
        let mut items: Vec<String> = Vec::with_capacity(results.len());
        for r in results {
            let entry = match &r.result {
                Ok((result, domain, hit, fmt_prec)) => {
                    let value = format_result(result, *fmt_prec);
                    serde_json::json!({
                        "line": r.line_no,
                        "expr": r.expr,
                        "result": value,
                        "domain": domain,
                        "cache": if *hit { "hit" } else { "miss" },
                    })
                }
                Err(e) => serde_json::json!({
                    "line": r.line_no,
                    "expr": r.expr,
                    "error": e.to_string(),
                }),
            };
            items.push(entry.to_string());
        }
        println!("[");
        println!("{}", items.join(",\n"));
        println!("]");
    } else {
        for r in results {
            match &r.result {
                Ok((result, domain, hit, fmt_prec)) => {
                    let value = format_result(result, *fmt_prec);
                    let cached_str = if *hit {
                        i18n.t("label.cached_suffix").to_string()
                    } else {
                        String::new()
                    };
                    println!(
                        "{}",
                        i18n.tf(
                            "batch.text_result",
                            &[
                                ("line", &r.line_no.to_string()),
                                ("expr", &r.expr),
                                ("value", &value),
                                ("domain", domain),
                                ("cached", &cached_str)
                            ]
                        )
                    );
                }
                Err(e) => {
                    eprintln!(
                        "{}",
                        i18n.tf(
                            "batch.text_error",
                            &[
                                ("line", &r.line_no.to_string()),
                                ("expr", &r.expr),
                                ("error", &e.localized_display(i18n))
                            ]
                        )
                    );
                }
            }
        }
    }
}

/// 打印汇总统计到 stderr:总数、成功、错误、缓存命中、耗时。
fn print_summary(results: &[BatchResult], elapsed: std::time::Duration, i18n: &I18n) {
    let total = results.len();
    let ok_count = results.iter().filter(|r| r.result.is_ok()).count();
    let err_count = total - ok_count;
    let cache_hits = results
        .iter()
        .filter(|r| {
            r.result
                .as_ref()
                .map(|(_, _, hit, _)| *hit)
                .unwrap_or(false)
        })
        .count();
    eprintln!(
        "{}",
        i18n.tf(
            "batch.summary",
            &[
                ("total", &total.to_string()),
                ("ok", &ok_count.to_string()),
                ("errors", &err_count.to_string()),
                ("hits", &cache_hits.to_string()),
                ("elapsed", &format!("{:?}", elapsed))
            ]
        )
    );
}

/// 批量条目:解析后的表达式。
struct BatchEntry {
    line_no: usize,
    expr: String,
}

/// 批量求值结果。
struct BatchResult {
    line_no: usize,
    expr: String,
    result: Result<(EvalResult, String, bool, Option<usize>), crate::CalcError>,
}

/// 读取文件或 stdin 的行,返回 (行号, 原始行) 列表,行号从 1 开始。
fn read_lines(path: &str) -> io::Result<Vec<(usize, String)>> {
    let mut lines: Vec<(usize, String)> = Vec::new();
    if path == "-" {
        let mut input = String::new();
        io::stdin().read_to_string(&mut input)?;
        for (i, line) in input.lines().enumerate() {
            // 剥离 UTF-8 BOM(仅首行)
            let line = if i == 0 {
                line.strip_prefix('\u{FEFF}').unwrap_or(line)
            } else {
                line
            };
            lines.push((i + 1, line.to_string()));
        }
    } else {
        let file = std::fs::File::open(path)?;
        let reader = io::BufReader::new(file);
        for (i, line) in reader.lines().enumerate() {
            let line = line?;
            // 剥离 UTF-8 BOM(仅首行)
            let line = if i == 0 {
                line.strip_prefix('\u{FEFF}').unwrap_or(&line).to_string()
            } else {
                line
            };
            lines.push((i + 1, line));
        }
    }
    Ok(lines)
}

// ============================ 单元测试 ============================

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    #[test]
    fn test_read_lines_from_file() {
        let mut tmp = tempfile::NamedTempFile::new().unwrap();
        writeln!(tmp, "2+3").unwrap();
        writeln!(tmp, "# comment").unwrap();
        writeln!(tmp, "sin(0)").unwrap();
        tmp.flush().unwrap();

        let lines = read_lines(tmp.path().to_str().unwrap()).unwrap();
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[0].0, 1);
        assert_eq!(lines[0].1, "2+3");
        assert_eq!(lines[2].1, "sin(0)");
    }

    #[test]
    fn test_batch_json_output_is_valid_json() {
        // JSON 输出统一走 serde_json,控制字符由库正确转义;
        // 输出整体必须是合法 JSON(round-trip 校验)。
        let sample = serde_json::json!({
            "line": 1,
            "expr": "a\u{0001}b\"c",
            "result": "1",
            "domain": "arithmetic",
            "cache": "miss",
        });
        let serialized = sample.to_string();
        let round: serde_json::Value =
            serde_json::from_str(&serialized).expect("序列化后必须可反序列化");
        assert_eq!(round["expr"], "a\u{0001}b\"c", "控制字符与引号应无损往返");
    }

    #[test]
    fn test_batch_run_basic_expressions() {
        let mut tmp = tempfile::NamedTempFile::new().unwrap();
        writeln!(tmp, "2+3").unwrap();
        writeln!(tmp, "4*5").unwrap();
        tmp.flush().unwrap();

        let ctx = EvalContext::new();
        let code = BatchProcessor::run(tmp.path().to_str().unwrap(), &ctx, false, &I18n::default());
        assert_eq!(code, 0);
    }

    #[test]
    fn test_batch_run_comment_skipped() {
        let mut tmp = tempfile::NamedTempFile::new().unwrap();
        writeln!(tmp, "# this is a comment").unwrap();
        writeln!(tmp).unwrap();
        writeln!(tmp, "1+1").unwrap();
        tmp.flush().unwrap();

        let ctx = EvalContext::new();
        let code = BatchProcessor::run(tmp.path().to_str().unwrap(), &ctx, false, &I18n::default());
        assert_eq!(code, 0);
    }

    #[test]
    fn test_batch_run_empty_file_rejected() {
        let mut tmp = tempfile::NamedTempFile::new().unwrap();
        writeln!(tmp, "# only comment").unwrap();
        tmp.flush().unwrap();

        let ctx = EvalContext::new();
        let code = BatchProcessor::run(tmp.path().to_str().unwrap(), &ctx, false, &I18n::default());
        assert_eq!(code, 2);
    }

    #[test]
    fn test_batch_run_with_error() {
        let mut tmp = tempfile::NamedTempFile::new().unwrap();
        writeln!(tmp, "2+3").unwrap();
        writeln!(tmp, "2++3").unwrap(); // 语法错误
        writeln!(tmp, "4*5").unwrap();
        tmp.flush().unwrap();

        let ctx = EvalContext::new();
        let code = BatchProcessor::run(tmp.path().to_str().unwrap(), &ctx, false, &I18n::default());
        assert_eq!(code, 1);
    }

    #[test]
    fn test_batch_run_json_output() {
        let mut tmp = tempfile::NamedTempFile::new().unwrap();
        writeln!(tmp, "2+3").unwrap();
        tmp.flush().unwrap();

        let ctx = EvalContext::new();
        let code = BatchProcessor::run(tmp.path().to_str().unwrap(), &ctx, true, &I18n::default());
        assert_eq!(code, 0);
    }

    #[test]
    fn test_batch_run_order_preserved() {
        let mut tmp = tempfile::NamedTempFile::new().unwrap();
        writeln!(tmp, "1").unwrap();
        writeln!(tmp, "2").unwrap();
        writeln!(tmp, "3").unwrap();
        tmp.flush().unwrap();

        // 即使并行求值,输出应保持原始顺序
        let ctx = EvalContext::new();
        let _code =
            BatchProcessor::run(tmp.path().to_str().unwrap(), &ctx, false, &I18n::default());
        // 验证不 panic 即可(顺序由 par_iter + collect 保证)
    }

    #[test]
    fn test_batch_nonexistent_file() {
        let ctx = EvalContext::new();
        let code = BatchProcessor::run(
            "/nonexistent/path/to/file.txt",
            &ctx,
            false,
            &I18n::default(),
        );
        assert_eq!(code, 2);
    }

    #[test]
    fn test_batch_line_exceeds_max_length() {
        let mut tmp = tempfile::NamedTempFile::new().unwrap();
        let long_line = "1+".repeat(2049); // 4098 字符,超过 4096
        writeln!(tmp, "{}", long_line).unwrap();
        tmp.flush().unwrap();

        let ctx = EvalContext::new();
        let code = BatchProcessor::run(tmp.path().to_str().unwrap(), &ctx, false, &I18n::default());
        assert_eq!(code, 2);
    }

    #[test]
    fn test_batch_count_exceeds_maximum() {
        let mut tmp = tempfile::NamedTempFile::new().unwrap();
        for _ in 0..1001 {
            writeln!(tmp, "1+1").unwrap();
        }
        tmp.flush().unwrap();

        let ctx = EvalContext::new();
        let code = BatchProcessor::run(tmp.path().to_str().unwrap(), &ctx, false, &I18n::default());
        assert_eq!(code, 2);
    }

    #[test]
    fn test_batch_json_output_with_error() {
        // 覆盖 JSON Err 分支
        let mut tmp = tempfile::NamedTempFile::new().unwrap();
        writeln!(tmp, "2+3").unwrap();
        writeln!(tmp, "2++3").unwrap(); // 语法错误
        tmp.flush().unwrap();

        let ctx = EvalContext::new();
        let code = BatchProcessor::run(tmp.path().to_str().unwrap(), &ctx, true, &I18n::default());
        assert_eq!(code, 1);
    }
}