lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README | LICENSE

method.rs (2655B)


      1 use crate::error::RadrootsNostrConnectError;
      2 use serde::{Deserialize, Deserializer, Serialize, Serializer};
      3 use std::fmt;
      4 use std::str::FromStr;
      5 
      6 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
      7 pub enum RadrootsNostrConnectMethod {
      8     Connect,
      9     GetPublicKey,
     10     GetSessionCapability,
     11     SignEvent,
     12     Nip04Encrypt,
     13     Nip04Decrypt,
     14     Nip44Encrypt,
     15     Nip44Decrypt,
     16     Ping,
     17     SwitchRelays,
     18     Custom(String),
     19 }
     20 
     21 impl RadrootsNostrConnectMethod {
     22     pub fn as_str(&self) -> &str {
     23         match self {
     24             Self::Connect => "connect",
     25             Self::GetPublicKey => "get_public_key",
     26             Self::GetSessionCapability => "get_session_capability",
     27             Self::SignEvent => "sign_event",
     28             Self::Nip04Encrypt => "nip04_encrypt",
     29             Self::Nip04Decrypt => "nip04_decrypt",
     30             Self::Nip44Encrypt => "nip44_encrypt",
     31             Self::Nip44Decrypt => "nip44_decrypt",
     32             Self::Ping => "ping",
     33             Self::SwitchRelays => "switch_relays",
     34             Self::Custom(value) => value.as_str(),
     35         }
     36     }
     37 }
     38 
     39 impl fmt::Display for RadrootsNostrConnectMethod {
     40     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     41         f.write_str(self.as_str())
     42     }
     43 }
     44 
     45 impl FromStr for RadrootsNostrConnectMethod {
     46     type Err = RadrootsNostrConnectError;
     47 
     48     fn from_str(value: &str) -> Result<Self, Self::Err> {
     49         match value {
     50             "connect" => Ok(Self::Connect),
     51             "get_public_key" => Ok(Self::GetPublicKey),
     52             "get_session_capability" => Ok(Self::GetSessionCapability),
     53             "sign_event" => Ok(Self::SignEvent),
     54             "nip04_encrypt" => Ok(Self::Nip04Encrypt),
     55             "nip04_decrypt" => Ok(Self::Nip04Decrypt),
     56             "nip44_encrypt" => Ok(Self::Nip44Encrypt),
     57             "nip44_decrypt" => Ok(Self::Nip44Decrypt),
     58             "ping" => Ok(Self::Ping),
     59             "switch_relays" => Ok(Self::SwitchRelays),
     60             other if !other.trim().is_empty() => Ok(Self::Custom(other.to_owned())),
     61             _ => Err(RadrootsNostrConnectError::InvalidMethod(value.to_owned())),
     62         }
     63     }
     64 }
     65 
     66 impl Serialize for RadrootsNostrConnectMethod {
     67     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
     68     where
     69         S: Serializer,
     70     {
     71         serializer.serialize_str(self.as_str())
     72     }
     73 }
     74 
     75 impl<'de> Deserialize<'de> for RadrootsNostrConnectMethod {
     76     fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
     77     where
     78         D: Deserializer<'de>,
     79     {
     80         let value = String::deserialize(deserializer)?;
     81         Self::from_str(&value).map_err(serde::de::Error::custom)
     82     }
     83 }