net_name.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. use crate::{
  2. serial::{Decodable, Encodable},
  3. Result,
  4. };
  5. use std::str::FromStr;
  6. #[derive(Debug, PartialEq, Eq, Hash, Clone)]
  7. pub enum NetworkName {
  8. Solana,
  9. Bitcoin,
  10. Empty
  11. }
  12. impl std::fmt::Display for NetworkName {
  13. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  14. match self {
  15. Self::Solana => {
  16. write!(f, "Solana")
  17. }
  18. Self::Bitcoin => {
  19. write!(f, "Bitcoin")
  20. }
  21. Self::Empty => {
  22. write!(f, "No Supported Network")
  23. }
  24. }
  25. }
  26. }
  27. impl FromStr for NetworkName {
  28. type Err = crate::Error;
  29. fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
  30. match s.to_lowercase().as_str() {
  31. "sol" | "solana" => Ok(NetworkName::Solana),
  32. "btc" | "bitcoin" => Ok(NetworkName::Bitcoin),
  33. _ => Err(crate::Error::NotSupportedNetwork),
  34. }
  35. }
  36. }
  37. impl Encodable for NetworkName {
  38. fn encode<S: std::io::Write>(&self, s: S) -> Result<usize> {
  39. let name = self.to_string();
  40. let len = name.encode(s)?;
  41. Ok(len)
  42. }
  43. }
  44. impl Decodable for NetworkName {
  45. fn decode<D: std::io::Read>(mut d: D) -> Result<Self> {
  46. let name: String = Decodable::decode(&mut d)?;
  47. let name = NetworkName::from_str(&name)?;
  48. Ok(name)
  49. }
  50. }