use std::collections::BTreeMap;
use crate::error::{AccountError, Result};
use crate::id::{AccountId, ProfileIx};
use crate::registry::{ProfileAnchor, ProfileRegistry};
pub struct Profile {
pub ix: ProfileIx,
anchor: ProfileAnchor,
}
impl Profile {
pub fn new(ix: ProfileIx, anchor: ProfileAnchor) -> Self {
Self { ix, anchor }
}
pub fn ix(&self) -> ProfileIx {
self.ix
}
pub fn anchor(&self) -> &ProfileAnchor {
&self.anchor
}
}
pub struct Account {
id: AccountId,
registry: ProfileRegistry,
resolved: BTreeMap<ProfileIx, Profile>,
}
impl Account {
pub fn new(id: AccountId, registry: ProfileRegistry) -> Self {
Self {
id,
registry,
resolved: BTreeMap::new(),
}
}
pub fn id(&self) -> &AccountId {
&self.id
}
pub fn registry(&self) -> &ProfileRegistry {
&self.registry
}
pub fn registry_mut(&mut self) -> &mut ProfileRegistry {
&mut self.registry
}
pub fn attach_resolved(&mut self, profile: Profile) -> Result<()> {
let ix = profile.ix();
if !self.registry.contains(ix) {
return Err(AccountError::ProfileNotFound(ix));
}
self.resolved.insert(ix, profile);
Ok(())
}
pub fn resolved(&self, ix: ProfileIx) -> Option<&Profile> {
self.resolved.get(&ix)
}
pub fn record(&self) -> AccountRecord {
AccountRecord {
id: self.id.clone(),
profiles: self.registry.clone(),
}
}
#[deprecated(
since = "0.7.0",
note = "an account may have NO active profile; use `registry().active()` and resolve it"
)]
pub fn default_profile(&self) -> Option<&Profile> {
let ix = self.registry.active()?.ix();
self.resolved(ix)
}
#[deprecated(
since = "0.7.0",
note = "use `registry_mut().set_active()`, whose ActiveSwitch the host MUST disclose"
)]
pub fn set_default_profile(&mut self, ix: ProfileIx) -> Result<()> {
self.registry.set_active(ix).map(|_switch| ())
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct AccountRecord {
pub id: AccountId,
pub profiles: ProfileRegistry,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mint::fixtures::bound_mint;
fn profile_at(ix: ProfileIx) -> Profile {
let (did, store) = bound_mint(1);
let anchor = ProfileAnchor::from_confirmed(&did, &store)
.expect("bound_mint yields two halves of ONE mint");
Profile::new(ix, anchor)
}
fn registry_with(ix: ProfileIx) -> ProfileRegistry {
let mut registry = ProfileRegistry::empty();
let (did, store) = bound_mint(1);
registry.record_minted(ix, &did, &store, None).unwrap();
registry
}
#[test]
fn an_account_with_no_profiles_is_representable() {
let account = Account::new(AccountId::new("a"), ProfileRegistry::empty());
assert!(account.registry().is_empty());
assert!(account.registry().active().is_none());
assert!(account.resolved(ProfileIx::ROOT).is_none());
assert_eq!(account.record().profiles, ProfileRegistry::empty());
}
#[test]
fn attaching_a_resolved_profile_for_an_unregistered_index_is_refused() {
let mut account = Account::new(AccountId::new("a"), registry_with(ProfileIx::ROOT));
let result = account.attach_resolved(profile_at(ProfileIx(9)));
assert!(matches!(
result,
Err(AccountError::ProfileNotFound(ProfileIx(9)))
));
assert!(account.resolved(ProfileIx(9)).is_none());
}
#[test]
fn a_resolved_profile_at_a_confirmed_index_is_attached() {
let mut account = Account::new(AccountId::new("a"), registry_with(ProfileIx::ROOT));
account
.attach_resolved(profile_at(ProfileIx::ROOT))
.unwrap();
assert_eq!(
account.resolved(ProfileIx::ROOT).map(Profile::ix),
Some(ProfileIx::ROOT)
);
}
#[test]
#[allow(deprecated)]
fn the_deprecated_default_profile_api_delegates_to_the_registry() {
let mut registry = registry_with(ProfileIx::ROOT);
let (did, store) = bound_mint(3);
registry
.record_minted(ProfileIx(1), &did, &store, None)
.unwrap();
let mut account = Account::new(AccountId::new("a"), registry);
account.attach_resolved(profile_at(ProfileIx(1))).unwrap();
account.set_default_profile(ProfileIx(1)).unwrap();
assert_eq!(account.registry().active().unwrap().ix(), ProfileIx(1));
assert_eq!(
account.default_profile().map(Profile::ix),
Some(ProfileIx(1))
);
}
#[test]
#[allow(deprecated)]
fn set_default_profile_rejects_an_absent_index_and_leaves_the_active_profile_unchanged() {
let mut account = Account::new(AccountId::new("a"), registry_with(ProfileIx::ROOT));
let result = account.set_default_profile(ProfileIx(9));
assert!(matches!(result, Err(AccountError::ProfileNotFound(_))));
assert_eq!(account.registry().active().unwrap().ix(), ProfileIx::ROOT);
}
#[test]
fn account_record_serde_round_trips() {
let record = AccountRecord {
id: AccountId::new("acct"),
profiles: registry_with(ProfileIx(7)),
};
let json = serde_json::to_string(&record).unwrap();
let back: AccountRecord = serde_json::from_str(&json).unwrap();
assert_eq!(record, back);
}
}