domain-fintech by zhanghandong/rust-skills
npx skills add https://github.com/zhanghandong/rust-skills --skill domain-fintech第 3 层:领域约束
| 领域规则 | 设计约束 | Rust 实现含义 |
|---|---|---|
| 审计追踪 | 不可变记录 | Arc,无突变 |
| 精确性 | 不使用浮点数 | rust_decimal |
| 一致性 | 事务边界 | 清晰的所有权 |
| 合规性 | 完整日志记录 | 结构化追踪 |
| 可复现性 | 确定性执行 | 无竞态条件 |
RULE: Never use f64 for money
WHY: Floating point loses precision
RUST: Use rust_decimal::Decimal
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
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
| 用途 | 库 |
|---|---|
| 十进制数学运算 | rust_decimal |
| 日期/时间 | chrono, time |
| UUID | uuid |
| 序列化 | serde |
| 验证 | validator |
| 模式 | 目的 | 实现 |
|---|---|---|
| 货币新类型 | 类型安全 | 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 |
| 可变事务 | 审计追踪中断 | 不可变 + 事件 |
| 使用字符串表示金额 | 无验证 | 已验证的新类型 |
| 静默溢出 | 资金消失 | 检查算术运算 |
| 约束 | 第 2 层模式 | 第 1 层实现 |
|---|---|---|
| 不可变记录 | 事件溯源 | Arc, Clone |
| 事务范围 | 聚合 | 拥有的子对象 |
| 精度 | 值对象 | rust_decimal 新类型 |
| 线程安全共享 | 共享不可变 | Arc (非 Rc) |
| 何时使用 | 参见 |
|---|---|
| 值对象设计 | m09-domain |
| 不可变数据的所有权 | m01-ownership |
| 共享的 Arc | m02-resource |
| 错误处理 | m13-domain-error |
每周安装量
422
代码仓库
GitHub 星标数
920
首次出现
Jan 20, 2026
安全审计
安装于
opencode383
codex371
gemini-cli365
github-copilot355
amp318
kimi-cli314
Layer 3: Domain Constraints
| Domain Rule | Design Constraint | Rust Implication |
|---|---|---|
| Audit trail | Immutable records | Arc, no mutation |
| Precision | No floating point | rust_decimal |
| Consistency | Transaction boundaries | Clear ownership |
| Compliance | Complete logging | Structured tracing |
| Reproducibility | Deterministic execution | No race conditions |
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
From constraints to design (Layer 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
| Purpose | Crate |
|---|---|
| Decimal math | rust_decimal |
| Date/time | chrono, time |
| UUID | uuid |
| Serialization | serde |
| Validation | validator |
| Pattern | Purpose | Implementation |
|---|---|---|
| Currency newtype | Type safety | struct Amount(Decimal); |
| Transaction | Atomic operations | Event sourcing |
| Audit log | Traceability | Structured logging with trace IDs |
| Ledger | Double-entry | Debit/credit balance |
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))
}
}
| Mistake | Domain Violation | Fix |
|---|---|---|
| Using f64 | Precision loss | rust_decimal |
| Mutable transaction | Audit trail broken | Immutable + events |
| String for amount | No validation | Validated newtype |
| Silent overflow | Money disappears | Checked arithmetic |
| Constraint | Layer 2 Pattern | Layer 1 Implementation |
|---|---|---|
| Immutable records | Event sourcing | Arc, Clone |
| Transaction scope | Aggregate | Owned children |
| Precision | Value Object | rust_decimal newtype |
| Thread-safe sharing | Shared immutable | Arc (not Rc) |
| When | See |
|---|---|
| Value Object design | m09-domain |
| Ownership for immutable | m01-ownership |
| Arc for sharing | m02-resource |
| Error handling | m13-domain-error |
Weekly Installs
422
Repository
GitHub Stars
920
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode383
codex371
gemini-cli365
github-copilot355
amp318
kimi-cli314
Rust性能优化指南:m10-performance技能详解,包含算法、数据结构与内存优化策略
704 周安装