io_jmap/rfc8621/thread/changes.rs
1//! JMAP `Thread/changes` coroutine (RFC 8621 ยง3.2): wraps the generic
2//! [`JmapChanges`] with the JMAP-Mail capability set.
3//!
4//! # Example
5//!
6//! ```rust,no_run
7//! use std::{
8//! io::{Read, Write},
9//! net::TcpStream,
10//! };
11//!
12//! use io_jmap::{
13//! coroutine::{JmapCoroutine, JmapCoroutineState, JmapYield},
14//! rfc8620::session::JmapSession,
15//! rfc8621::thread::changes::{JmapThreadChanges, JmapThreadChangesOptions},
16//! };
17//! use secrecy::SecretString;
18//!
19//! // Ready stream needed (TCP-connected, TLS-negociated)
20//! let mut stream = TcpStream::connect("api.example.com:443").unwrap();
21//! let mut buf = [0u8; 4096];
22//!
23//! let session: JmapSession = serde_json::from_str(r#"{
24//! "username": "",
25//! "accounts": {},
26//! "primaryAccounts": {"urn:ietf:params:jmap:mail": "a1"},
27//! "capabilities": {},
28//! "apiUrl": "https://api.example.com/jmap/",
29//! "downloadUrl": "",
30//! "uploadUrl": "",
31//! "eventSourceUrl": "",
32//! "state": ""
33//! }"#).unwrap();
34//! let auth = SecretString::from("Bearer xyz");
35//! let mut coroutine =
36//! JmapThreadChanges::new(&session, &auth, "s1", JmapThreadChangesOptions::default())
37//! .unwrap();
38//! let mut arg = None;
39//!
40//! let out = loop {
41//! match coroutine.resume(arg.take()) {
42//! JmapCoroutineState::Yielded(JmapYield::WantsWrite(bytes)) => {
43//! stream.write_all(&bytes).unwrap();
44//! }
45//! JmapCoroutineState::Yielded(JmapYield::WantsRead) => {
46//! let n = stream.read(&mut buf).unwrap();
47//! arg = Some(&buf[..n]);
48//! }
49//! JmapCoroutineState::Complete(Ok(out)) => break out,
50//! JmapCoroutineState::Complete(Err(err)) => panic!("{err}"),
51//! }
52//! };
53//!
54//! println!("new state {}", out.new_state);
55//! ```
56
57use alloc::{string::String, vec};
58
59use secrecy::SecretString;
60use thiserror::Error;
61
62use crate::{
63 coroutine::*,
64 jmap_try,
65 rfc8620::{JMAP_CORE_CAPABILITY, changes::*, session::JmapSession},
66 rfc8621::JMAP_MAIL_CAPABILITY,
67};
68
69/// Failure causes during a JMAP `Thread/changes` flow.
70#[derive(Debug, Error)]
71pub enum JmapThreadChangesError {
72 /// The inner generic changes coroutine failed.
73 #[error("JMAP Thread/changes failed: {0}")]
74 Changes(#[from] JmapChangesError),
75}
76
77/// Options for [`JmapThreadChanges::new`].
78#[derive(Clone, Debug, Default)]
79pub struct JmapThreadChangesOptions {
80 /// Server-side cap on the number of changes returned.
81 pub max_changes: Option<u64>,
82}
83
84/// I/O-free coroutine for the JMAP `Thread/changes` method.
85pub struct JmapThreadChanges {
86 state: State,
87}
88
89impl JmapThreadChanges {
90 /// Prepares the method call request and builds the coroutine.
91 pub fn new(
92 session: &JmapSession,
93 http_auth: &SecretString,
94 since_state: impl Into<String>,
95 opts: JmapThreadChangesOptions,
96 ) -> Result<Self, JmapThreadChangesError> {
97 let account_id = session
98 .primary_accounts
99 .get(JMAP_MAIL_CAPABILITY)
100 .cloned()
101 .unwrap_or_default();
102 let api_url = &session.api_url;
103
104 Ok(Self {
105 state: State::Changes(JmapChanges::new(
106 account_id,
107 http_auth,
108 api_url,
109 "Thread/changes",
110 vec![JMAP_CORE_CAPABILITY.into(), JMAP_MAIL_CAPABILITY.into()],
111 since_state,
112 JmapChangesOptions {
113 max_changes: opts.max_changes,
114 },
115 )?),
116 })
117 }
118}
119
120impl JmapCoroutine for JmapThreadChanges {
121 type Yield = JmapYield;
122 type Return = Result<JmapChangesOutput, JmapThreadChangesError>;
123
124 fn resume(&mut self, arg: Option<&[u8]>) -> JmapCoroutineState<Self::Yield, Self::Return> {
125 match &mut self.state {
126 State::Changes(changes) => {
127 let out = jmap_try!(changes, arg);
128 JmapCoroutineState::Complete(Ok(out))
129 }
130 }
131 }
132}
133
134enum State {
135 Changes(JmapChanges),
136}