| 1 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#; |
| 2 |
|
| 3 |
use quick_xmlrust-analyzer cargo quick-xml 0.42.0 crate/::eventsrust-analyzer cargo quick-xml 0.42.0 events/::Eventrust-analyzer cargo quick-xml 0.42.0 events/Event#; |
| 4 |
use quick_xmlrust-analyzer cargo quick-xml 0.42.0 crate/::readerrust-analyzer cargo quick-xml 0.42.0 reader/::Readerrust-analyzer cargo quick-xml 0.42.0 reader/Reader#; |
| 5 |
|
| 6 |
pub type ExchangeRatesrust-analyzer cargo numbat-exchange-rates 0.5.0 ExchangeRates# = HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, f64>; |
| 7 |
|
| 8 |
pub fn parse_exchange_ratesrust-analyzer cargo numbat-exchange-rates 0.5.0 parse_exchange_rates().(xml_content: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<ExchangeRatesrust-analyzer cargo numbat-exchange-rates 0.5.0 ExchangeRates#> { |
| 9 |
let mut rates = ExchangeRatesrust-analyzer cargo numbat-exchange-rates 0.5.0 ExchangeRates#::defaultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S>`][Default]default().(); |
| 10 |
|
| 11 |
let mut reader = Readerrust-analyzer cargo quick-xml 0.42.0 reader/Reader#::from_strrust-analyzer cargo quick-xml 0.42.0 reader/slice_reader/impl#[`Reader<&'a [u8]>`]from_str().(xml_content); |
| 12 |
loop { |
| 13 |
match reader.read_eventrust-analyzer cargo quick-xml 0.42.0 reader/slice_reader/impl#[`Reader<&'a [u8]>`]read_event().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch(). { |
| 14 |
Eventrust-analyzer cargo quick-xml 0.42.0 events/Event#::Eofrust-analyzer cargo quick-xml 0.42.0 events/Event#Eof# => break, |
| 15 |
Eventrust-analyzer cargo quick-xml 0.42.0 events/Event#::Emptyrust-analyzer cargo quick-xml 0.42.0 events/Event#Empty#(e) => { |
| 16 |
if e.local_namerust-analyzer cargo quick-xml 0.42.0 events/impl#[`BytesStart<'i>`]local_name().().as_refrust-analyzer cargo quick-xml 0.42.0 name/impl#[`LocalName<'a>`][`AsRef<str>`]as_ref().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().b"Cube" { |
| 17 |
continue; |
| 18 |
} |
| 19 |
let currency = &e |
| 20 |
.try_get_attributerust-analyzer cargo quick-xml 0.42.0 events/impl#[`BytesStart<'i>`]try_get_attribute().("currency") |
| 21 |
.okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch(). |
| 22 |
.unescape_valuerust-analyzer cargo quick-xml 0.42.0 events/attributes/impl#[`Attribute<'a>`]unescape_value().() |
| 23 |
.okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 24 |
let rate = &e.try_get_attributerust-analyzer cargo quick-xml 0.42.0 events/impl#[`BytesStart<'i>`]try_get_attribute().("rate").okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..unescape_valuerust-analyzer cargo quick-xml 0.42.0 events/attributes/impl#[`Attribute<'a>`]unescape_value().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 25 |
let rate = rate.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 26 |
|
| 27 |
rates.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(currency.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), rate); |
| 28 |
} |
| 29 |
_ => {} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(rates) |
| 34 |
} |
| 35 |
|
| 36 |
#[cfg(feature = "fetch-exchangerates")] |
| 37 |
const ECB_XML_URLrust-analyzer cargo numbat-exchange-rates 0.5.0 ECB_XML_URL.: &str = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; |
| 38 |
|
| 39 |
#[cfg(feature = "fetch-exchangerates")] |
| 40 |
fn fetch_ecb_xmlrust-analyzer cargo numbat-exchange-rates 0.5.0 fetch_ecb_xml().() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| •41 |
reqwestrust-analyzer cargo reqwest 0.13.4 crate/::blockingrust-analyzer cargo reqwest 0.13.4 blocking/::getrust-analyzer cargo reqwest 0.13.4 blocking/get().(ECB_XML_URLrust-analyzer cargo numbat-exchange-rates 0.5.0 ECB_XML_URL.).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..textrust-analyzer cargo reqwest 0.13.4 blocking/response/impl#[Response]text().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().() |
| 42 |
} |
| 43 |
|
| 44 |
#[cfg(feature = "fetch-exchangerates")] |
| 45 |
pub fn fetch_exchange_ratesrust-analyzer cargo numbat-exchange-rates 0.5.0 fetch_exchange_rates().() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<ExchangeRatesrust-analyzer cargo numbat-exchange-rates 0.5.0 ExchangeRates#> { |
| 46 |
let xml_content = fetch_ecb_xmlrust-analyzer cargo numbat-exchange-rates 0.5.0 fetch_ecb_xml().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 47 |
parse_exchange_ratesrust-analyzer cargo numbat-exchange-rates 0.5.0 parse_exchange_rates().(&xml_content) |
| 48 |
} |
| 49 |
|
| 50 |
#[cfg(test)] |
| 51 |
#[cfg(feature = "fetch-exchangerates")] |
| 52 |
mod testsrust-analyzer cargo numbat-exchange-rates 0.5.0 tests/ { |
| 53 |
use superrust-analyzer cargo numbat-exchange-rates 0.5.0 crate/::*; |
| 54 |
|
| 55 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 56 |
fn fetch_exchange_rates_worksrust-analyzer cargo numbat-exchange-rates 0.5.0 tests/fetch_exchange_rates_works().() { |
| 57 |
fetch_exchange_ratesrust-analyzer cargo numbat-exchange-rates 0.5.0 fetch_exchange_rates().(); |
| 58 |
} |
| 59 |
} |