// Currency symbols utilities. // SEE: tools/update-coins/symbols.rs.tmpl // @autogenerated use std::convert::TryFrom; use super::errors::{Error, ErrorKind}; /// Currency symbol. #[derive(Serialize, Deserialize, PartialEq, Clone)] pub enum Symbol { /// Euro EUR = 1, /// United States Dollar USD = 2,{{range $k, $v := .}} /// {{$v.Name}} {{$v.Symbol}} = {{$v.Num}},{{end}} } /// Tries to convert string to a currency `Symbol`. impl<'a> TryFrom<&'a str> for Symbol { type Error = Error; fn try_from(name: &str) -> Result { match name { "EUR" => Ok(Symbol::EUR), "USD" => Ok(Symbol::USD),{{range $k, $v := .}} "{{$v.Symbol}}" => Ok(Symbol::{{$v.Symbol}}),{{end}} _ => Err(ErrorKind::UnknownCurrency(name.to_owned()).into()), } } } impl ::std::fmt::Debug for Symbol { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { let symbol = match self { &Symbol::EUR => "EUR", &Symbol::USD => "USD",{{range $k, $v := .}} &Symbol::{{$v.Symbol}} => "{{$v.Symbol}}",{{end}} }; f.write_str(symbol) } } #[cfg(test)] mod tests { use super::*; #[test] fn symbol_debug() { let c = Symbol::try_from("BTC").unwrap(); assert_eq!("BTC".to_owned(), format!("{:?}", c)); } }