1 Commits

Author SHA1 Message Date
0f930271c7 Use smolstr since we only expect small strings 2025-12-17 22:10:46 +01:00
12 changed files with 90 additions and 180 deletions

30
Cargo.lock generated
View File

@@ -91,6 +91,15 @@ version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
[[package]]
name = "borsh"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1da5ab77c1437701eeff7c88d968729e7766172279eab0676857b3d63af7a6f"
dependencies = [
"cfg_aliases",
]
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.19.1" version = "3.19.1"
@@ -119,6 +128,12 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]] [[package]]
name = "chrono" name = "chrono"
version = "0.4.42" version = "0.4.42"
@@ -227,6 +242,7 @@ dependencies = [
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_json",
"smol_str",
"tokio", "tokio",
] ]
@@ -961,9 +977,9 @@ dependencies = [
[[package]] [[package]]
name = "rustls-pki-types" name = "rustls-pki-types"
version = "1.13.1" version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "708c0f9d5f54ba0272468c1d306a52c495b31fa155e91bc25371e6df7996908c" checksum = "21e6f2ab2928ca4291b86736a8bd920a277a399bba1589409d72154ff87c1282"
dependencies = [ dependencies = [
"zeroize", "zeroize",
] ]
@@ -1096,6 +1112,16 @@ version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
[[package]]
name = "smol_str"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3498b0a27f93ef1402f20eefacfaa1691272ac4eca1cdc8c596cb0a245d6cbf5"
dependencies = [
"borsh",
"serde_core",
]
[[package]] [[package]]
name = "socket2" name = "socket2"
version = "0.6.1" version = "0.6.1"

View File

@@ -34,4 +34,5 @@ quick-xml = { version = "0.38", features = ["async-tokio", "tokio"] }
reqwest = "0.12" reqwest = "0.12"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
smol_str = { version = "0.3", features = ["serde"] }
tokio = { version = "1.48", features = ["macros"] } tokio = { version = "1.48", features = ["macros"] }

View File

@@ -1,4 +1,5 @@
use clap::{arg, Parser, ValueEnum}; use clap::{arg, Parser, ValueEnum};
use smol_str::SmolStr;
use super::{ShowDays, SortBy}; use super::{ShowDays, SortBy};
@@ -8,7 +9,7 @@ use super::{ShowDays, SortBy};
pub struct Cli { pub struct Cli {
/// Which currencies do you want to fetch rates for? /// Which currencies do you want to fetch rates for?
#[arg(long = "currencies", short = 'c')] #[arg(long = "currencies", short = 'c')]
pub currencies: Vec<String>, pub currencies: Vec<SmolStr>,
#[arg(value_enum, default_value_t = FormatOption::Plain)] #[arg(value_enum, default_value_t = FormatOption::Plain)]
pub command: FormatOption, pub command: FormatOption,
@@ -35,7 +36,7 @@ pub struct Cli {
/// Recalculate to the perspective from an included currency /// Recalculate to the perspective from an included currency
#[arg(long = "perspective", short = 'p')] #[arg(long = "perspective", short = 'p')]
pub perspective: Option<String>, pub perspective: Option<SmolStr>,
/// Invert the rate /// Invert the rate
#[arg(long = "invert", short = 'i')] #[arg(long = "invert", short = 'i')]

View File

@@ -1,7 +1,5 @@
use clap::ValueEnum; use clap::ValueEnum;
use crate::models::Currency;
#[derive(Debug, ValueEnum, Clone)] #[derive(Debug, ValueEnum, Clone)]
pub enum SortBy { pub enum SortBy {
Currency, Currency,
@@ -9,9 +7,9 @@ pub enum SortBy {
} }
impl SortBy { impl SortBy {
pub fn get_comparer(&self) -> fn(&(&Currency, f64), &(&Currency, f64)) -> std::cmp::Ordering { pub fn get_comparer(&self) -> fn(&(&str, f64), &(&str, f64)) -> std::cmp::Ordering {
match self { match self {
Self::Currency => |a, b| a.0.as_ref().cmp(b.0.as_ref()), Self::Currency => |a, b| a.0.cmp(&b.0),
Self::Rate => |a, b| a.1.total_cmp(&b.1), Self::Rate => |a, b| a.1.total_cmp(&b.1),
} }
} }

View File

@@ -2,11 +2,11 @@ use clap::Parser as _;
use ecb_rates::cache::{Cache, CacheLine}; use ecb_rates::cache::{Cache, CacheLine};
use ecb_rates::HeaderDescription; use ecb_rates::HeaderDescription;
use reqwest::{Client, IntoUrl}; use reqwest::{Client, IntoUrl};
use smol_str::StrExt;
use std::process::ExitCode; use std::process::ExitCode;
use std::str::FromStr;
use ecb_rates::cli::{Cli, FormatOption}; use ecb_rates::cli::{Cli, FormatOption};
use ecb_rates::models::{Currency, ExchangeRateResult}; use ecb_rates::models::ExchangeRateResult;
use ecb_rates::parsing::parse; use ecb_rates::parsing::parse;
use ecb_rates::table::{TableRef, TableTrait as _}; use ecb_rates::table::{TableRef, TableTrait as _};
use ecb_rates::utils_calc::{change_perspective, filter_currencies, invert_rates, round}; use ecb_rates::utils_calc::{change_perspective, filter_currencies, invert_rates, round};
@@ -79,19 +79,9 @@ async fn main() -> ExitCode {
parsed parsed
}; };
cli.perspective = cli.perspective.map(|s| s.to_uppercase()); cli.perspective = cli.perspective.map(|s| s.to_uppercase_smolstr());
let parsed_currency = match cli.perspective.as_ref() { if let Some(currency) = cli.perspective.as_ref() {
Some(currency) => match Currency::from_str(currency) { header_description.replace_eur(&currency);
Ok(k) => Some(k),
Err(e) => {
eprintln!("The currency code '{}' is invalid: {:?}", currency, e);
return ExitCode::FAILURE;
}
},
None => None,
};
if let Some(currency) = parsed_currency.as_ref() {
header_description.replace_eur(currency.as_ref());
let error_occured = change_perspective(&mut parsed, &currency).is_none(); let error_occured = change_perspective(&mut parsed, &currency).is_none();
if error_occured { if error_occured {
eprintln!("The currency wasn't in the data from the ECB!"); eprintln!("The currency wasn't in the data from the ECB!");
@@ -107,19 +97,11 @@ async fn main() -> ExitCode {
round(&mut parsed, cli.max_decimals); round(&mut parsed, cli.max_decimals);
if !cli.currencies.is_empty() { if !cli.currencies.is_empty() {
let currencies = match cli let currencies = cli
.currencies .currencies
.iter() .iter()
.map(|x| x.to_uppercase()) .map(|x| x.to_uppercase_smolstr())
.map(|x| Currency::from_str(&x)) .collect::<Vec<_>>();
.collect::<anyhow::Result<Vec<_>>>()
{
Ok(k) => k,
Err(e) => {
eprintln!("Failed to parse currenc(y/ies): {:?}", e);
return ExitCode::FAILURE;
}
};
filter_currencies(&mut parsed, &currencies); filter_currencies(&mut parsed, &currencies);
} }

View File

@@ -1,10 +1,9 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use smol_str::SmolStr;
use std::collections::HashMap; use std::collections::HashMap;
use super::Currency;
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct ExchangeRateResult { pub struct ExchangeRateResult {
pub time: String, pub time: SmolStr,
pub rates: HashMap<Currency, f64>, pub rates: HashMap<SmolStr, f64>,
} }

View File

@@ -1,97 +0,0 @@
use std::{
fmt,
ops::Index,
slice::Iter,
str::{self, FromStr},
};
use serde::{de, Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Currency {
name: [u8; 3],
}
impl Currency {
pub fn as_str(&self) -> &str {
// SAFETY: We validate that bytes are ASCII in FromStr.
unsafe { str::from_utf8_unchecked(&self.name) }
}
pub fn iter(&self) -> Iter<'_, u8> {
self.name.iter()
}
}
impl AsRef<str> for Currency {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Serialize for Currency {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for Currency {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
FromStr::from_str(&s).map_err(de::Error::custom)
}
}
impl FromStr for Currency {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() != 3 {
anyhow::bail!("Currency code must be exactly 3 chars");
}
if !s.is_ascii() {
anyhow::bail!("Currency code must be ASCII");
}
let b = s.as_bytes();
Ok(Self {
name: [b[0], b[1], b[2]],
})
}
}
impl TryFrom<&str> for Currency {
type Error = anyhow::Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Currency::from_str(value)
}
}
impl fmt::Display for Currency {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl Index<usize> for Currency {
type Output = u8;
fn index(&self, index: usize) -> &Self::Output {
&self.name[index]
}
}
impl<'a> IntoIterator for &'a Currency {
type Item = &'a u8;
type IntoIter = Iter<'a, u8>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

View File

@@ -1,5 +0,0 @@
mod currency;
mod exchange_rate_result;
pub use currency::Currency;
pub use exchange_rate_result::ExchangeRateResult;

View File

@@ -1,45 +1,52 @@
use std::{collections::HashMap, str::FromStr}; use std::collections::HashMap;
use quick_xml::events::Event; use quick_xml::events::Event;
use quick_xml::Reader; use quick_xml::Reader;
use smol_str::SmolStr;
use crate::models::{Currency, ExchangeRateResult}; use crate::models::ExchangeRateResult;
fn smol_from_utf8(bytes: &[u8]) -> SmolStr {
str::from_utf8(bytes)
.map(SmolStr::new)
.unwrap_or_else(|_| SmolStr::new(String::from_utf8_lossy(bytes)))
}
pub fn parse(xml: &str) -> anyhow::Result<Vec<ExchangeRateResult>> { pub fn parse(xml: &str) -> anyhow::Result<Vec<ExchangeRateResult>> {
let mut reader = Reader::from_str(xml); let mut reader = Reader::from_str(xml);
reader.config_mut().trim_text(true); reader.config_mut().trim_text(true);
let mut results = Vec::new(); let mut results = Vec::new();
let mut current_time: Option<String> = None; let mut current_time: Option<SmolStr> = None;
let mut inside_cube_time = false; let mut inside_cube_time = false;
let mut current_rates = HashMap::new(); let mut current_rates = HashMap::new();
fn handle_cube_element( fn handle_cube_element(
e: &quick_xml::events::BytesStart, e: &quick_xml::events::BytesStart,
current_time: &mut Option<String>, current_time: &mut Option<SmolStr>,
inside_cube_time: &mut bool, inside_cube_time: &mut bool,
current_rates: &mut HashMap<Currency, f64>, current_rates: &mut HashMap<SmolStr, f64>,
results: &mut Vec<ExchangeRateResult>, results: &mut Vec<ExchangeRateResult>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
if e.name().local_name().as_ref() != b"Cube" { if e.name().local_name().as_ref() != b"Cube" {
return Ok(()); return Ok(());
} }
let mut time_attr: Option<String> = None; let mut time_attr: Option<SmolStr> = None;
let mut currency_attr: Option<Currency> = None; let mut currency_attr: Option<SmolStr> = None;
let mut rate_attr: Option<String> = None; let mut rate_attr: Option<SmolStr> = None;
for attr_result in e.attributes() { for attr_result in e.attributes() {
let attr = attr_result?; let attr = attr_result?;
let key = attr.key.as_ref(); let key = attr.key.as_ref();
let val = String::from_utf8_lossy(attr.value.as_ref()).to_string(); let val = smol_from_utf8(attr.value.as_ref());
match key { match key {
b"time" => { b"time" => {
time_attr = Some(val); time_attr = Some(val);
} }
b"currency" => { b"currency" => {
currency_attr = Some(Currency::from_str(&val)?); currency_attr = Some(val);
} }
b"rate" => { b"rate" => {
rate_attr = Some(val); rate_attr = Some(val);

View File

@@ -1,27 +1,29 @@
use std::fmt::Display; use std::fmt::Display;
use smol_str::SmolStr;
use crate::cli::SortBy; use crate::cli::SortBy;
use crate::models::{Currency, ExchangeRateResult}; use crate::models::ExchangeRateResult;
use crate::DEFAULT_WIDTH; use crate::DEFAULT_WIDTH;
use super::table_display::helper_table_print; use super::table_display::helper_table_print;
use super::{TableGet, TableTrait}; use super::{TableGet, TableTrait};
pub struct Table { pub struct Table {
pub(super) header: Option<String>, pub(super) header: Option<SmolStr>,
pub(super) column_left: String, pub(super) column_left: SmolStr,
pub(super) column_right: String, pub(super) column_right: SmolStr,
pub(super) rows: Vec<(Currency, f64)>, pub(super) rows: Vec<(SmolStr, f64)>,
pub color: bool, pub color: bool,
pub width: usize, pub width: usize,
pub left_offset: usize, pub left_offset: usize,
} }
impl<'a> TableTrait<'a> for Table { impl<'a> TableTrait<'a> for Table {
type Header = String; type Header = SmolStr;
type ColumnLeft = String; type ColumnLeft = SmolStr;
type ColumnRight = String; type ColumnRight = SmolStr;
type RowLeft = Currency; type RowLeft = SmolStr;
fn new( fn new(
header: Option<Self::Header>, header: Option<Self::Header>,
@@ -59,8 +61,8 @@ impl<'a> TableTrait<'a> for Table {
} }
impl TableGet for Table { impl TableGet for Table {
type RowLeftRef = Currency; type RowLeftRef = SmolStr;
type RowRightRef = String; type RowRightRef = SmolStr;
fn get_header(&self) -> Option<&str> { fn get_header(&self) -> Option<&str> {
self.header.as_deref() self.header.as_deref()
@@ -85,7 +87,7 @@ impl TableGet for Table {
impl From<ExchangeRateResult> for Table { impl From<ExchangeRateResult> for Table {
fn from(value: ExchangeRateResult) -> Self { fn from(value: ExchangeRateResult) -> Self {
let mut table = Table::new(Some(value.time), "Currency".to_string(), "Rate".to_string()); let mut table = Table::new(Some(value.time), "Currency".into(), "Rate".into());
for (key, val) in value.rates.into_iter() { for (key, val) in value.rates.into_iter() {
table.add_row(key, val); table.add_row(key, val);
} }

View File

@@ -1,7 +1,7 @@
use std::fmt::Display; use std::fmt::Display;
use crate::cli::SortBy; use crate::cli::SortBy;
use crate::models::{Currency, ExchangeRateResult}; use crate::models::ExchangeRateResult;
use crate::DEFAULT_WIDTH; use crate::DEFAULT_WIDTH;
use super::table_display::helper_table_print; use super::table_display::helper_table_print;
@@ -13,7 +13,7 @@ pub struct TableRef<'a> {
header: Option<&'a str>, header: Option<&'a str>,
column_left: &'a str, column_left: &'a str,
column_right: &'a str, column_right: &'a str,
rows: Vec<(&'a Currency, f64)>, rows: Vec<(&'a str, f64)>,
pub color: bool, pub color: bool,
pub width: usize, pub width: usize,
pub left_offset: usize, pub left_offset: usize,
@@ -23,7 +23,7 @@ impl<'a> TableTrait<'a> for TableRef<'a> {
type Header = &'a str; type Header = &'a str;
type ColumnLeft = &'a str; type ColumnLeft = &'a str;
type ColumnRight = &'a str; type ColumnRight = &'a str;
type RowLeft = &'a Currency; type RowLeft = &'a str;
fn new( fn new(
header: Option<Self::Header>, header: Option<Self::Header>,
@@ -60,7 +60,7 @@ impl<'a> TableTrait<'a> for TableRef<'a> {
} }
impl<'a> TableGet for TableRef<'a> { impl<'a> TableGet for TableRef<'a> {
type RowLeftRef = &'a Currency; type RowLeftRef = &'a str;
type RowRightRef = &'a str; type RowRightRef = &'a str;
fn get_header(&self) -> Option<&str> { fn get_header(&self) -> Option<&str> {
@@ -78,7 +78,7 @@ impl<'a> TableGet for TableRef<'a> {
fn get_width(&self) -> usize { fn get_width(&self) -> usize {
self.width self.width
} }
fn get_left_offset(&self) -> usize { fn get_left_offset(&self) -> usize {
self.left_offset self.left_offset
} }
@@ -106,7 +106,7 @@ impl<'a> From<&'a Table> for TableRef<'a> {
let rows = table let rows = table
.rows .rows
.iter() .iter()
.map(|(left, right)| (left, *right)) .map(|(left, right)| (left.as_str(), *right))
.collect(); .collect();
TableRef { TableRef {

View File

@@ -1,13 +1,12 @@
use std::{borrow::BorrowMut, collections::HashMap, ops::Deref, str::FromStr}; use std::{borrow::BorrowMut, collections::HashMap, ops::Deref};
use crate::models::{Currency, ExchangeRateResult}; use smol_str::SmolStr;
pub fn filter_currencies( use crate::models::ExchangeRateResult;
exchange_rate_results: &mut [ExchangeRateResult],
currencies: &[Currency], pub fn filter_currencies(exchange_rate_results: &mut [ExchangeRateResult], currencies: &[SmolStr]) {
) {
for exchange_rate in exchange_rate_results { for exchange_rate in exchange_rate_results {
let rates_ptr: *mut HashMap<Currency, f64> = &mut exchange_rate.rates; let rates_ptr: *mut HashMap<_, _> = &mut exchange_rate.rates;
exchange_rate exchange_rate
.rates .rates
.keys() .keys()
@@ -25,7 +24,7 @@ pub fn filter_currencies(
pub fn change_perspective( pub fn change_perspective(
exchange_rate_results: &mut [ExchangeRateResult], exchange_rate_results: &mut [ExchangeRateResult],
currency: &Currency, currency: &str,
) -> Option<()> { ) -> Option<()> {
for rate_res in exchange_rate_results { for rate_res in exchange_rate_results {
let currency_rate = rate_res.rates.remove(currency)?; let currency_rate = rate_res.rates.remove(currency)?;
@@ -35,10 +34,7 @@ pub fn change_perspective(
*iter_rate = eur_rate * iter_rate.deref(); *iter_rate = eur_rate * iter_rate.deref();
} }
rate_res.rates.insert( rate_res.rates.insert("EUR".into(), eur_rate);
unsafe { Currency::from_str("EUR").unwrap_unchecked() },
eur_rate,
);
} }
Some(()) Some(())
} }