Skip to main content

quant_primitives/
currency.rs

1//! Currency newtype wrapper.
2
3use serde::{Deserialize, Serialize};
4use std::fmt;
5use std::sync::Arc;
6
7/// A currency identifier (e.g., USD, EUR, BTC).
8///
9/// Normalizes to uppercase for consistent comparison.
10/// Backed by `Arc<str>` for O(1) clone in hot loops.
11#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
12pub struct Currency(Arc<str>);
13
14impl Currency {
15    /// Create a new currency, normalizing to uppercase.
16    pub fn new(code: impl Into<String>) -> Self {
17        Self(Arc::from(code.into().to_uppercase().as_str()))
18    }
19
20    /// Get the currency code.
21    pub fn code(&self) -> &str {
22        &self.0
23    }
24}
25
26impl fmt::Display for Currency {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        write!(f, "{}", &*self.0)
29    }
30}
31
32impl From<&str> for Currency {
33    fn from(s: &str) -> Self {
34        Self::new(s)
35    }
36}
37
38#[cfg(test)]
39#[path = "currency_tests.rs"]
40mod tests;