rustkernel_banking/
types.rs

1//! Banking types and data structures.
2
3use std::collections::HashMap;
4
5// ============================================================================
6// Transaction Types
7// ============================================================================
8
9/// A financial transaction for fraud analysis.
10#[derive(Debug, Clone)]
11pub struct BankTransaction {
12    /// Transaction ID.
13    pub id: u64,
14    /// Source account ID.
15    pub source_account: u64,
16    /// Destination account ID.
17    pub dest_account: u64,
18    /// Transaction amount.
19    pub amount: f64,
20    /// Timestamp (Unix epoch seconds).
21    pub timestamp: u64,
22    /// Transaction type.
23    pub tx_type: TransactionType,
24    /// Channel (online, branch, ATM, etc.).
25    pub channel: Channel,
26    /// Optional merchant category code.
27    pub mcc: Option<u16>,
28    /// Optional location (country code).
29    pub location: Option<String>,
30}
31
32/// Transaction type.
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
34pub enum TransactionType {
35    /// Wire transfer.
36    Wire,
37    /// ACH transfer.
38    ACH,
39    /// Card payment.
40    Card,
41    /// Cash withdrawal.
42    CashWithdrawal,
43    /// Cash deposit.
44    CashDeposit,
45    /// Check.
46    Check,
47    /// Internal transfer.
48    Internal,
49    /// Cryptocurrency.
50    Crypto,
51}
52
53/// Transaction channel.
54#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
55pub enum Channel {
56    /// Online banking.
57    Online,
58    /// Mobile app.
59    Mobile,
60    /// Branch.
61    Branch,
62    /// ATM.
63    ATM,
64    /// Phone.
65    Phone,
66    /// API.
67    API,
68}
69
70// ============================================================================
71// Fraud Pattern Types
72// ============================================================================
73
74/// A fraud pattern definition.
75#[derive(Debug, Clone)]
76pub struct FraudPattern {
77    /// Pattern ID.
78    pub id: u32,
79    /// Pattern name.
80    pub name: String,
81    /// Pattern type.
82    pub pattern_type: FraudPatternType,
83    /// Risk score weight (0-100).
84    pub risk_weight: f64,
85    /// Pattern parameters.
86    pub params: PatternParams,
87}
88
89/// Type of fraud pattern.
90#[derive(Debug, Clone, Copy, PartialEq, Eq)]
91pub enum FraudPatternType {
92    /// Rapid succession of transactions (structuring).
93    RapidSplit,
94    /// Circular transaction flow.
95    CircularFlow,
96    /// Velocity anomaly (too many transactions).
97    VelocityAnomaly,
98    /// Amount anomaly (unusual amounts).
99    AmountAnomaly,
100    /// Geographic anomaly (impossible travel).
101    GeoAnomaly,
102    /// Time anomaly (unusual hours).
103    TimeAnomaly,
104    /// Account takeover indicators.
105    AccountTakeover,
106    /// Mule account behavior.
107    MuleAccount,
108    /// Layering (complex transaction chains).
109    Layering,
110}
111
112/// Pattern detection parameters.
113#[derive(Debug, Clone)]
114pub struct PatternParams {
115    /// Time window in seconds.
116    pub time_window: u64,
117    /// Minimum count threshold.
118    pub min_count: u32,
119    /// Amount threshold.
120    pub amount_threshold: f64,
121    /// Additional string patterns (for Aho-Corasick).
122    pub string_patterns: Vec<String>,
123    /// Custom parameters.
124    pub custom: HashMap<String, f64>,
125}
126
127impl Default for PatternParams {
128    fn default() -> Self {
129        Self {
130            time_window: 3600, // 1 hour
131            min_count: 3,
132            amount_threshold: 10000.0,
133            string_patterns: Vec::new(),
134            custom: HashMap::new(),
135        }
136    }
137}
138
139// ============================================================================
140// Detection Result Types
141// ============================================================================
142
143/// Result of fraud pattern detection.
144#[derive(Debug, Clone)]
145pub struct FraudDetectionResult {
146    /// Transaction ID that triggered detection.
147    pub transaction_id: u64,
148    /// Overall fraud score (0-100).
149    pub fraud_score: f64,
150    /// Matched patterns.
151    pub matched_patterns: Vec<PatternMatch>,
152    /// Risk level.
153    pub risk_level: RiskLevel,
154    /// Recommended action.
155    pub recommended_action: RecommendedAction,
156    /// Related transaction IDs.
157    pub related_transactions: Vec<u64>,
158}
159
160/// A pattern match.
161#[derive(Debug, Clone)]
162pub struct PatternMatch {
163    /// Pattern ID.
164    pub pattern_id: u32,
165    /// Pattern name.
166    pub pattern_name: String,
167    /// Match score (0-100).
168    pub score: f64,
169    /// Match details.
170    pub details: String,
171    /// Evidence transactions.
172    pub evidence: Vec<u64>,
173}
174
175/// Risk level classification.
176#[derive(Debug, Clone, Copy, PartialEq, Eq)]
177pub enum RiskLevel {
178    /// Low risk - normal processing.
179    Low,
180    /// Medium risk - flag for review.
181    Medium,
182    /// High risk - hold for investigation.
183    High,
184    /// Critical - block immediately.
185    Critical,
186}
187
188impl From<f64> for RiskLevel {
189    fn from(score: f64) -> Self {
190        match score {
191            s if s < 25.0 => RiskLevel::Low,
192            s if s < 50.0 => RiskLevel::Medium,
193            s if s < 75.0 => RiskLevel::High,
194            _ => RiskLevel::Critical,
195        }
196    }
197}
198
199/// Recommended action.
200#[derive(Debug, Clone, Copy, PartialEq, Eq)]
201pub enum RecommendedAction {
202    /// Allow transaction.
203    Allow,
204    /// Flag for review.
205    Review,
206    /// Hold pending investigation.
207    Hold,
208    /// Block transaction.
209    Block,
210    /// Block and alert.
211    BlockAndAlert,
212}
213
214impl From<RiskLevel> for RecommendedAction {
215    fn from(level: RiskLevel) -> Self {
216        match level {
217            RiskLevel::Low => RecommendedAction::Allow,
218            RiskLevel::Medium => RecommendedAction::Review,
219            RiskLevel::High => RecommendedAction::Hold,
220            RiskLevel::Critical => RecommendedAction::BlockAndAlert,
221        }
222    }
223}
224
225// ============================================================================
226// Account Profile Types
227// ============================================================================
228
229/// Account profile for behavioral baseline.
230#[derive(Debug, Clone)]
231pub struct AccountProfile {
232    /// Account ID.
233    pub account_id: u64,
234    /// Average transaction amount.
235    pub avg_amount: f64,
236    /// Standard deviation of amounts.
237    pub std_amount: f64,
238    /// Average transactions per day.
239    pub avg_daily_count: f64,
240    /// Typical transaction hours (0-23).
241    pub typical_hours: Vec<u8>,
242    /// Typical locations.
243    pub typical_locations: Vec<String>,
244    /// Account age in days.
245    pub account_age_days: u32,
246    /// Total historical transaction count.
247    pub total_transactions: u64,
248}
249
250impl Default for AccountProfile {
251    fn default() -> Self {
252        Self {
253            account_id: 0,
254            avg_amount: 500.0,
255            std_amount: 200.0,
256            avg_daily_count: 5.0,
257            typical_hours: (9..18).collect(),
258            typical_locations: vec!["US".to_string()],
259            account_age_days: 365,
260            total_transactions: 1000,
261        }
262    }
263}