github_stats/
repository.rs

1//! For getting repository information.
2
3use chrono::prelude::{DateTime, Utc};
4use serde::Deserialize;
5
6use crate::{Result, User};
7
8/// Represents that stats of a [Github] repository.
9///
10/// [Github]: https://github.com/
11#[derive(Debug, Deserialize)]
12pub struct Repo {
13    id: u64,
14    node_id: String,
15    name: String,
16    full_name: String,
17    private: bool,
18    owner: User,
19    html_url: String,
20    description: String,
21    fork: bool,
22    url: String,
23    created_at: DateTime<Utc>,
24    updated_at: DateTime<Utc>,
25    pushed_at: DateTime<Utc>,
26    git_url: String,
27    ssh_url: String,
28    clone_url: String,
29    svn_url: String,
30    homepage: String,
31    /// In *kilo*bytes.
32    size: u64,
33    stargazers_count: u64,
34    language: Option<String>,
35    forks_count: u64,
36    archived: bool,
37    disabled: bool,
38    has_projects: bool,
39    has_pages: bool,
40    has_downloads: bool,
41    /// Issues + PRs
42    open_issues: u64,
43    default_branch: String,
44    /// Number of watchers.
45    subscribers_count: u64,
46    has_issues: bool,
47    has_wiki: bool,
48    open_issues_count: u64,
49}
50
51impl Repo {
52    /// Creates a new `Repo`.
53    ///
54    /// # Example
55    ///
56    /// ```no_run
57    /// use github_stats::Repo;
58    ///
59    /// let repo = Repo::new("rust-lang", "rust", "<my user agent>");
60    /// ```
61    pub async fn new(user: &str, repo: &str, user_agent: &str) -> Result<Self> {
62        let repo: Repo = reqwest::Client::builder()
63            .user_agent(user_agent)
64            .build()?
65            .get(&repo_api_url(user, repo))
66            .send()
67            .await?
68            .json()
69            .await?;
70
71        Ok(repo)
72    }
73
74    pub fn id(&self) -> u64 {
75        self.id
76    }
77
78    pub fn node_id(&self) -> &str {
79        &self.node_id
80    }
81
82    pub fn name(&self) -> &str {
83        &self.name
84    }
85
86    pub fn full_name(&self) -> &str {
87        &self.full_name
88    }
89
90    pub fn private(&self) -> bool {
91        self.private
92    }
93
94    pub fn owner(&self) -> &User {
95        &self.owner
96    }
97
98    pub fn html_url(&self) -> &str {
99        &self.html_url
100    }
101
102    pub fn description(&self) -> &str {
103        &self.description
104    }
105
106    pub fn fork(&self) -> bool {
107        self.fork
108    }
109
110    pub fn url(&self) -> &str {
111        &self.url
112    }
113
114    pub fn created_at(&self) -> &DateTime<Utc> {
115        &self.created_at
116    }
117
118    pub fn updated_at(&self) -> &DateTime<Utc> {
119        &self.updated_at
120    }
121
122    pub fn pushed_at(&self) -> &DateTime<Utc> {
123        &self.pushed_at
124    }
125
126    pub fn git_url(&self) -> &str {
127        &self.git_url
128    }
129
130    pub fn ssh_url(&self) -> &str {
131        &self.ssh_url
132    }
133
134    pub fn clone_url(&self) -> &str {
135        &self.clone_url
136    }
137
138    pub fn svn_url(&self) -> &str {
139        &self.svn_url
140    }
141
142    pub fn homepage(&self) -> &str {
143        &self.homepage
144    }
145
146    /// In *kilo*bytes.
147    pub fn size(&self) -> u64 {
148        self.size
149    }
150
151    pub fn stargazers_count(&self) -> u64 {
152        self.stargazers_count
153    }
154
155    pub fn language(&self) -> &Option<String> {
156        &self.language
157    }
158
159    pub fn forks_count(&self) -> u64 {
160        self.forks_count
161    }
162
163    pub fn archived(&self) -> bool {
164        self.archived
165    }
166
167    pub fn disabled(&self) -> bool {
168        self.disabled
169    }
170
171    pub fn has_projects(&self) -> bool {
172        self.has_projects
173    }
174
175    pub fn has_pages(&self) -> bool {
176        self.has_pages
177    }
178
179    pub fn has_downloads(&self) -> bool {
180        self.has_downloads
181    }
182
183    /// Issues + PRs
184    pub fn open_issues(&self) -> u64 {
185        self.open_issues
186    }
187
188    pub fn default_branch(&self) -> &str {
189        &self.default_branch
190    }
191
192    /// Number of watchers.
193    pub fn subscribers_count(&self) -> u64 {
194        self.subscribers_count
195    }
196
197    pub fn has_issues(&self) -> bool {
198        self.has_issues
199    }
200
201    pub fn has_wiki(&self) -> bool {
202        self.has_wiki
203    }
204
205    pub fn open_issues_count(&self) -> u64 {
206        self.open_issues_count
207    }
208}
209
210// Takes [Github] user and repo IDs to make a link to the API for that repo.
211//
212// [Github]: https://github.com/
213fn repo_api_url(user: &str, repo: &str) -> String {
214    const URL: &str = "https://api.github.com/repos";
215    format!("{}/{}/{}", URL, user, repo)
216}