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
// Copyright (c) 2026 Kirky.X🌠
// SPDX-License-Identifier: MIT

//! FX 场景化计算纯函数:留学预算 + 跨境定价,汇率表由调用方注入。

use crate::core::CalcError;
use crate::math::fx::{RateTable, convert};

/// 留学预算计算结果。
#[derive(Clone, Debug, PartialEq)]
pub struct BudgetResult {
    /// 外币学费原值。
    pub tuition_foreign: f64,
    /// 学费货币代码。
    pub tuition_currency: String,
    /// 学费换算为本币金额。
    pub tuition_home: f64,
    /// 本币代码。
    pub home_currency: String,
    /// 汇率(1 单位外币 = ? 本币)。
    pub rate: f64,
    /// 留学年限。
    pub duration_years: u32,
    /// 年均学费(本币)。
    pub annual_tuition_home: f64,
    /// 月生活费(外币),None 表示未提供。
    pub living_monthly_foreign: Option<f64>,
    /// 月生活费(本币),None 表示未提供。
    pub living_monthly_home: Option<f64>,
    /// 总生活费(本币),None 表示未提供。
    pub total_living_home: Option<f64>,
    /// 总费用(学费 + 生活费,本币)。None 表示无生活费数据。
    pub total_cost_home: Option<f64>,
    /// 汇率风险区间(±3%)。
    pub exchange_risk: ExchangeRisk,
}

/// 汇率风险区间。
#[derive(Clone, Debug, PartialEq)]
pub struct ExchangeRisk {
    /// 下限(本币)。
    pub low: f64,
    /// 上限(本币)。
    pub high: f64,
    /// 本币代码。
    pub currency: String,
}

/// 跨境定价单项结果。
#[derive(Clone, Debug, PartialEq)]
pub struct PricingItem {
    /// 目标货币代码。
    pub currency: String,
    /// 建议售价(外币)。
    pub recommended_price: f64,
    /// 实际利润率(回算后)。
    pub actual_profit_rate: f64,
    /// 利润(本币)。
    pub profit_cny: f64,
    /// 平台费(本币)。
    pub platform_fee_cny: f64,
}

/// 跨境定价计算结果。
#[derive(Clone, Debug, PartialEq)]
pub struct PricingResult {
    /// 人民币成本。
    pub cost_cny: f64,
    /// 各币种定价明细。
    pub pricing: Vec<PricingItem>,
}

/// 留学预算计算:外币学费/生活费换算本币,算总费用与 ±3% 汇率风险区间。
pub fn budget_calculation(
    tuition: f64,
    tuition_currency: &str,
    living_cost_monthly: Option<f64>,
    duration_years: u32,
    home_currency: &str,
    table: &RateTable,
) -> Result<BudgetResult, CalcError> {
    // 输入校验
    if tuition <= 0.0 {
        return Err(CalcError::domain("tuition must be positive".to_string())
            .with_i18n("msg.fxscenario.tuition_positive", vec![]));
    }
    if duration_years == 0 || duration_years > 10 {
        return Err(
            CalcError::domain("duration_years must be between 1 and 10".to_string())
                .with_i18n("msg.fxscenario.duration_range", vec![]),
        );
    }

    // 汇率:1 单位 tuition_currency = ? home_currency
    let rate = convert(1.0, tuition_currency, home_currency, table)?;

    // 学费换算
    let tuition_home = convert(tuition, tuition_currency, home_currency, table)?;
    let annual_tuition_home = tuition_home / duration_years as f64;

    // 生活费换算
    let (living_monthly_home, total_living_home, total_cost_home) = match living_cost_monthly {
        Some(monthly) if monthly > 0.0 => {
            let monthly_home = convert(monthly, tuition_currency, home_currency, table)?;
            let total_living = monthly_home * 12.0 * duration_years as f64;
            let total = tuition_home + total_living;
            (Some(monthly_home), Some(total_living), Some(total))
        }
        _ => (None, None, None),
    };

    // ±3% 汇率风险区间(基于总费用或仅学费)
    let base_for_risk = total_cost_home.unwrap_or(tuition_home);
    let exchange_risk = ExchangeRisk {
        low: base_for_risk * 0.97,
        high: base_for_risk * 1.03,
        currency: home_currency.to_string(),
    };

    Ok(BudgetResult {
        tuition_foreign: tuition,
        tuition_currency: tuition_currency.to_string(),
        tuition_home,
        home_currency: home_currency.to_string(),
        rate,
        duration_years,
        annual_tuition_home,
        living_monthly_foreign: living_cost_monthly,
        living_monthly_home,
        total_living_home,
        total_cost_home,
        exchange_risk,
    })
}

