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
#![allow(unused)]

pub mod response;
pub use response::Response;

/// https://developers.asana.com/docs/schemas
use serde::{Deserialize, de::DeserializeOwned};
use serde_json::Value;

#[derive(Deserialize, Debug)]
pub struct AsanaNamedResource {
    pub gid: String,
    pub resource_type: String,
    pub name: String,
}

#[derive(Deserialize, Debug)]
pub struct AsanaResource {
    pub gid: String,
    pub resource_type: String,
}

#[derive(Deserialize, Debug, Clone)]
pub struct Error {
    pub help: Option<String>,
    pub message: Option<String>,
    pub phrase: Option<String>,
}

#[derive(Deserialize, Debug)]
pub struct Errors {
    pub errors: Vec<Error>,
}

#[derive(Deserialize, Debug)]
pub struct Workspace {
    pub gid: String,
    pub resource_type: String,
    pub name: String,
    pub email_domains: Option<Vec<String>>,
    pub is_organization: Option<bool>,
}

#[derive(Deserialize, Debug)]
pub struct Photo {
    pub image_128x128: String,
    pub image_21x21: String,
    pub image_27x27: String,
    pub image_36x36: String,
    pub image_60x60: String,
}

#[derive(Deserialize, Debug)]
pub struct User {
    pub gid: String,
    pub resource_type: String,
    pub name: String,
    pub email: String,
    pub photo: Option<Photo>,
    pub workspaces: Vec<Workspace>,
}

#[derive(Deserialize, Debug)]
pub struct UserCompact {
    pub gid: String,
    pub resource_type: String,
    pub name: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_asana_named_resource() {
        let raw = r#"{
            "gid": "12345",
            "resource_type": "task",
            "name": "Bug Task"
        }"#;

        let resource: AsanaNamedResource = serde_json::from_str(raw).unwrap();
        assert_eq!(resource.name, "Bug Task");
        assert_eq!(resource.resource_type, "task");
    }

    #[test]
    fn test_asana_resource() {
        let raw = r#"{
            "gid": "12345",
            "resource_type": "task"
        }"#;

        let resource: AsanaResource = serde_json::from_str(raw).unwrap();
        assert_eq!(resource.resource_type, "task");
        assert_eq!(resource.gid, "12345");
    }

    #[test]
    fn test_workspace() {
        let raw = r#"{
            "gid": "12345",
            "resource_type": "workspace",
            "name": "My Company Workspace",
            "email_domains": [
              "asana.com"
            ],
            "is_organization": false
          }"#;

        let ws: Workspace = serde_json::from_str(raw).unwrap();
        assert_eq!(ws.resource_type, "workspace");
        assert_eq!(ws.gid, "12345");
        assert_eq!(ws.email_domains.unwrap().len(), 1);
        assert!(!ws.is_organization.unwrap());
    }

    #[test]
    fn test_user() {
        let raw = r#"{
            "gid": "12345",
            "resource_type": "user",
            "name": "Greg Sanchez",
            "email": "gsanchez@example.com",
            "photo": {
              "image_128x128": "https://...",
              "image_21x21": "https://...",
              "image_27x27": "https://...",
              "image_36x36": "https://...",
              "image_60x60": "https://..."
            },
            "workspaces": [
              {
                "gid": "12345",
                "resource_type": "workspace",
                "name": "My Company Workspace"
              }
            ]
          }"#;

        let user: User = serde_json::from_str(raw).unwrap();
        assert_eq!(user.name, "Greg Sanchez");
        assert_eq!(user.gid, "12345");
        assert_eq!(user.photo.unwrap().image_128x128, "https://...");
        assert_eq!(user.workspaces[0].resource_type, "workspace");
    }

    #[test]
    fn test_error() {
        let raw = r#"{
            "errors": [
              {
                "help": "For more information on API status codes and how to handle them, read the docs on errors: https://asana.github.io/developer-docs/#errors'",
                "message": "project: Missing input",
                "phrase": "6 sad squid snuggle softly"
              }
            ]
          }"#;

        let errors: Errors = serde_json::from_str(raw).unwrap();
        assert_eq!(errors.errors.len(), 1);
        assert!(errors.errors[0].help.is_some());
        assert!(errors.errors[0].message.is_some());
        assert!(errors.errors[0].phrase.is_some());
    }
}