finance_solution/
tvm_convert_rate.rs

1//! The internal module which supports the solution struct for Rate Conversion (see `convert_rate`).
2
3use std::fmt::Debug;
4use std::fmt;
5use colored::*;
6
7
8// Import needed for the function references in the Rustdoc comments.
9#[allow(unused_imports)]
10use crate::*;
11
12/// The possible types of rates to convert.
13#[derive(Debug, Clone)]
14pub enum ConvertRateVariable {
15    Apr,
16    Ear,
17    Epr,
18    AprContinuous,
19    EarContinuous
20}
21impl ConvertRateVariable {
22    pub fn is_apr(&self) -> bool {
23        match self {
24            ConvertRateVariable::Apr => true,
25            _ => false,
26        }
27    }
28    pub fn is_epr(&self) -> bool {
29        match self {
30            ConvertRateVariable::Epr => true,
31            _ => false,
32        }
33    }
34    pub fn is_ear(&self) -> bool {
35        match self {
36            ConvertRateVariable::Ear => true,
37            _ => false,
38        }
39    }
40    pub fn is_apr_continuous(&self) -> bool {
41        match self {
42            ConvertRateVariable::AprContinuous => true,
43            _ => false,
44        }
45    }
46    pub fn is_ear_continuous(&self) -> bool {
47        match self {
48            ConvertRateVariable::EarContinuous => true,
49            _ => false,
50        }
51    }
52}
53impl fmt::Display for ConvertRateVariable {
54    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55       match *self {
56           ConvertRateVariable::Apr => write!(f, "Apr"),
57           ConvertRateVariable::Epr => write!(f, "Epr"),
58           ConvertRateVariable::Ear => write!(f, "Ear"),
59           ConvertRateVariable::AprContinuous => write!(f, "AprContinuous"),
60           ConvertRateVariable::EarContinuous => write!(f, "EarContinuous"),
61       }
62    }
63}
64
65// #[derive(Debug)]
66pub struct ConvertRateSolution {
67    input_name: ConvertRateVariable,
68    input_rate: f64,
69    compounds_per_year: u32,
70    apr_in_percent: String,
71    epr_in_percent: String,
72    ear_in_percent: String,
73    apr: f64,
74    epr: f64,
75    ear: f64,
76    apr_formula: String,
77    epr_formula: String,
78    ear_formula: String,
79}
80impl ConvertRateSolution {
81    pub fn new(input_name: ConvertRateVariable, input_rate: f64, compounds_per_year: u32, apr_in_percent: String, epr_in_percent: String, ear_in_percent: String, apr:f64, epr:f64, ear:f64, apr_formula: &str, epr_formula: &str, ear_formula: &str) -> Self {
82        Self {
83            input_name,
84            input_rate,
85            compounds_per_year,
86            apr_in_percent,
87            epr_in_percent,
88            ear_in_percent,
89            apr,
90            epr,
91            ear,
92            apr_formula: apr_formula.to_string(),
93            epr_formula: epr_formula.to_string(),
94            ear_formula: ear_formula.to_string(),
95        }
96    }
97
98    /// Returns the input rate.
99    pub fn input_rate(&self) -> f64 {
100        self.input_rate
101    }
102    /// Returns the annual rate (apr).
103    pub fn apr(&self) -> f64 {
104        self.apr
105    }
106    /// Returns the periodic rate (epr).
107    pub fn epr(&self) -> f64 {
108        self.epr
109    }
110    /// Returns the effective annual rate (ear).
111    pub fn ear(&self) -> f64 {
112        self.ear
113    }
114    /// Returns the input name (Ear, Apr, Epr, AprContinuous...etc).
115    pub fn input_name(&self) -> &ConvertRateVariable {
116        &self.input_name
117    }
118    /// Returns the compounds per year as u32.
119    pub fn compounds_per_year(&self) -> u32 {
120        self.compounds_per_year
121    }
122    /// Returns the annual rate (APR) in percentage format (for example, 3.58%).
123    pub fn apr_in_percent(&self) -> &String {
124        &self.apr_in_percent
125    }
126    /// Returns the effective annual rate (EAR) in percentage format (for example, 3.69%).
127    pub fn ear_in_percent(&self) -> &String {
128        &self.ear_in_percent
129    }
130    /// Returns the periodic rate (EPR) in percentage format (for example, 1.12%).
131    pub fn epr_in_percent(&self) -> &String {
132        &self.epr_in_percent
133    }
134}
135
136
137impl Debug for ConvertRateSolution {
138    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139    write!(f, "{{\n {},\n {},\n {},\n {}\n {}\n {}\n {}\n {}\n {}\n {}\n {}\n {}\n}}",
140               &format!("input_name: {}", self.input_name.to_string().magenta()),
141               &format!("input_rate: {}", self.input_rate.to_string().yellow()),
142               &format!("compounds_per_year: {:.4}", self.compounds_per_year.to_string().yellow()),
143               &format!("apr_in_percent: {:.6}%", self.apr_in_percent),
144               &format!("epr_in_percent: {:.6}%", self.epr_in_percent),
145               &format!("ear_in_percent: {:.6}%", self.ear_in_percent),
146               &format!("apr: {}", self.apr),
147               &format!("epr: {}", self.epr),
148               &format!("ear: {}", self.ear),
149               &format!("apr_formula: {}", self.apr_formula),
150               &format!("epr_formula: {}", self.epr_formula),
151               &format!("ear_formula: {}", self.ear_formula),
152        )
153    }
154}