use std::fmt;
use bitcoin_hashes::sha256;
use secp256k1::XOnlyPublicKey;
pub enum TagKind {
P,
E,
Nonce,
}
impl fmt::Display for TagKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::P => write!(f, "p"),
Self::E => write!(f, "e"),
Self::Nonce => write!(f, "nonce"),
}
}
}
pub enum TagData {
Generic(TagKind, Vec<String>),
EventId(sha256::Hash),
PubKey(XOnlyPublicKey),
ContactList {
pk: XOnlyPublicKey,
relay_url: String,
alias: String,
},
POW {
nonce: u128,
difficulty: u8,
},
}
impl From<TagData> for Vec<String> {
fn from(data: TagData) -> Self {
match data {
TagData::Generic(kind, data) => vec![vec![kind.to_string()], data].concat(),
TagData::EventId(id) => vec![TagKind::E.to_string(), id.to_string()],
TagData::PubKey(pk) => vec![TagKind::P.to_string(), pk.to_string()],
TagData::ContactList {
pk,
relay_url,
alias,
} => vec![TagKind::P.to_string(), pk.to_string(), relay_url, alias],
TagData::POW { nonce, difficulty } => vec![
TagKind::Nonce.to_string(),
nonce.to_string(),
difficulty.to_string(),
],
}
}
}
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
pub struct Tag(Vec<String>);
impl Tag {
pub fn new(data: TagData) -> Self {
Self(data.into())
}
pub fn kind(&self) -> &str {
&self.0[0]
}
pub fn content(&self) -> &str {
&self.0[1]
}
}