use super::{Id, NAddr, PublicKey, RelayUrl};
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum EventReference {
Id {
id: Id,
author: Option<PublicKey>,
relays: Vec<RelayUrl>,
marker: Option<String>,
},
Addr(NAddr),
}
impl EventReference {
pub fn author(&self) -> Option<PublicKey> {
match self {
EventReference::Id { author, .. } => *author,
EventReference::Addr(naddr) => Some(naddr.author),
}
}
pub fn set_author(&mut self, new_author: PublicKey) {
match self {
EventReference::Id { ref mut author, .. } => *author = Some(new_author),
EventReference::Addr(ref mut naddr) => naddr.author = new_author,
}
}
pub fn copy_relays(&self) -> Vec<RelayUrl> {
match self {
EventReference::Id { relays, .. } => relays.clone(),
EventReference::Addr(naddr) => naddr
.relays
.iter()
.filter_map(|r| RelayUrl::try_from_unchecked_url(r).ok())
.collect(),
}
}
pub fn extend_relays(&mut self, relays: Vec<RelayUrl>) {
let mut new_relays = self.copy_relays();
new_relays.extend(relays);
match self {
EventReference::Id { ref mut relays, .. } => *relays = new_relays,
EventReference::Addr(ref mut naddr) => {
naddr.relays = new_relays.iter().map(|r| r.to_unchecked_url()).collect()
}
}
}
}
impl PartialEq for EventReference {
fn eq(&self, other: &Self) -> bool {
match self {
EventReference::Id { id: id1, .. } => {
match other {
EventReference::Id { id: id2, .. } => {
id1 == id2
}
_ => false,
}
}
EventReference::Addr(addr1) => match other {
EventReference::Addr(addr2) => addr1 == addr2,
_ => false,
},
}
}
}
impl Eq for EventReference {}
impl Hash for EventReference {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
EventReference::Id { id, .. } => {
id.hash(state);
}
EventReference::Addr(addr) => {
addr.hash(state);
}
}
}
}