1use std::str::FromStr;
2
3#[derive(Debug, Clone)]
4pub struct Currency(pub String);
5
6impl Currency {
7 pub fn new(currency: &str) -> anyhow::Result<Self> {
8 if !currency.chars().all(|c| c.is_ascii_alphabetic()) {
9 anyhow::bail!("Currency must contain only letters");
10 }
11 Ok(Self(currency.to_ascii_uppercase()))
12 }
13
14 pub fn encode(&self) -> Option<u32> {
15 let mut result: u32 = 0;
16
17 for (i, c) in self.0.chars().enumerate() {
18 let value = match c {
19 'A'..='Z' => (c as u32) - ('A' as u32),
20 _ => return None,
21 };
22 result |= value << (i * 5);
23 }
24
25 Some(result)
26 }
27}
28
29impl FromStr for Currency {
30 type Err = anyhow::Error;
31
32 fn from_str(s: &str) -> Result<Self, Self::Err> {
33 Currency::new(s)
34 }
35}