Skip to main content

finance_solution/
tvm_convert_rate.rs

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