1
+ use std:: { error, fmt, fs, io:: Write , time} ;
2
+
1
3
use crate :: Error ;
2
4
use crate :: config:: { self , ExchangeRateSource } ;
3
5
use crate :: file_paths;
4
- use std:: { error, fmt, fs, io:: Write , time} ;
5
-
6
- const MAX_AGE : u64 = 86400 * 3 ;
7
6
8
7
fn get_current_timestamp ( ) -> Result < u64 , Error > {
9
8
Ok ( time:: SystemTime :: now ( )
@@ -19,7 +18,7 @@ fn get_cache_filename(source: config::ExchangeRateSource) -> Result<&'static str
19
18
} )
20
19
}
21
20
22
- fn load_cached_data ( source : config:: ExchangeRateSource ) -> Result < String , Error > {
21
+ fn load_cached_data ( source : config:: ExchangeRateSource , max_age : u64 ) -> Result < String , Error > {
23
22
let mut cache_file = file_paths:: get_cache_dir ( file_paths:: DirMode :: DontCreate ) ?;
24
23
cache_file. push ( get_cache_filename ( source) ?) ;
25
24
let cache_contents = fs:: read_to_string ( cache_file) ?;
@@ -30,7 +29,7 @@ fn load_cached_data(source: config::ExchangeRateSource) -> Result<String, Error>
30
29
let age = current_timestamp
31
30
. checked_sub ( timestamp)
32
31
. ok_or ( "invalid cache timestamp" ) ?;
33
- if age > MAX_AGE {
32
+ if age > max_age {
34
33
return Err ( "cache expired" . into ( ) ) ;
35
34
}
36
35
Ok ( cache_xml. to_string ( ) )
@@ -55,8 +54,11 @@ fn http_get(_url: &str) -> Result<String, Error> {
55
54
Err ( "internet access has been disabled in this build of fend" . into ( ) )
56
55
}
57
56
58
- fn load_exchange_rate_xml ( source : config:: ExchangeRateSource ) -> Result < ( String , bool ) , Error > {
59
- match load_cached_data ( source) {
57
+ fn load_exchange_rate_xml (
58
+ source : config:: ExchangeRateSource ,
59
+ max_age : u64 ,
60
+ ) -> Result < ( String , bool ) , Error > {
61
+ match load_cached_data ( source, max_age) {
60
62
Ok ( xml) => return Ok ( ( xml, true ) ) ,
61
63
Err ( _e) => {
62
64
// failed to load cached data
@@ -152,8 +154,11 @@ fn parse_exchange_rates_un(exchange_rates: &str) -> Result<Vec<(String, f64)>, E
152
154
Ok ( result)
153
155
}
154
156
155
- fn get_exchange_rates ( source : config:: ExchangeRateSource ) -> Result < Vec < ( String , f64 ) > , Error > {
156
- let ( xml, cached) = load_exchange_rate_xml ( source) ?;
157
+ fn get_exchange_rates (
158
+ source : config:: ExchangeRateSource ,
159
+ max_age : u64 ,
160
+ ) -> Result < Vec < ( String , f64 ) > , Error > {
161
+ let ( xml, cached) = load_exchange_rate_xml ( source, max_age) ?;
157
162
let parsed_data = parse_exchange_rates ( source, & xml) ?;
158
163
if !cached {
159
164
store_cached_data ( source, & xml) ?;
@@ -195,6 +200,7 @@ impl error::Error for ExchangeRateSourceDisabledError {}
195
200
pub struct ExchangeRateHandler {
196
201
pub enable_internet_access : bool ,
197
202
pub source : ExchangeRateSource ,
203
+ pub max_age : u64 ,
198
204
}
199
205
200
206
impl fend_core:: ExchangeRateFn for ExchangeRateHandler {
@@ -205,7 +211,7 @@ impl fend_core::ExchangeRateFn for ExchangeRateHandler {
205
211
if !self . enable_internet_access {
206
212
return Err ( InternetAccessDisabledError . into ( ) ) ;
207
213
}
208
- let exchange_rates = get_exchange_rates ( self . source ) ?;
214
+ let exchange_rates = get_exchange_rates ( self . source , self . max_age ) ?;
209
215
for ( c, rate) in exchange_rates {
210
216
if currency == c {
211
217
return Ok ( rate) ;
0 commit comments