summaryrefslogtreecommitdiff
path: root/crates/jmap-proto/src/method/copy.rs
blob: d103c8d383998fff1cf52115739b841121d8c11e (plain)
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
use serde::Serialize;
use utils::map::vec_map::VecMap;

use crate::{
    error::{method::MethodError, set::SetError},
    object::Object,
    parser::{json::Parser, Error, JsonObjectParser, Token},
    request::{method::MethodObject, reference::MaybeReference, RequestProperty},
    types::{
        blob::BlobId,
        id::Id,
        state::State,
        value::{SetValue, Value},
    },
};

#[derive(Debug, Clone)]
pub struct CopyRequest {
    pub from_account_id: Id,
    pub if_from_in_state: Option<State>,
    pub account_id: Id,
    pub if_in_state: Option<State>,
    pub create: VecMap<MaybeReference<Id, String>, Object<SetValue>>,
    pub on_success_destroy_original: Option<bool>,
    pub destroy_from_if_in_state: Option<State>,
    pub arguments: RequestArguments,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct CopyResponse {
    #[serde(rename = "fromAccountId")]
    pub from_account_id: Id,

    #[serde(rename = "accountId")]
    pub account_id: Id,

    #[serde(rename = "oldState")]
    pub old_state: State,

    #[serde(rename = "newState")]
    pub new_state: State,

    #[serde(rename = "created")]
    #[serde(skip_serializing_if = "VecMap::is_empty")]
    pub created: VecMap<Id, Object<Value>>,

    #[serde(rename = "notCreated")]
    #[serde(skip_serializing_if = "VecMap::is_empty")]
    pub not_created: VecMap<Id, SetError>,
}

#[derive(Debug, Clone)]
pub struct CopyBlobRequest {
    pub from_account_id: Id,
    pub account_id: Id,
    pub blob_ids: Vec<BlobId>,
}

#[derive(Debug, Clone, Serialize)]
pub struct CopyBlobResponse {
    #[serde(rename = "fromAccountId")]
    pub from_account_id: Id,

    #[serde(rename = "accountId")]
    pub account_id: Id,

    #[serde(rename = "copied")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub copied: Option<VecMap<BlobId, BlobId>>,

    #[serde(rename = "notCopied")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub not_copied: Option<VecMap<BlobId, SetError>>,
}

#[derive(Debug, Clone)]
pub enum RequestArguments {
    Email,
}

impl JsonObjectParser for CopyRequest {
    fn parse(parser: &mut Parser) -> crate::parser::Result<Self>
    where
        Self: Sized,
    {
        let mut request = CopyRequest {
            arguments: match &parser.ctx {
                MethodObject::Email => RequestArguments::Email,
                _ => {
                    return Err(Error::Method(MethodError::UnknownMethod(format!(
                        "{}/copy",
                        parser.ctx
                    ))))
                }
            },
            account_id: Id::default(),
            if_in_state: None,
            from_account_id: Id::default(),
            if_from_in_state: None,
            create: VecMap::default(),
            on_success_destroy_original: None,
            destroy_from_if_in_state: None,
        };

        parser
            .next_token::<String>()?
            .assert_jmap(Token::DictStart)?;

        while {
            let property = parser.next_dict_key::<RequestProperty>()?;
            match &property.hash[0] {
                0x6449_746e_756f_6363_61 => {
                    request.account_id = parser.next_token::<Id>()?.unwrap_string("accountId")?;
                }
                0x6574_6165_7263 => {
                    request.create =
                        <VecMap<MaybeReference<Id, String>, Object<SetValue>>>::parse(parser)?;
                }
                0x6449_746e_756f_6363_416d_6f72_66 => {
                    request.from_account_id =
                        parser.next_token::<Id>()?.unwrap_string("fromAccountId")?;
                }
                0x6574_6174_536e_496d_6f72_4666_69 => {
                    request.if_from_in_state = parser
                        .next_token::<State>()?
                        .unwrap_string_or_null("ifFromInState")?;
                }
                0x796f_7274_7365_4473_7365_6363_7553_6e6f => {
                    request.on_success_destroy_original = parser
                        .next_token::<String>()?
                        .unwrap_bool_or_null("onSuccessDestroyOriginal")?;
                }
                0x536e_4966_496d_6f72_4679_6f72_7473_6564 => {
                    request.destroy_from_if_in_state = parser
                        .next_token::<State>()?
                        .unwrap_string_or_null("destroyFromIfInState")?;
                }
                0x6574_6174_536e_4966_69 => {
                    request.if_in_state = parser
                        .next_token::<State>()?
                        .unwrap_string_or_null("ifInState")?;
                }
                _ => {
                    parser.skip_token(parser.depth_array, parser.depth_dict)?;
                }
            }

            !parser.is_dict_end()?
        } {}

        Ok(request)
    }
}

impl JsonObjectParser for CopyBlobRequest {
    fn parse(parser: &mut Parser) -> crate::parser::Result<Self>
    where
        Self: Sized,
    {
        let mut request = CopyBlobRequest {
            account_id: Id::default(),
            from_account_id: Id::default(),
            blob_ids: Vec::new(),
        };

        parser
            .next_token::<String>()?
            .assert_jmap(Token::DictStart)?;

        while {
            let property = parser.next_dict_key::<RequestProperty>()?;
            match &property.hash[0] {
                0x6449_746e_756f_6363_61 => {
                    request.account_id = parser.next_token::<Id>()?.unwrap_string("accountId")?;
                }
                0x6449_746e_756f_6363_416d_6f72_66 => {
                    request.from_account_id =
                        parser.next_token::<Id>()?.unwrap_string("fromAccountId")?;
                }
                0x7364_4962_6f6c_62 => {
                    request.blob_ids = parser
                        .next_token::<Vec<BlobId>>()?
                        .unwrap_string("blobIds")?;
                }
                _ => {
                    parser.skip_token(parser.depth_array, parser.depth_dict)?;
                }
            }

            !parser.is_dict_end()?
        } {}

        Ok(request)
    }
}