Skip to main content

io_email/envelope/jmap/
diff.rs

1//! Helpers for incremental envelope sync via Email/changes (RFC 8620
2//! §5.2), used by [`crate::client::EmailClientStd::diff_envelopes`].
3
4use alloc::{string::String, vec::Vec};
5
6use io_jmap::{
7    client::JmapClientStdError,
8    rfc8620::{JmapMethodError, changes::JmapChangesError},
9    rfc8621::email::changes::JmapEmailChangesError,
10};
11
12/// Email/state is opaque; the encode/decode pair mirrors the IMAP
13/// side for symmetry.
14pub fn encode(state: &str) -> Vec<u8> {
15    state.as_bytes().to_vec()
16}
17
18/// Reverse of [`encode`]; `None` on invalid UTF-8.
19pub fn decode(bytes: &[u8]) -> Option<String> {
20    core::str::from_utf8(bytes).ok().map(String::from)
21}
22
23/// True for the spec-defined cannotCalculateChanges signal (RFC 8620
24/// §5.2); other errors propagate.
25pub fn is_cannot_calculate_changes(err: &JmapClientStdError) -> bool {
26    let JmapClientStdError::EmailChanges(JmapEmailChangesError::Changes(JmapChangesError::Method(
27        method,
28    ))) = err
29    else {
30        return false;
31    };
32    matches!(method, JmapMethodError::CannotCalculateChanges { .. })
33}