jmap_client/event_source/
mod.rs

1/*
2 * Copyright Stalwart Labs LLC See the COPYING
3 * file at the top-level directory of this distribution.
4 *
5 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
8 * option. This file may not be copied, modified, or distributed
9 * except according to those terms.
10 */
11
12pub mod parser;
13pub mod stream;
14
15use crate::{core::session::URLParser, TypeState};
16use ahash::AHashMap;
17use serde::{Deserialize, Serialize};
18
19pub enum URLParameter {
20    Types,
21    CloseAfter,
22    Ping,
23}
24
25impl URLParser for URLParameter {
26    fn parse(value: &str) -> Option<Self> {
27        match value {
28            "types" => Some(URLParameter::Types),
29            "closeafter" => Some(URLParameter::CloseAfter),
30            "ping" => Some(URLParameter::Ping),
31            _ => None,
32        }
33    }
34}
35
36#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
37pub struct Changes {
38    id: Option<String>,
39    changes: AHashMap<String, AHashMap<TypeState, String>>,
40}
41
42impl Changes {
43    pub fn new(id: Option<String>, changes: AHashMap<String, AHashMap<TypeState, String>>) -> Self {
44        Self { id, changes }
45    }
46
47    pub fn id(&self) -> Option<&str> {
48        self.id.as_deref()
49    }
50
51    pub fn account_changes(&mut self, account_id: &str) -> Option<AHashMap<TypeState, String>> {
52        self.changes.remove(account_id)
53    }
54
55    pub fn changed_accounts(&self) -> impl Iterator<Item = &String> {
56        self.changes.keys()
57    }
58
59    pub fn changes(&self, account_id: &str) -> Option<impl Iterator<Item = (&TypeState, &String)>> {
60        self.changes.get(account_id).map(|changes| changes.iter())
61    }
62
63    pub fn has_type(&self, type_: TypeState) -> bool {
64        self.changes
65            .values()
66            .any(|changes| changes.contains_key(&type_))
67    }
68
69    pub fn into_inner(self) -> AHashMap<String, AHashMap<TypeState, String>> {
70        self.changes
71    }
72
73    pub fn is_empty(&self) -> bool {
74        !self.changes.values().any(|changes| !changes.is_empty())
75    }
76}