use alloc::vec::Vec;
use core::fmt;
use crate::types::RelayUrl;
use crate::{Event, Kind, PublicKey, Tag, TagStandard};
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
InvalidLength,
InvalidKind,
IdentifierTagNotFound,
MismatchedBadgeDefinitionOrAward,
BadgeAwardsLackAwardedPublicKey,
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidLength => f.write_str("invalid length"),
Self::InvalidKind => f.write_str("invalid kind"),
Self::IdentifierTagNotFound => f.write_str("identifier tag not found"),
Self::MismatchedBadgeDefinitionOrAward => f.write_str("mismatched badge definition/award"),
Self::BadgeAwardsLackAwardedPublicKey => f.write_str("badge award events lack the awarded public keybadge award events lack the awarded public key"),
}
}
}
#[inline]
pub(crate) fn filter_for_kind(events: Vec<Event>, kind_needed: &Kind) -> Vec<Event> {
events
.into_iter()
.filter(|e| &e.kind == kind_needed)
.collect()
}
pub(crate) fn extract_awarded_public_key<'a>(
tags: &'a [Tag],
awarded_public_key: &PublicKey,
) -> Option<(&'a PublicKey, &'a Option<RelayUrl>)> {
tags.iter().find_map(|t| match t.as_standardized() {
Some(TagStandard::PublicKey {
public_key,
relay_url,
..
}) if public_key == awarded_public_key => Some((public_key, relay_url)),
_ => None,
})
}