use std::collections::BTreeMap;
use dig_social_profile::{SlotEdit, Value};
use super::fields::ProfileFields;
use super::slot::ProfileSlot;
#[derive(Debug, Clone, PartialEq, Eq)]
enum SlotChange {
Set(String),
Remove,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ProfileEdit(BTreeMap<ProfileSlot, SlotChange>);
impl ProfileEdit {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn set(mut self, slot: ProfileSlot, text: impl Into<String>) -> Self {
self.0.insert(slot, SlotChange::Set(text.into()));
self
}
#[must_use]
pub fn remove(mut self, slot: ProfileSlot) -> Self {
self.0.insert(slot, SlotChange::Remove);
self
}
#[must_use]
pub fn with_display_name(self, name: impl Into<String>) -> Self {
self.set(ProfileSlot::DisplayName, name)
}
#[must_use]
pub fn with_bio(self, bio: impl Into<String>) -> Self {
self.set(ProfileSlot::Bio, bio)
}
#[must_use]
pub fn with_xch_address(self, address: impl Into<String>) -> Self {
self.set(ProfileSlot::XchAddress, address)
}
#[must_use]
pub fn set_avatar_image(self, data_url: impl Into<String>) -> Self {
self.set(ProfileSlot::AvatarImage, data_url)
}
#[must_use]
pub fn clear_avatar_image(self) -> Self {
self.remove(ProfileSlot::AvatarImage)
}
#[must_use]
pub fn set_banner_image(self, data_url: impl Into<String>) -> Self {
self.set(ProfileSlot::BannerImage, data_url)
}
#[must_use]
pub fn clear_banner_image(self) -> Self {
self.remove(ProfileSlot::BannerImage)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn preview(&self, current: &ProfileFields) -> ProfileFields {
let mut next = current.clone();
for (slot, change) in &self.0 {
match change {
SlotChange::Set(text) => next.insert(*slot, text.clone()),
SlotChange::Remove => next.remove(*slot),
}
}
next
}
pub(crate) fn slot_edits(&self) -> Vec<SlotEdit> {
self.0
.iter()
.map(|(slot, change)| match change {
SlotChange::Set(text) => SlotEdit::Set(slot.slot_id(), Value::Utf8(text.clone())),
SlotChange::Remove => SlotEdit::Remove(slot.slot_id()),
})
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_last_change_to_a_slot_is_the_one_that_survives() {
let set_then_removed = ProfileEdit::new()
.with_bio("first")
.remove(ProfileSlot::Bio);
let removed_then_set = ProfileEdit::new().remove(ProfileSlot::Bio).with_bio("last");
assert_eq!(set_then_removed.len(), 1);
assert_eq!(removed_then_set.len(), 1);
assert_eq!(
set_then_removed.slot_edits(),
vec![SlotEdit::Remove(ProfileSlot::Bio.slot_id())]
);
assert_eq!(
removed_then_set.slot_edits(),
vec![SlotEdit::Set(
ProfileSlot::Bio.slot_id(),
Value::Utf8("last".into())
)]
);
}
#[test]
fn a_preview_applies_sets_and_removals_in_the_same_batch() {
let mut current = ProfileFields::new();
current.insert(ProfileSlot::DisplayName, "ada".into());
current.insert(ProfileSlot::Bio, "counts things".into());
current.insert(ProfileSlot::Location, "london".into());
let next = ProfileEdit::new()
.with_bio("writes notes")
.remove(ProfileSlot::Location)
.with_xch_address("xch1abc")
.preview(¤t);
assert_eq!(next.display_name(), Some("ada"), "untouched slot survives");
assert_eq!(next.bio(), Some("writes notes"), "set slot advances");
assert_eq!(
next.get(ProfileSlot::Location),
None,
"removed slot is gone"
);
assert_eq!(next.xch_address(), Some("xch1abc"), "new slot appears");
assert_eq!(current.get(ProfileSlot::Location), Some("london"));
}
#[test]
fn an_empty_batch_produces_no_slot_edits() {
assert!(ProfileEdit::new().is_empty());
assert!(ProfileEdit::new().slot_edits().is_empty());
}
}