From: Ian Jackson Date: Sun, 25 Oct 2020 10:34:55 +0000 (+0000) Subject: wip new account etc., adding acctid etc. X-Git-Tag: otter-0.2.0~595 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=a5891765601eca62c20111b02701e99f3b0d9824;p=otter.git wip new account etc., adding acctid etc. Signed-off-by: Ian Jackson --- diff --git a/src/accounts.rs b/src/accounts.rs index ff962c40..9a0b0215 100644 --- a/src/accounts.rs +++ b/src/accounts.rs @@ -256,20 +256,26 @@ impl AccountRecord { #[throws(MgmtError)] pub fn insert_entry(account: AccountName, _auth: Authorisation, - data: AccountRecord) + new_record: AccountRecord) { let accounts = ACCOUNTS.write(); use hash_map::Entry::*; - match accounts.names.get(account) { - Occupied(oe) => accounts.records.oe.value() - - .get_or_insert_with(default).entry(account); - use hash_map::Entry::*; - let ve = match entry { - Occupied(_) => throw!(ME::AlreadyExists), - Vacant(ve) => ve, - }; - ve.insert(data); + let accounts = accounts.get_or_insert_with(default); + let name_entry = accounts.names.entry(account); + if_chain!{ + if let Occupied(oe) = name_entry; + let acctid = *oe.get(); + if let Some(old_record) = accounts.records.get_mut(acctid); + then { + *old_record = new_record; + } else { + let acctid = accounts.records.insert(new_record); + match name_entry { + Vacant(ve) => { ve.insert(acctid); } + Occupied(oe) => { oe.insert(acctid); } + } + } + } save_accounts_now()?; }