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
use crate::helpers::ToJsonString;
use reqwest::Error as ReqwestError;
use serde::{Deserialize, Serialize};
use serde_json::error::Error as JsonError;
use serde_json::Value;
use std::collections::HashMap;

#[macro_export]
macro_rules! impl_to_json_string {
    ($type_name:ty) => {
        impl ToJsonString for $type_name {
            fn to_json_string(&self) -> Result<String, JsonError> {
                serde_json::to_string(self)
            }
        }
    };
}

// region Envelopes

pub type Response<T> = Result<GitHubApiResult<T>, GitHubApiError>;

#[derive(Debug)]
pub struct GitHubApiResult<T> {
    pub result: T,
    pub raw_result: String,
    pub limits: Option<LimitRemainingReset>,
    pub owner: Option<String>,
    pub repository: Option<String>,
    pub next_page: Option<u64>,
}

#[derive(Debug)]
pub enum GitHubApiError {
    NotImplemented,
    JsonError((JsonError, String)),
    GitHubError((String, String)),
    ReqwestError(ReqwestError),
}

// endregion

// region Enums

#[derive(Debug, Serialize, Deserialize)]
pub enum OpenClosed {
    #[serde(rename(deserialize = "open"))]
    Open,
    #[serde(rename(deserialize = "closed"))]
    Closed,
}

#[derive(Debug)]
pub enum Pagination {
    First(u64),
    Previous(u64),
    Next(u64),
    Last(u64),
    Undefined(String, u64),
}

// endregion

// region RateLimitResponse

#[derive(Debug, Serialize, Deserialize)]
pub struct RateLimitResponse {
    pub resources: RateLimitResources,

    // NOTE: The rate is deprecated by GitHub, and may disappear at any time.
    //       Use `resources.core` instead.
    pub rate: Option<LimitRemainingReset>,
}
impl_to_json_string!(RateLimitResponse);

#[derive(Debug, Serialize, Deserialize)]
pub struct RateLimitResources {
    pub core: LimitRemainingReset,
    pub search: LimitRemainingReset,
    pub graphql: LimitRemainingReset,
    pub integration_manifest: LimitRemainingReset,
}
impl_to_json_string!(RateLimitResources);

#[derive(Debug, Serialize, Deserialize)]
pub struct LimitRemainingReset {
    pub limit: u64,
    pub remaining: u64,
    pub reset: u64,
}
impl_to_json_string!(LimitRemainingReset);

// endregion

// region TagsResponse

#[derive(Debug, Serialize, Deserialize)]
pub struct TagsResponse {
    pub name: String,
    pub zipball_url: String,
    pub tarball_url: String,
    pub commit: TagsCommit,
    pub node_id: String,

    #[serde(flatten)]
    pub uncaptured: HashMap<String, Value>,
}
impl_to_json_string!(TagsResponse);

#[derive(Debug, Serialize, Deserialize)]
pub struct TagsCommit {
    pub sha: String,
    pub url: String,

    #[serde(flatten)]
    pub uncaptured: HashMap<String, Value>,
}
impl_to_json_string!(TagsCommit);

// endregion

// region ReleasesResponse

#[derive(Debug, Serialize, Deserialize)]
pub struct ReleasesResponse {
    pub url: String,
    pub assets_url: String,
    pub upload_url: String,
    pub html_url: String,
    pub id: u64,
    pub node_id: String,
    pub tag_name: String,
    pub target_commitish: String,
    pub name: String,
    pub draft: bool,
    pub author: GenericPerson,
    pub prerelease: bool,
    pub created_at: String,
    pub published_at: String,
    pub assets: Vec<ReleasesAsset>,
    pub tarball_url: String,
    pub zipball_url: String,
    pub body: String,

    #[serde(flatten)]
    pub uncaptured: HashMap<String, Value>,
}
impl_to_json_string!(ReleasesResponse);

#[derive(Debug, Serialize, Deserialize)]
pub struct ReleasesAsset {
    pub url: String,
    pub id: u64,
    pub node_id: String,
    pub name: String,
    pub label: Option<String>,
    pub uploader: GenericPerson,
    pub content_type: String,
    pub state: String,
    pub size: u64,
    pub download_count: u64,
    pub created_at: String,
    pub updated_at: String,
    pub browser_download_url: String,

    #[serde(flatten)]
    pub uncaptured: HashMap<String, Value>,
}
impl_to_json_string!(ReleasesAsset);

#[derive(Debug, Serialize, Deserialize)]
pub struct GenericPerson {
    pub login: String,
    pub id: u64,
    pub node_id: String,
    pub avatar_url: String,
    pub gravatar_id: String,
    pub url: String,
    pub html_url: String,
    pub followers_url: String,
    pub following_url: String,
    pub gists_url: String,
    pub starred_url: String,
    pub subscriptions_url: String,
    pub organizations_url: String,
    pub repos_url: String,
    pub events_url: String,
    pub received_events_url: String,
    pub r#type: String,
    pub site_admin: bool,

    #[serde(flatten)]
    pub uncaptured: HashMap<String, Value>,
}
impl_to_json_string!(GenericPerson);

// endregion

// region LicenseResponse

#[derive(Debug, Serialize, Deserialize)]
pub struct LicenseResponse {
    pub name: String,
    pub path: String,
    pub sha: String,
    pub size: u64,
    pub url: String,
    pub html_url: String,
    pub git_url: String,
    pub download_url: String,
    pub r#type: String,
    pub content: String,
    pub encoding: String,
    #[serde(rename(deserialize = "_links"))]
    pub links: LicenseLinks,
    pub license: LicenseLicense,

    #[serde(flatten)]
    pub uncaptured: HashMap<String, Value>,
}
impl_to_json_string!(LicenseResponse);

#[derive(Debug, Serialize, Deserialize)]
pub struct LicenseLinks {
    #[serde(rename(deserialize = "self"))]
    pub self_link: String,
    pub git: String,
    pub html: String,

    #[serde(flatten)]
    pub uncaptured: HashMap<String, Value>,
}
impl_to_json_string!(LicenseLinks);

#[derive(Debug, Serialize, Deserialize)]
pub struct LicenseLicense {
    pub key: String,
    pub name: String,
    pub spdx_id: String,
    pub url: String,
    pub node_id: String,

    #[serde(flatten)]
    pub uncaptured: HashMap<String, Value>,
}
impl_to_json_string!(LicenseLicense);

// endregion