jmap_client/email/
search_snippet.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
12use serde::{Deserialize, Serialize};
13
14use crate::core::{query::Filter, request::ResultReference, RequestParams};
15
16#[derive(Deserialize, Clone, Debug)]
17pub struct SearchSnippet {
18    #[serde(rename = "emailId")]
19    email_id: String,
20    subject: Option<String>,
21    preview: Option<String>,
22}
23
24#[derive(Debug, Clone, Serialize)]
25pub struct SearchSnippetGetRequest {
26    #[serde(rename = "accountId")]
27    account_id: String,
28
29    #[serde(rename = "filter")]
30    #[serde(skip_serializing_if = "Option::is_none")]
31    filter: Option<Filter<super::query::Filter>>,
32
33    #[serde(rename = "emailIds")]
34    #[serde(skip_serializing_if = "Option::is_none")]
35    email_ids: Option<Vec<String>>,
36
37    #[serde(rename = "#emailIds")]
38    #[serde(skip_serializing_if = "Option::is_none")]
39    email_ids_ref: Option<ResultReference>,
40}
41
42#[derive(Debug, Clone, Deserialize)]
43pub struct SearchSnippetGetResponse {
44    #[serde(rename = "accountId")]
45    account_id: String,
46
47    #[serde(rename = "list")]
48    list: Vec<SearchSnippet>,
49
50    #[serde(rename = "notFound")]
51    not_found: Option<Vec<String>>,
52}
53
54impl SearchSnippetGetRequest {
55    pub fn new(params: RequestParams) -> Self {
56        SearchSnippetGetRequest {
57            account_id: params.account_id,
58            filter: None,
59            email_ids: None,
60            email_ids_ref: None,
61        }
62    }
63
64    pub fn filter(&mut self, filter: impl Into<Filter<super::query::Filter>>) -> &mut Self {
65        self.filter = Some(filter.into());
66        self
67    }
68
69    pub fn email_id(&mut self, email_id: impl Into<String>) -> &mut Self {
70        self.email_ids
71            .get_or_insert_with(Vec::new)
72            .push(email_id.into());
73        self
74    }
75
76    pub fn email_ids(
77        &mut self,
78        email_ids: impl IntoIterator<Item = impl Into<String>>,
79    ) -> &mut Self {
80        self.email_ids
81            .get_or_insert_with(Vec::new)
82            .extend(email_ids.into_iter().map(|id| id.into()));
83        self
84    }
85
86    pub fn email_ids_ref(&mut self, reference: ResultReference) -> &mut Self {
87        self.email_ids_ref = reference.into();
88        self.email_ids = None;
89        self
90    }
91}
92
93impl SearchSnippet {
94    pub fn email_id(&self) -> &str {
95        &self.email_id
96    }
97
98    pub fn subject(&self) -> Option<&str> {
99        self.subject.as_deref()
100    }
101
102    pub fn preview(&self) -> Option<&str> {
103        self.preview.as_deref()
104    }
105}
106
107impl SearchSnippetGetResponse {
108    pub fn account_id(&self) -> &str {
109        &self.account_id
110    }
111
112    pub fn snippet(&self, id: &str) -> Option<&SearchSnippet> {
113        self.list.iter().find(|snippet| snippet.email_id == id)
114    }
115
116    pub fn list(&self) -> &[SearchSnippet] {
117        &self.list
118    }
119
120    pub fn not_found(&self) -> Option<&[String]> {
121        self.not_found.as_deref()
122    }
123}