关于 domain-fintech
面向金融软件的 Rust 领域技能,聚焦精确十进制运算、不可变审计轨迹和交易一致性,指导开发者建模金额、账本操作与可追溯记录。
主要功能
- 01使用 Decimal 保证金额精度
- 02不可变交易与审计记录
- 03货币与金额类型建模
- 04双重记账账本模式
- 05交易校验与结构化日志
适用场景
- 01建模带币种的金额类型
- 02记录可追溯交易事件
- 03设计借贷平衡账本
- 04防止金额计算精度损失
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ actionbook/ rust-skills --skill domain-fintech安装后在 Claude Code 中输入/domain-fintech确认已加载。
详细介绍
金融科技领域
第 3 层:领域约束
领域约束 → 设计影响
| 领域规则 | 设计约束 | Rust 影响 |
|---|---|---|
| 审计追踪 | 不可变记录 | Arc,不进行修改 |
| 精度 | 不使用浮点数 | rust_decimal |
| 一致性 | 事务边界 | 明确所有权 |
| 合规性 | 完整日志记录 | 结构化追踪 |
| 可复现性 | 确定性执行 | 无竞态条件 |
关键约束
金融精度
RULE: Never use f64 for money
WHY: Floating point loses precision
RUST: Use rust_decimal::Decimal
审计要求
RULE: All transactions must be immutable and traceable
WHY: Regulatory compliance, dispute resolution
RUST: Arc<T> for sharing, event sourcing pattern
一致性
RULE: Money can't disappear or appear
WHY: Double-entry accounting principles
RUST: Transaction types with validated totals
向下追溯 ↓
从约束到设计(第 2 层):
"Need immutable transaction records"
↓ m09-domain: Model as Value Objects
↓ m01-ownership: Use Arc for shared immutable data
"Need precise decimal math"
↓ m05-type-driven: Newtype for Currency/Amount
↓ rust_decimal: Use Decimal type
"Need transaction boundaries"
↓ m12-lifecycle: RAII for transaction scope
↓ m09-domain: Aggregate boundaries
关键 Crate
| 用途 | Crate |
|---|---|
| 十进制运算 | rust_decimal |
| 日期/时间 | chrono, time |
| UUID | uuid |
| 序列化 | serde |
| 验证 | validator |
设计模式
| 模式 | 用途 | 实现方式 |
|---|---|---|
| 货币 newtype | 类型安全 | struct Amount(Decimal); |
| 事务 | 原子操作 | 事件溯源 |
| 审计日志 | 可追溯性 | 带追踪 ID 的结构化日志记录 |
| 账本 | 借贷记账 | 借方/贷方余额 |
代码模式:货币类型
use rust_decimal::Decimal;
#[derive(Clone, Debug, PartialEq)]
pub struct Amount {
value: Decimal,
currency: Currency,
}
impl Amount {
pub fn new(value: Decimal, currency: Currency) -> Self {
Self { value, currency }
}
pub fn add(&self, other: &Amount) -> Result<Amount, CurrencyMismatch> {
if self.currency != other.currency {
return Err(CurrencyMismatch);
}
Ok(Amount::new(self.value + other.value, self.currency))
}
}
常见错误
| 错误 | 违反的领域规则 | 修复方法 |
|---|---|---|
| 使用 f64 | 精度损失 | rust_decimal |
| 可变事务 | 审计追踪受损 | 不可变 + 事件 |
| 使用字符串表示金额 | 无法验证 | 经验证的 newtype |
| 溢出未处理 | 资金消失 | 检查算术运算 |
追溯到第 1 层
| 约束 | 第 2 层模式 | 第 1 层实现 |
|---|---|---|
| 不可变记录 | 事件溯源 | Arc,Clone |
| 事务作用域 | 聚合 | 拥有子项 |
| 精度 | 值对象 | rust_decimal newtype |
| 线程安全共享 | 共享不可变数据 | Arc(而非 Rc) |
相关技能
| 适用场景 | 参见 |
|---|---|
| 值对象设计 | m09-domain |
| 不可变数据的所有权 | m01-ownership |
| 使用 Arc 共享 | m02-resource |
| 错误处理 | m13-domain-error |
评论
支持 Markdown