/// 跨境多币种定价:按成本、目标利润率与平台费率算安全售价并回算实际利润率。
pub fn pricing_calculation(
    cost_cny: f64,
    target_profit_rate: f64,
    currencies: &[&str],
    platform_rate: f64,
    safety_buffer: f64,
    table: &RateTable,
) -> Result<PricingResult, CalcError> {
    // 输入校验
    if cost_cny <= 0.0 {
        return Err(CalcError::domain("cost_cny must be positive".to_string())
            .with_i18n("msg.fxscenario.cost_positive", vec![]));
    }
    if target_profit_rate <= 0.0 || target_profit_rate >= 1.0 {
        return Err(CalcError::domain(
            "target_profit_rate must be between 0 and 1 (exclusive)".to_string(),
        )
        .with_i18n("msg.fxscenario.profit_rate_range", vec![]));
    }
    if !(0.0..1.0).contains(&platform_rate) {
        return Err(
            CalcError::domain("platform_rate must be in [0, 1)".to_string())
                .with_i18n("msg.fxscenario.platform_rate_range", vec![]),
        );
    }
    if !(0.0..=0.5).contains(&safety_buffer) {
        return Err(
            CalcError::domain("safety_buffer must be in [0, 0.5]".to_string())
                .with_i18n("msg.fxscenario.safety_buffer_range", vec![]),
        );
    }
    if currencies.is_empty() {
        return Err(
            CalcError::domain("currencies list must not be empty".to_string())
                .with_i18n("msg.fxscenario.currencies_empty", vec![]),
        );
    }
    if currencies.len() > 10 {
        return Err(
            CalcError::domain("currencies list must not exceed 10 items".to_string())
                .with_i18n("msg.fxscenario.currencies_too_many", vec![]),
        );
    }

    let mut pricing = Vec::with_capacity(currencies.len());

    for &cur in currencies {
        // 1 外币 = ? CNY(经 base 三角换算)
        let one_foreign_in_cny = convert(1.0, cur, "CNY", table)?;

        // 安全汇率(考虑缓冲):1外币 = ?CNY(打折后)
        let safe_rate = one_foreign_in_cny * (1.0 - safety_buffer);

        // 目标收入(CNY) = 成本 / (1 - 目标利润率)
        let target_revenue_cny = cost_cny / (1.0 - target_profit_rate);

        // 售价(外币) = 目标收入CNY / 安全汇率 / (1 - 平台费率)
        let price_foreign = target_revenue_cny / safe_rate / (1.0 - platform_rate);

        // 回算验证(用实时汇率,非安全汇率)
        let gross_cny = price_foreign * one_foreign_in_cny;
        let platform_fee_cny = gross_cny * platform_rate;
        let net_cny = gross_cny - platform_fee_cny;
        let profit_cny = net_cny - cost_cny;
        let actual_profit_rate = profit_cny / cost_cny;

        pricing.push(PricingItem {
            currency: cur.to_string(),
            recommended_price: (price_foreign * 100.0).round() / 100.0, // 保留2位小数
            actual_profit_rate: (actual_profit_rate * 10000.0).round() / 10000.0, // 4位
            profit_cny: (profit_cny * 100.0).round() / 100.0,
            platform_fee_cny: (platform_fee_cny * 100.0).round() / 100.0,
        });
    }

    Ok(PricingResult { cost_cny, pricing })
}

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

    /// 测试用 mock 汇率表:EUR-base,含 USD/CNY/GBP/JPY。
    fn mock_table() -> RateTable {
        let mut rates = HashMap::new();
        rates.insert("USD".to_string(), 1.08);
        rates.insert("CNY".to_string(), 7.85);
        rates.insert("GBP".to_string(), 0.85);
        rates.insert("JPY".to_string(), 165.0);
        RateTable {
            base: "EUR".to_string(),
            date: "2026-08-10".to_string(),
            rates,
            source: "frankfurter".to_string(),
        }
    }

    // ===== budget_calculation() 测试 =====

    #[test]
    fn test_budget_happy_path_with_living() {
        // 50000 USD 学费 + 2000 USD/月生活费,4 年,换算为 CNY
        let table = mock_table();
        let result = budget_calculation(50000.0, "USD", Some(2000.0), 4, "CNY", &table).unwrap();

        // 学费换算
        let expected_rate = 1.0 / 1.08 * 7.85;
        assert!((result.rate - expected_rate).abs() < 1e-6, "rate mismatch");
        let expected_tuition_home = 50000.0 / 1.08 * 7.85;
        assert!(
            (result.tuition_home - expected_tuition_home).abs() < 0.01,
            "tuition_home mismatch"
        );

        // 年均学费
        assert!((result.annual_tuition_home - expected_tuition_home / 4.0).abs() < 0.01);

        // 生活费
        let expected_monthly_home = 2000.0 / 1.08 * 7.85;
        assert!((result.living_monthly_home.unwrap() - expected_monthly_home).abs() < 0.01);
        let expected_total_living = expected_monthly_home * 12.0 * 4.0;
        assert!((result.total_living_home.unwrap() - expected_total_living).abs() < 0.01);

        // 总费用
        let expected_total = expected_tuition_home + expected_total_living;
        assert!((result.total_cost_home.unwrap() - expected_total).abs() < 0.01);

        // ±3% 风险区间
        assert!((result.exchange_risk.low - expected_total * 0.97).abs() < 0.01);
        assert!((result.exchange_risk.high - expected_total * 1.03).abs() < 0.01);
        assert_eq!(result.exchange_risk.currency, "CNY");
    }

    #[test]
    fn test_budget_tuition_only_no_living() {
        let table = mock_table();
        let result = budget_calculation(10000.0, "USD", None, 1, "CNY", &table).unwrap();

        assert!(result.living_monthly_home.is_none());
        assert!(result.total_living_home.is_none());
        assert!(result.total_cost_home.is_none());

        // 风险区间基于仅学费
        let tuition_home = 10000.0 / 1.08 * 7.85;
        assert!((result.exchange_risk.low - tuition_home * 0.97).abs() < 0.01);
    }

    #[test]
    fn test_budget_same_currency_identity() {
        let table = mock_table();
        let result = budget_calculation(50000.0, "USD", None, 2, "USD", &table).unwrap();

        assert!(
            (result.rate - 1.0).abs() < 1e-9,
            "same currency rate should be 1.0"
        );
        assert!((result.tuition_home - 50000.0).abs() < 1e-9);
        assert!((result.annual_tuition_home - 25000.0).abs() < 1e-9);
    }

    #[test]
    fn test_budget_unknown_currency_error() {
        let table = mock_table();
        let result = budget_calculation(50000.0, "XYZ", None, 2, "CNY", &table);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.kind, crate::core::ErrorKind::Domain);
    }

    #[test]
    fn test_budget_zero_duration_error() {
        let table = mock_table();
        let result = budget_calculation(50000.0, "USD", None, 0, "CNY", &table);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.kind, crate::core::ErrorKind::Domain);
    }

    #[test]
    fn test_budget_zero_tuition_error() {
        let table = mock_table();
        let result = budget_calculation(0.0, "USD", None, 2, "CNY", &table);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.kind, crate::core::ErrorKind::Domain);
    }

    #[test]
    fn test_budget_duration_exceeds_max_error() {
        let table = mock_table();
        let result = budget_calculation(50000.0, "USD", None, 11, "CNY", &table);
        assert!(result.is_err());
    }

    // ===== pricing_calculation() 测试 =====

    #[test]
    fn test_pricing_happy_path_single_currency() {
        let table = mock_table();
        let result = pricing_calculation(50.0, 0.3, &["USD"], 0.15, 0.05, &table).unwrap();

        assert_eq!(result.cost_cny, 50.0);
        assert_eq!(result.pricing.len(), 1);

        let item = &result.pricing[0];
        assert_eq!(item.currency, "USD");
        assert!(item.recommended_price > 0.0);
        // 实际利润率应接近目标(因安全缓冲略高)
        assert!(
            item.actual_profit_rate >= 0.3,
            "actual profit rate {} should be >= target 0.3 (safety buffer)",
            item.actual_profit_rate
        );
        assert!(item.profit_cny > 0.0);
        assert!(item.platform_fee_cny > 0.0);
    }

    #[test]
    fn test_pricing_multi_currency() {
        let table = mock_table();
        let result =
            pricing_calculation(100.0, 0.25, &["USD", "EUR", "GBP"], 0.10, 0.05, &table).unwrap();

        assert_eq!(result.pricing.len(), 3);
        for item in &result.pricing {
            assert!(item.recommended_price > 0.0);
            assert!(item.profit_cny > 0.0);
        }
    }

    #[test]
    fn test_pricing_zero_cost_error() {
        let table = mock_table();
        let result = pricing_calculation(0.0, 0.3, &["USD"], 0.15, 0.05, &table);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().kind, crate::core::ErrorKind::Domain);
    }

    #[test]
    fn test_pricing_negative_cost_error() {
        let table = mock_table();
        let result = pricing_calculation(-10.0, 0.3, &["USD"], 0.15, 0.05, &table);
        assert!(result.is_err());
    }

    #[test]
    fn test_pricing_profit_rate_zero_error() {
        let table = mock_table();
        let result = pricing_calculation(50.0, 0.0, &["USD"], 0.15, 0.05, &table);
        assert!(result.is_err());
    }

    #[test]
    fn test_pricing_profit_rate_one_error() {
        let table = mock_table();
        let result = pricing_calculation(50.0, 1.0, &["USD"], 0.15, 0.05, &table);
        assert!(result.is_err());
    }

    #[test]
    fn test_pricing_empty_currencies_error() {
        let table = mock_table();
        let result = pricing_calculation(50.0, 0.3, &[], 0.15, 0.05, &table);
        assert!(result.is_err());
    }

    #[test]
    fn test_pricing_too_many_currencies_error() {
        let table = mock_table();
        let currencies: Vec<&str> = (0..11).map(|_| "USD").collect();
        let result = pricing_calculation(50.0, 0.3, &currencies, 0.15, 0.05, &table);
        assert!(result.is_err());
    }

    #[test]
    fn test_pricing_unknown_currency_error() {
        let table = mock_table();
        let result = pricing_calculation(50.0, 0.3, &["XYZ"], 0.15, 0.05, &table);
        assert!(result.is_err());
    }

    #[test]
    fn test_pricing_zero_platform_rate() {
        let table = mock_table();
        let result = pricing_calculation(50.0, 0.3, &["USD"], 0.0, 0.05, &table).unwrap();
        assert_eq!(result.pricing[0].platform_fee_cny, 0.0);
    }

    #[test]
    fn test_pricing_zero_safety_buffer() {
        let table = mock_table();
        let result = pricing_calculation(50.0, 0.3, &["USD"], 0.15, 0.0, &table).unwrap();
        // 无缓冲时售价应低于有缓冲时(更安全但利润更紧)
        assert!(result.pricing[0].recommended_price > 0.0);
    }
}