Skip to main content

github_rust/github/
models.rs

1//! Public repository models, independent of REST and GraphQL response envelopes.
2use crate::github::types::{Language, License};
3use serde::{Deserialize, Serialize};
4
5/// Repository metadata. Optional counts are unknown when an API cannot supply them.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Repository {
8    pub node_id: String,
9    pub database_id: Option<u64>,
10    pub name: String,
11    pub name_with_owner: String,
12    pub description: Option<String>,
13    pub url: String,
14    pub homepage_url: Option<String>,
15    pub created_at: String,
16    pub updated_at: String,
17    pub pushed_at: Option<String>,
18    pub is_private: bool,
19    pub is_fork: bool,
20    pub is_archived: bool,
21    pub stargazer_count: u32,
22    pub fork_count: u32,
23    /// Users subscribed to notifications, not users who starred the repository.
24    pub watcher_count: Option<u32>,
25    /// Open issues, excluding pull requests. Unavailable from the REST repository response.
26    pub open_issue_count: Option<u32>,
27    /// Pull requests in all states. Unavailable from the REST repository response.
28    pub pull_request_count: Option<u32>,
29    /// Unavailable from the REST repository response.
30    pub release_count: Option<u32>,
31    pub primary_language: Option<Language>,
32    /// None if the optional language breakdown could not be retrieved.
33    pub languages: Option<Vec<LanguageUsage>>,
34    /// False when languages are unavailable or the GraphQL connection has more pages.
35    pub languages_complete: bool,
36    pub license_info: Option<License>,
37    pub default_branch: Option<String>,
38    pub topics: Vec<String>,
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct LanguageUsage {
43    pub language: Language,
44    pub bytes: u64,
45}
46
47/// Repository summary returned by search.
48#[derive(Debug, Clone, Default, Serialize, Deserialize)]
49pub struct SearchRepository {
50    pub node_id: String,
51    pub database_id: Option<u64>,
52    pub name: String,
53    pub name_with_owner: String,
54    pub description: Option<String>,
55    pub url: String,
56    pub stargazer_count: u32,
57    pub fork_count: u32,
58    pub created_at: String,
59    pub updated_at: String,
60    pub pushed_at: Option<String>,
61    pub primary_language: Option<Language>,
62    pub license_info: Option<License>,
63    pub topics: Vec<String>,
64}
65
66macro_rules! repository_helpers {
67    ($model:ty) => {
68        impl $model {
69            pub fn language(&self) -> Option<&str> {
70                self.primary_language
71                    .as_ref()
72                    .map(|language| language.name.as_str())
73            }
74            pub fn license(&self) -> Option<&str> {
75                self.license_info
76                    .as_ref()
77                    .map(|license| license.name.as_str())
78            }
79            pub fn license_spdx(&self) -> Option<&str> {
80                self.license_info
81                    .as_ref()
82                    .and_then(|license| license.spdx_id.as_deref())
83            }
84            pub fn topics(&self) -> Vec<&str> {
85                self.topics.iter().map(String::as_str).collect()
86            }
87            pub fn owner(&self) -> &str {
88                self.name_with_owner
89                    .split('/')
90                    .next()
91                    .unwrap_or(&self.name_with_owner)
92            }
93        }
94    };
95}
96repository_helpers!(Repository);
97repository_helpers!(SearchRepository);
98
99impl Repository {
100    pub fn default_branch(&self) -> Option<&str> {
101        self.default_branch.as_deref()
102    }
103    pub fn open_issues(&self) -> Option<u32> {
104        self.open_issue_count
105    }
106    pub fn watcher_count(&self) -> Option<u32> {
107        self.watcher_count
108    }
109}