libchat/extensions/components/src/contact_registry.rs
Jazz Turner-Baggs 3245498438
Add GroupV1 + InboxV2 (#92)
* Add GroupV1

* Clean warnings

* Remove dead test

* Re-use components in integration tests

* Remove deadcode

* undo import fixes

* tidy

* Update Accounts + service_traits

* Remove ClientCtx

* Remove duplicate test_utils

* Wrap constructor in result

* Warning fixups

* Appease clippy

* Update comments

* Update todo

* Clean up warnings

* Avoid panic

* Fix libchat import in chat-cli

* Add InboxV2 comment

* Add comments to GroupV1Convo

* Update doc comments

* reduce visibility

* Doc Integration tests

* Hashlen update

* remove type alias for ProtocolParams

* Remove stray printlines

* Review fixes

* PR review changes

* Add trait comments

* chat_proto import paths

* PR Feedback fixes

* Update CliClient

* Update CLI DeliveryService impls
2026-05-19 11:54:54 -07:00

77 lines
1.9 KiB
Rust

use std::{
collections::HashMap,
fmt::Debug,
sync::{Arc, Mutex},
};
use libchat::{AccountId, RegistrationService};
/// A Contact Registry used for Tests.
/// This implementation stores bundle bytes and then returns them when
/// retrieved
///
#[derive(Clone)]
pub struct EphemeralRegistry {
registry: Arc<Mutex<HashMap<String, Vec<u8>>>>,
}
impl EphemeralRegistry {
pub fn new() -> Self {
Self {
registry: Arc::new(Mutex::new(HashMap::new())),
}
}
}
impl Default for EphemeralRegistry {
fn default() -> Self {
Self::new()
}
}
impl Debug for EphemeralRegistry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let registry = self.registry.lock().unwrap();
let truncated: Vec<(&String, String)> = registry
.iter()
.map(|(k, v)| {
let hex = if v.len() <= 8 {
hex::encode(v)
} else {
format!(
"{}..{}",
hex::encode(&v[..4]),
hex::encode(&v[v.len() - 4..])
)
};
(k, hex)
})
.collect();
f.debug_struct("EphemeralRegistry")
.field("registry", &truncated)
.finish()
}
}
impl RegistrationService for EphemeralRegistry {
type Error = String;
fn register(&mut self, identity: &str, key_bundle: Vec<u8>) -> Result<(), Self::Error> {
self.registry
.lock()
.unwrap()
.insert(identity.to_string(), key_bundle);
Ok(())
}
fn retrieve(&self, identity: &AccountId) -> Result<Option<Vec<u8>>, Self::Error> {
Ok(self
.registry
.lock()
.unwrap()
.get(identity.as_str())
.cloned())
}
}