arminius_lib_sync/syncv4/
assemble.rs1use std::collections::{BTreeMap, HashMap};
8
9use arminius_lib_component::error::{InternalServerError, ResultExt as _};
10use ruma::{UInt, api::client::sync::sync_events};
11
12use crate::syncv4::execute::Persisted;
13
14fn convert_lists_counts(
16 lists_counts: HashMap<String, usize>,
17) -> Result<BTreeMap<String, sync_events::v5::response::List>, InternalServerError> {
18 lists_counts
19 .into_iter()
20 .map(|(list_name, list_count)| {
21 let mut list = sync_events::v5::response::List::default();
22
23 list.count = UInt::try_from(list_count).unrecoverable_error("Failed to create UInt")?;
24
25 Ok::<_, InternalServerError>((list_name, list))
26 })
27 .collect::<Result<BTreeMap<_, _>, _>>()
28}
29
30impl Persisted {
31 pub fn assemble(
33 self,
34 txn_id: Option<String>,
35 ) -> Result<sync_events::v5::Response, InternalServerError> {
36 let mut response = sync_events::v5::Response::new(self.pos);
37
38 response.txn_id = txn_id;
39 response.lists = convert_lists_counts(self.lists_counts)?;
40 response.rooms = self.rooms;
41
42 Ok(response)
43 }
44}