summaryrefslogtreecommitdiffstats
path: root/github_v3/src/model.rs
blob: 5a81c5fd1771dcd849903a91905580dfaa2c978a (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
#[derive(Debug, Copy, Eq, PartialEq, Clone)]
pub enum UserType {
    Org,
    User,
    Bot,
}

use serde::de;
use serde::de::{Deserializer, Visitor};
use serde::Serializer;
use serde_derive::Deserialize;
use serde_derive::Serialize;
use std::fmt;

/// Case-insensitive enum
impl<'de> serde::Deserialize<'de> for UserType {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: Deserializer<'de>,
    {
        struct UserTypeVisitor;

        impl<'a> Visitor<'a> for UserTypeVisitor {
            type Value = UserType;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("user/org/bot")
            }

            fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
                match v.to_ascii_lowercase().as_str() {
                    "org" | "organization" => Ok(UserType::Org),
                    "user" => Ok(UserType::User),
                    "bot" => Ok(UserType::Bot),
                    x => Err(de::Error::unknown_variant(x, &["user", "org", "bot"])),
                }
            }

            fn visit_string<E: de::Error>(self, v: String) -> Result<Self::Value, E> {
                self.visit_str(&v)
            }
        }

        deserializer.deserialize_string(UserTypeVisitor)
    }
}

impl serde::Serialize for UserType {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer,
    {
        serializer.serialize_str(match *self {
            UserType::User => "user",
            UserType::Org => "org",
            UserType::Bot => "bot",
        })
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    pub id: u32,
    pub login: String,
    pub name: Option<String>,
    pub avatar_url: Option<String>,  // "https://avatars0.githubusercontent.com/u/1111?v=4",
    pub gravatar_id: Option<String>, // "",
    pub html_url: String,            // "https://github.com/zzzz",
    pub blog: Option<String>,        // "https://example.com
    #[serde(rename = "type")]
    pub user_type: UserType,
    pub created_at: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContribWeek {
    #[serde(rename = "w")]
    pub week_timestamp: u32,
    #[serde(rename = "a")]
    pub added: u32,
    #[serde(rename = "d")]
    pub deleted: u32,
    #[serde(rename = "c")]
    pub commits: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResults<T> {
    pub items: Vec<T>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserContrib {
    pub total: u32,
    pub weeks: Vec<ContribWeek>,
    pub author: Option<User>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitCommitAuthor {
    pub date: String, // "2018-04-30T16:24:52Z",
    pub email: String,
    pub name: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitCommit {
    pub author: GitCommitAuthor,
    pub committer: GitCommitAuthor,
    pub message: String,
    pub comment_count: u32,
    // tree.sha
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommitMeta {
    pub sha: String, // TODO: deserialize to bin
    pub author: Option<User>,
    pub committer: Option<User>,
    pub commit: GitCommit,
    // parents: [{sha}]
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubRepo {
    pub name: String,
    pub description: Option<String>,
    pub fork: bool,
    pub created_at: String,
    pub updated_at: Option<String>,
    pub pushed_at: Option<String>,
    pub homepage: Option<String>,
    pub stargazers_count: u32,  // Stars
    pub forks_count: u32,       // Real number of forks
    pub subscribers_count: u32, // Real number of watches
    pub has_issues: bool,
    pub open_issues_count: Option<u32>,
    // language: JavaScript,
    pub has_downloads: bool,
    // has_wiki: true,
    pub has_pages: bool,
    pub archived: bool,
    pub default_branch: Option<String>,
    pub owner: Option<User>,
    #[serde(default)]
    pub topics: Vec<String>,

    #[serde(default)]
    pub is_template: Vec<String>,

    /// My custom addition!
    pub github_page_url: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubRelease {
    // url: Option<String>, // "https://api.github.com/repos/octocat/Hello-World/releases/1",
    // html_url: Option<String>, // "https://github.com/octocat/Hello-World/releases/v1.0.0",
    // assets_url: Option<String>, // "https://api.github.com/repos/octocat/Hello-World/releases/1/assets",
    // upload_url: Option<String>, // "https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}",
    // tarball_url: Option<String>, // "https://api.github.com/repos/octocat/Hello-World/tarball/v1.0.0",
    // zipball_url: Option<String>, // "https://api.github.com/repos/octocat/Hello-World/zipball/v1.0.0",
    // id: Option<String>, // 1,
    // node_id: Option<String>, // "MDc6UmVsZWFzZTE=",
    pub tag_name: Option<String>, // "v1.0.0",
    // target_commitish: Option<String>, // "master",
    // name: Option<String>, // "v1.0.0",
    pub body: Option<String>,         // "Description of the release",
    pub draft: Option<bool>,          // false,
    pub prerelease: Option<bool>,     // false,
    pub created_at: Option<String>,   // "2013-02-27T19:35:32Z",
    pub published_at: Option<String>, // "2013-02-27T19:35:32Z",
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Topics {
    pub names: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserOrg {
    pub login: String, // "github",
    //id: String, // 1,
    // node_id: String, // "MDEyOk9yZ2FuaXphdGlvbjE=",
    pub url: String, // "https://api.github.com/orgs/github",
    // public_members_url: String, // "https://api.github.com/orgs/github/public_members{/member}",
    // avatar_url: String, // "https://github.com/images/error/octocat_happy.gif",
    pub description: Option<String>, // "A great organization"
}