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
use serde;

use std::fmt;
use types::*;
use command;
use uuid::Uuid;

use serde::{Serialize, Deserialize, Serializer, Deserializer};
use serde::ser::SerializeSeq;

#[derive(Serialize, Deserialize, Debug)]
pub enum UploadState {
    #[serde(rename = "pending")]
    Pending,

    #[serde(rename = "completed")]
    Completed,
}


#[derive(Debug)]
pub struct Thumbnail {
    link : String,
    width : usize,
    height : usize,
}


#[derive(Serialize, Deserialize, Default, Debug)]
#[serde(default)]

/// A Todoist note
pub struct Note {
    /// The note's unique ID
    pub id : ID,

    /// The ID of the note's poster
    pub user_id : ID,

    /// The ID of the item the note is attached to
    pub item_id : ID,

    /// The ID of the project this note is a part of
    pub project_id : ID,

    /// The note's text
    pub content : String,

    /// the file attached to this note
    pub file_attachment : Attachment,

    /// List of user ids to notify
    pub uids_to_notify : Vec<ID>,

    /// whether this note is marked as deleted
    pub is_deleted : IntBool,

    /// whether this note has been marked as archived
    pub is_archived : IntBool,

    /// the date that this note was posted
    pub posting : Date,
}

#[derive(Serialize, Deserialize, Default, Debug)]
#[serde(default)]

/// A file attachment
pub struct Attachment {
    /// The attachment's name
    pub file_name : String,

    /// The file's size
    pub file_size : usize,

    /// the files MIME type
    pub file_type : String,

    /// the url where this file can be found
    pub file_url : String,

    /// the file's upload state (pending or complete)
    pub upload_state : UploadState,

    /// small thumbnail
    pub tn_s : Option<Thumbnail>,

    /// medium thumbnail
    pub tn_m : Option<Thumbnail>,

    /// large thumbnail
    pub tn_l : Option<Thumbnail>,
}


pub struct ProjectNote(Note);

impl Note {
    fn project(self) -> ProjectNote {
        ProjectNote(self)
    }
}

impl<'de> Deserialize<'de> for Thumbnail {
    fn deserialize<D : Deserializer<'de>>(deserializer: D) -> Result<Thumbnail, D::Error> {
        struct ThumbnailVisitor;

        impl<'de> serde::de::Visitor<'de> for ThumbnailVisitor {
            type Value = Thumbnail;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("an array in the form [string, usize, usize]")
            }

            fn visit_seq<A : serde::de::SeqAccess<'de> >(self, mut value: A) -> Result<Self::Value, A::Error>    {
                let mut x = Thumbnail{
                    link: "".to_string(),
                    width: 0,
                    height: 0,
                };

                x.link = value.next_element()?.unwrap_or("".to_string());
                x.width = value.next_element()?.unwrap_or(0);
                x.height = value.next_element()?.unwrap_or(0);
                Ok(x)
            }
        }

        deserializer.deserialize_seq(ThumbnailVisitor{})
    }
}

impl Default for UploadState {
    fn default() -> UploadState {
        UploadState::Completed
    }
}

impl Serialize for Thumbnail {
    fn serialize<S : Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut seq = serializer.serialize_seq(Some(3))?;
        seq.serialize_element(&self.link)?;
        seq.serialize_element(&self.width)?;
        seq.serialize_element(&self.height)?;
        seq.end()
    }
}

impl command::Create for ProjectNote {
    fn create(self) -> command::Command {
        command::Command {
            typ: "note_add".to_string(),
            args: Box::new(
                command::note::Create {
                    project_id      : Some(self.0.project_id),
                    item_id         : None,
                    content         : self.0.content,
                    file_attachment : Some(self.0.file_attachment),
                    uids_to_notify  : None,
                }
            ),
            uuid:    Uuid::new_v4(),
            temp_id: None,
        }
    }
}

impl command::Create for Note {
    fn create(self) -> command::Command {
        command::Command {
            typ: "note_add".to_string(),
            args: Box::new(
                command::note::Create {
                    item_id         : Some(self.project_id),
                    project_id      : None,
                    content         : self.content,
                    file_attachment : Some(self.file_attachment),
                    uids_to_notify  : Some(self.uids_to_notify),
                }
            ),
            uuid:    Uuid::new_v4(),
            temp_id: None,
        }
    }
}

impl command::Update for Note {
    fn update(self) -> command::Command {
        command::Command {
            typ: "note_update".to_string(),
            args: Box::new(
                command::note::Update {
                    content         : self.content,
                    id              : self.id,
                    file_attachment : Some(self.file_attachment),
                }
            ),
            uuid:    Uuid::new_v4(),
            temp_id: None,
        }
    }
}

impl command::Delete for Note {
    fn delete(self) -> command::Command {
        command::Command {
            typ: "note_delete".to_string(),
            args: Box::new(
                command::Identity {
                    ids: vec![self.id],
                }
            ),
            uuid:    Uuid::new_v4(),
            temp_id: None,
        }
    }
}