use std::collections::BTreeMap;
use super::slot::ProfileSlot;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ProfileFields(BTreeMap<ProfileSlot, String>);
impl ProfileFields {
pub fn new() -> Self {
Self::default()
}
pub fn get(&self, slot: ProfileSlot) -> Option<&str> {
self.0.get(&slot).map(String::as_str)
}
pub fn display_name(&self) -> Option<&str> {
self.get(ProfileSlot::DisplayName)
}
pub fn bio(&self) -> Option<&str> {
self.get(ProfileSlot::Bio)
}
pub fn xch_address(&self) -> Option<&str> {
self.get(ProfileSlot::XchAddress)
}
pub fn avatar_image(&self) -> Option<&str> {
self.get(ProfileSlot::AvatarImage)
}
pub fn banner_image(&self) -> Option<&str> {
self.get(ProfileSlot::BannerImage)
}
pub fn iter(&self) -> impl Iterator<Item = (ProfileSlot, &str)> {
self.0.iter().map(|(slot, text)| (*slot, text.as_str()))
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub(crate) fn insert(&mut self, slot: ProfileSlot, text: String) {
self.0.insert(slot, text);
}
pub(crate) fn remove(&mut self, slot: ProfileSlot) {
self.0.remove(&slot);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_absent_slot_is_distinguishable_from_one_published_as_empty_text() {
let mut fields = ProfileFields::new();
fields.insert(ProfileSlot::Bio, String::new());
assert_eq!(fields.get(ProfileSlot::Bio), Some(""));
assert_eq!(fields.get(ProfileSlot::DisplayName), None);
assert_eq!(fields.len(), 1);
assert!(!fields.is_empty());
}
#[test]
fn iteration_is_in_schema_id_order_whatever_order_slots_arrived_in() {
let mut fields = ProfileFields::new();
fields.insert(ProfileSlot::XchAddress, "xch1abc".into());
fields.insert(ProfileSlot::DisplayName, "ada".into());
fields.insert(ProfileSlot::Bio, "counts things".into());
let order: Vec<ProfileSlot> = fields.iter().map(|(slot, _)| slot).collect();
assert_eq!(
order,
vec![
ProfileSlot::DisplayName,
ProfileSlot::Bio,
ProfileSlot::XchAddress
]
);
}
}