quant-primitives 0.7.0

Pure trading primitives — candles, intervals, symbols, currencies, asset taxonomy
Documentation
//! Currency newtype wrapper.

use serde::{Deserialize, Serialize};
use std::fmt;
use std::sync::Arc;

/// A currency identifier (e.g., USD, EUR, BTC).
///
/// Normalizes to uppercase for consistent comparison.
/// Backed by `Arc<str>` for O(1) clone in hot loops.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Currency(Arc<str>);

impl Currency {
    /// Create a new currency, normalizing to uppercase.
    pub fn new(code: impl Into<String>) -> Self {
        Self(Arc::from(code.into().to_uppercase().as_str()))
    }

    /// Get the currency code.
    pub fn code(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for Currency {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", &*self.0)
    }
}

impl From<&str> for Currency {
    fn from(s: &str) -> Self {
        Self::new(s)
    }
}

#[cfg(test)]
#[path = "currency_tests.rs"]
mod tests;