1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! jmap-metadata-client — JMAP Object Metadata extension client helpers.
//!
//! Implements the client-side helpers for
//! [draft-ietf-jmap-metadata-04](https://datatracker.ietf.org/doc/draft-ietf-jmap-metadata/).
//!
//! ## Architecture change from -01 to -02
//!
//! The -02 revision eliminates the standalone `Metadata` object type.
//! Metadata becomes `metadata` and `privateMetadata` properties on
//! each listed JMAP data type. Clients read and write metadata
//! through the existing `Foo/get`, `Foo/set`, `Foo/changes`, and
//! `Foo/query` methods — there are no separate `Metadata/*` methods.
//!
//! This crate no longer provides a `SessionClient` or method bindings.
//! Instead, it provides **pure helper functions** that consumers use
//! alongside their existing extension-client crate calls:
//!
//! - **Property constants** — [`METADATA_PROPERTY`], [`PRIVATE_METADATA_PROPERTY`]
//! for inclusion in `/get` `properties` lists.
//! - **Filter-condition builders** — [`metadata_exists_condition`],
//! [`metadata_text_contains_condition`], [`metadata_text_equals_condition`],
//! and their `private_metadata_*` counterparts, producing JSON values
//! suitable for merging into per-type `FilterCondition` objects.
//! - **`/changes` helpers** — [`IGNORE_METADATA_ONLY_CHANGES`] arg name,
//! [`is_metadata_property`], [`is_metadata_only_change`] for classifying
//! `updatedProperties` in `/changes` responses.
//!
//! All functions are stateless and operate on string/JSON values.
//!
//! # Migration from -01
//!
//! The -01 crate exposed `JmapMetadataExt`, `SessionClient`,
//! `metadata_get`, `metadata_set`, `metadata_changes`, `metadata_query`,
//! `metadata_query_changes`, five `*Params` structs, and `MetadataChangesParams`.
//! All of those are removed in -02. Extension-client crates that previously
//! depended on this crate for standalone `Metadata/*` methods should instead
//! use the helper functions from this crate alongside their own typed method
//! calls (e.g. `email_get` with metadata properties, `email_query` with
//! metadata filter conditions).
//!
//! # Example
//!
//! ```rust
//! use jmap_metadata_client::{
//! METADATA_PROPERTY,
//! PRIVATE_METADATA_PROPERTY,
//! metadata_exists_condition,
//! metadata_text_contains_condition,
//! };
//!
//! // Include metadata properties in a /get properties list
//! let properties = vec!["id", "subject", METADATA_PROPERTY, PRIVATE_METADATA_PROPERTY];
//! assert!(properties.contains(&"metadata"));
//!
//! // Build filter conditions for a /query request
//! let exists = metadata_exists_condition("photography");
//! assert_eq!(exists, serde_json::json!({ "metadataExists": "photography" }));
//!
//! let text_match = metadata_text_contains_condition("photography/caption", "sunset");
//! assert_eq!(text_match, serde_json::json!({
//! "metadataTextContains": { "path": "photography/caption", "value": "sunset" }
//! }));
//! ```
// ---------------------------------------------------------------------------
// Property name constants (§2)
// ---------------------------------------------------------------------------
/// Wire name of the shared metadata property on listed data types (§2).
///
/// Include in the `properties` list of a `/get` request to fetch the
/// per-namespace shared metadata map.
pub const METADATA_PROPERTY: &str = "metadata";
/// Wire name of the per-user private metadata property on listed data
/// types (§2).
///
/// Include in the `properties` list of a `/get` request to fetch the
/// per-namespace per-user metadata map. Only available when the data type's
/// `supportsPrivate` in the account capability is `true` (§1.2.1).
pub const PRIVATE_METADATA_PROPERTY: &str = "privateMetadata";
// ---------------------------------------------------------------------------
// /changes helpers (§3.3)
// ---------------------------------------------------------------------------
/// Wire name of the `ignoreMetadataOnlyChanges` argument for `/changes`
/// requests (draft-ietf-jmap-metadata-04 §3.3).
///
/// When set to `true` in a `/changes` request, objects whose only changes
/// are to `metadata` and/or `privateMetadata` properties are excluded from
/// the `updated` array in the response. The server still reports an id
/// it "cannot determine" to be metadata-only. A request whose `using`
/// lacks `urn:ietf:params:jmap:metadata` behaves as if this were `true`
/// (-04 §3).
pub const IGNORE_METADATA_ONLY_CHANGES: &str = "ignoreMetadataOnlyChanges";
/// Returns `true` if `property` is one of the metadata-extension properties
/// (`metadata` or `privateMetadata`).
///
/// Useful for classifying the `updatedProperties` array in a `/changes`
/// response (draft-ietf-jmap-metadata-04 §3.3) to determine whether a
/// change is metadata-only.
///
/// # Examples
///
/// ```
/// use jmap_metadata_client::is_metadata_property;
///
/// assert!(is_metadata_property("metadata"));
/// assert!(is_metadata_property("privateMetadata"));
/// assert!(!is_metadata_property("subject"));
/// assert!(!is_metadata_property("id"));
/// ```
/// Returns `true` if ALL properties in `updated_properties` are metadata
/// properties (draft-ietf-jmap-metadata-04 §3.3).
///
/// A `/changes` response whose `updatedProperties` is a list indicates
/// that only those properties changed. If every property in the list is
/// a metadata property, the change is metadata-only and may be skipped by
/// clients that don't need metadata state updates.
///
/// A `Mailbox/changes` list may also name the RFC 8621 §2.2 count
/// properties: -04 §3.3 makes it "the union of the count property names
/// and the metadata property names that may have changed", for example
/// `["totalEmails", "metadata"]`. This returns `false` for such a list
/// because a count changed too. A caller that treats "only metadata plus
/// counts changed" as one case must check for the count names itself.
///
/// Returns `false` for an empty slice (no properties ≠ metadata-only).
///
/// # Examples
///
/// ```
/// use jmap_metadata_client::is_metadata_only_change;
///
/// assert!(is_metadata_only_change(&["metadata".to_string()]));
/// assert!(is_metadata_only_change(&["metadata".to_string(), "privateMetadata".to_string()]));
/// assert!(!is_metadata_only_change(&["metadata".to_string(), "subject".to_string()]));
/// assert!(!is_metadata_only_change(&[]));
/// ```
// ---------------------------------------------------------------------------
// Filter-condition builders (§3.6)
// ---------------------------------------------------------------------------
/// Build a `metadataExists` filter condition (§3.6).
///
/// Returns a JSON object `{ "metadataExists": "<path>" }` suitable for
/// merging into a per-type `FilterCondition` object in a `/query` request.
///
/// `path` is a namespace identifier, optionally followed by an RFC 6901
/// JSON Pointer into the namespace value, such as `<namespace>/<key>` or
/// `<namespace>/<key>/<subkey>` (draft-ietf-jmap-metadata-04 §3.6).
///
/// # Examples
///
/// ```
/// use jmap_metadata_client::metadata_exists_condition;
/// use serde_json::json;
///
/// assert_eq!(
/// metadata_exists_condition("photography"),
/// json!({ "metadataExists": "photography" })
/// );
/// assert_eq!(
/// metadata_exists_condition("acme.example.com/memo"),
/// json!({ "metadataExists": "acme.example.com/memo" })
/// );
/// ```
/// Build a `privateMetadataExists` filter condition (§3.6).
///
/// Same as [`metadata_exists_condition`] but targets `privateMetadata`.
///
/// # Examples
///
/// ```
/// use jmap_metadata_client::private_metadata_exists_condition;
/// use serde_json::json;
///
/// assert_eq!(
/// private_metadata_exists_condition("photography"),
/// json!({ "privateMetadataExists": "photography" })
/// );
/// ```
/// Build a `metadataTextContains` filter condition (§3.6).
///
/// Returns a JSON object suitable for merging into a per-type
/// `FilterCondition`. Matches objects where the string value at `path`
/// in the `metadata` map contains `value` as a substring.
///
/// # Examples
///
/// ```
/// use jmap_metadata_client::metadata_text_contains_condition;
/// use serde_json::json;
///
/// assert_eq!(
/// metadata_text_contains_condition("photography/caption", "sunset"),
/// json!({ "metadataTextContains": { "path": "photography/caption", "value": "sunset" } })
/// );
/// ```
/// Build a `privateMetadataTextContains` filter condition (§3.6).
///
/// Same as [`metadata_text_contains_condition`] but targets `privateMetadata`.
///
/// # Examples
///
/// ```
/// use jmap_metadata_client::private_metadata_text_contains_condition;
/// use serde_json::json;
///
/// assert_eq!(
/// private_metadata_text_contains_condition("workflow/note", "urgent"),
/// json!({ "privateMetadataTextContains": { "path": "workflow/note", "value": "urgent" } })
/// );
/// ```
/// Build a `metadataTextEquals` filter condition (§3.6).
///
/// Returns a JSON object suitable for merging into a per-type
/// `FilterCondition`. Matches objects where the string value at `path`
/// in the `metadata` map is exactly equal to `value`.
///
/// # Examples
///
/// ```
/// use jmap_metadata_client::metadata_text_equals_condition;
/// use serde_json::json;
///
/// assert_eq!(
/// metadata_text_equals_condition("photography/rating", "5"),
/// json!({ "metadataTextEquals": { "path": "photography/rating", "value": "5" } })
/// );
/// ```
/// Build a `privateMetadataTextEquals` filter condition (§3.6).
///
/// Same as [`metadata_text_equals_condition`] but targets `privateMetadata`.
///
/// # Examples
///
/// ```
/// use jmap_metadata_client::private_metadata_text_equals_condition;
/// use serde_json::json;
///
/// assert_eq!(
/// private_metadata_text_equals_condition("workflow/status", "approved"),
/// json!({ "privateMetadataTextEquals": { "path": "workflow/status", "value": "approved" } })
/// );
/// ```
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------