use dig_social_profile::slot::standard;
use dig_social_profile::SlotId;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ProfileSlot {
DisplayName,
Bio,
Avatar,
Banner,
Pronouns,
Location,
Links,
XchAddress,
AvatarImage,
BannerImage,
}
impl Ord for ProfileSlot {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.id().cmp(&other.id())
}
}
impl PartialOrd for ProfileSlot {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl ProfileSlot {
pub const ALL: [Self; 10] = [
Self::DisplayName,
Self::Bio,
Self::Avatar,
Self::Banner,
Self::Pronouns,
Self::Location,
Self::Links,
Self::XchAddress,
Self::AvatarImage,
Self::BannerImage,
];
pub fn id(self) -> u16 {
self.slot_id().0
}
pub fn from_id(id: u16) -> Option<Self> {
Self::ALL.into_iter().find(|slot| slot.id() == id)
}
pub fn key(self) -> &'static str {
match self {
Self::DisplayName => "display_name",
Self::Bio => "bio",
Self::Avatar => "avatar",
Self::Banner => "banner",
Self::Pronouns => "pronouns",
Self::Location => "location",
Self::Links => "links",
Self::XchAddress => "xch_address",
Self::AvatarImage => "avatar_image",
Self::BannerImage => "banner_image",
}
}
pub(crate) fn slot_id(self) -> SlotId {
match self {
Self::DisplayName => standard::DISPLAY_NAME,
Self::Bio => standard::BIO,
Self::Avatar => standard::AVATAR,
Self::Banner => standard::BANNER,
Self::Pronouns => standard::PRONOUNS,
Self::Location => standard::LOCATION,
Self::Links => standard::LINKS,
Self::XchAddress => standard::XCH_ADDRESS,
Self::AvatarImage => standard::AVATAR_INLINE,
Self::BannerImage => standard::BANNER_INLINE,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_variant_carries_the_schema_slot_id_not_its_position() {
assert_eq!(ProfileSlot::DisplayName.id(), standard::DISPLAY_NAME.0);
assert_eq!(ProfileSlot::Bio.id(), standard::BIO.0);
assert_eq!(ProfileSlot::Avatar.id(), standard::AVATAR.0);
assert_eq!(ProfileSlot::Banner.id(), standard::BANNER.0);
assert_eq!(ProfileSlot::Pronouns.id(), standard::PRONOUNS.0);
assert_eq!(ProfileSlot::Location.id(), standard::LOCATION.0);
assert_eq!(ProfileSlot::Links.id(), standard::LINKS.0);
assert_eq!(ProfileSlot::XchAddress.id(), standard::XCH_ADDRESS.0);
assert_eq!(ProfileSlot::AvatarImage.id(), standard::AVATAR_INLINE.0);
assert_eq!(ProfileSlot::BannerImage.id(), standard::BANNER_INLINE.0);
}
#[test]
fn a_standard_id_round_trips_and_a_non_standard_one_is_refused() {
for slot in ProfileSlot::ALL {
assert_eq!(ProfileSlot::from_id(slot.id()), Some(slot));
}
assert_eq!(ProfileSlot::from_id(standard::SCHEMA_VERSION.0), None);
assert_eq!(ProfileSlot::from_id(standard::BLS_G1_PUBLIC_KEY.0), None);
assert_eq!(ProfileSlot::from_id(standard::PEER_ID.0), None);
assert_eq!(ProfileSlot::from_id(0xBEEF), None);
}
#[test]
fn keys_and_ids_are_unique_across_every_slot() {
let ids: std::collections::BTreeSet<u16> =
ProfileSlot::ALL.iter().map(|s| s.id()).collect();
let keys: std::collections::BTreeSet<&str> =
ProfileSlot::ALL.iter().map(|s| s.key()).collect();
assert_eq!(ids.len(), ProfileSlot::ALL.len());
assert_eq!(keys.len(), ProfileSlot::ALL.len());
}
}