use crate::error::Result;
use crate::types::IdentifierKind;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Recipient {
Address(String),
InboxId(String),
Ens(String),
}
impl Recipient {
#[must_use]
pub fn parse(input: &str) -> Self {
let s = input.trim();
if s.len() == 42
&& s.starts_with("0x")
&& s.as_bytes()[2..].iter().all(u8::is_ascii_hexdigit)
{
Self::Address(s.to_lowercase())
} else if s.contains('.') {
Self::Ens(s.to_owned())
} else {
Self::InboxId(s.to_owned())
}
}
}
impl From<&str> for Recipient {
fn from(s: &str) -> Self {
Self::parse(s)
}
}
impl From<String> for Recipient {
fn from(s: String) -> Self {
Self::parse(&s)
}
}
impl From<crate::types::AccountIdentifier> for Recipient {
fn from(id: crate::types::AccountIdentifier) -> Self {
match id.kind {
IdentifierKind::Ethereum => Self::Address(id.address),
IdentifierKind::Passkey => Self::InboxId(id.address),
}
}
}
impl std::fmt::Display for Recipient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Address(a) => f.write_str(a),
Self::InboxId(id) => f.write_str(id),
Self::Ens(name) => f.write_str(name),
}
}
}
pub trait Resolver: Send + Sync {
fn resolve(&self, name: &str) -> Result<String>;
fn reverse_resolve(&self, _address: &str) -> Result<Option<String>> {
Ok(None)
}
}