Skip to main content

credit_facility_rs/
types.rs

1use chrono::{DateTime, Utc};
2use rust_decimal::Decimal;
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6use crate::decimal::{Money, Rate};
7
8/// unique identifier for a facility
9pub type FacilityId = Uuid;
10
11/// term loan types
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
13pub enum TermLoanType {
14    Mortgage,
15    PersonalLoan,
16    AutoLoan,
17    StudentLoan,
18    BusinessLoan,
19}
20
21/// open-term loan types
22#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23pub enum OpenTermType {
24    BitcoinBacked,
25    AssetBacked,
26}
27
28/// revolving facility types
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
30pub enum RevolvingType {
31    CreditCard,
32    LineOfCredit,
33    HELOC,
34}
35
36/// facility status
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
38pub enum FacilityStatus {
39    /// loan created but not yet disbursed
40    Originated,
41    /// loan active and performing
42    Active,
43    /// payment missed but within grace period
44    GracePeriod,
45    /// past grace period, late fees apply
46    Delinquent,
47    /// seriously delinquent or covenant breach
48    Default,
49    /// liquidation in process
50    Liquidating,
51    /// collateral sold
52    Liquidated,
53    /// fully paid off
54    Settled,
55    /// written off as loss
56    ChargedOff,
57}
58
59/// amortization method for term loans
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
61pub enum AmortizationMethod {
62    /// each payment reduces principal, interest on remaining
63    DecliningPrincipal,
64    /// equal payment amounts throughout term
65    EqualInstallments,
66    /// pay interest only, principal at maturity
67    InterestOnly,
68}
69
70
71/// payment schedule
72#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
73pub enum PaymentSchedule {
74    /// monthly payment on specific day
75    Monthly { day_of_month: u8 },
76    /// weekly payment on specific day
77    Weekly { day_of_week: u8 },
78    /// daily payments
79    Daily,
80    /// no fixed schedule, pay on demand
81    OnDemand,
82    /// interest only payments
83    InterestOnly { payment_day: u8 },
84    /// no payment required (open-term loans)
85    None,
86}
87
88/// overpayment application method
89#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
90pub enum OverpaymentStrategy {
91    /// reduce future payment amounts
92    ReduceEmi,
93    /// reduce loan term
94    ReduceTerm,
95    /// one-time principal reduction
96    ReducePrincipal,
97    /// reduce credit limit (revolving)
98    ReduceLimit,
99}
100
101/// collateral position
102#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
103pub struct CollateralPosition {
104    pub asset_type: String,
105    pub asset_amount: Decimal,
106    pub current_value: Money,
107    pub initial_value: Money,
108    pub last_valuation: DateTime<Utc>,
109    pub valuation_source: String,
110}
111
112/// ltv thresholds
113#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
114pub struct LtvThresholds {
115    pub initial_ltv: Rate,
116    pub warning_ltv: Rate,
117    pub margin_call_ltv: Rate,
118    pub liquidation_ltv: Rate,
119}
120
121/// ltv status
122#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
123pub enum LtvStatus {
124    Healthy,
125    Warning,
126    MarginCall,
127    Liquidation,
128}
129
130/// payment application result
131#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
132pub struct PaymentApplication {
133    pub to_fees: Money,
134    pub to_penalties: Money,
135    pub to_interest: Money,
136    pub to_principal: Money,
137    pub excess: Money,
138}
139
140impl PaymentApplication {
141    pub fn total_applied(&self) -> Money {
142        self.to_fees + self.to_penalties + self.to_interest + self.to_principal
143    }
144}
145
146/// deficiency balance after liquidation
147#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
148pub struct DeficiencyBalance {
149    pub original_loan_id: FacilityId,
150    pub liquidation_date: DateTime<Utc>,
151    pub collateral_proceeds: Money,
152    pub remaining_debt: Money,
153    pub recovery_status: RecoveryStatus,
154}
155
156/// recovery status for deficiency
157#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
158pub enum RecoveryStatus {
159    Pursuing,
160    PaymentPlan,
161    LegalAction,
162    WrittenOff,
163}