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    /// Creation timestamp supplied by GitHub, in ISO 8601 format.
16    pub created_at: String,
17    /// Last metadata update timestamp, in ISO 8601 format.
18    pub updated_at: String,
19    /// Last push timestamp in ISO 8601 format, when available.
20    pub pushed_at: Option<String>,
21    pub is_private: bool,
22    pub is_fork: bool,
23    pub is_archived: bool,
24    pub stargazer_count: u32,
25    pub fork_count: u32,
26    /// Users subscribed to notifications, not users who starred the repository.
27    pub watcher_count: Option<u32>,
28    /// Open issues, excluding pull requests. Unavailable from the REST repository response.
29    pub open_issue_count: Option<u32>,
30    /// Pull requests in all states. Unavailable from the REST repository response.
31    pub pull_request_count: Option<u32>,
32    /// Unavailable from the REST repository response.
33    pub release_count: Option<u32>,
34    pub primary_language: Option<Language>,
35    /// None if the optional language breakdown could not be retrieved.
36    pub languages: Option<Vec<LanguageUsage>>,
37    /// False when languages are unavailable or the GraphQL connection has more pages.
38    pub languages_complete: bool,
39    pub license_info: Option<License>,
40    pub default_branch: Option<String>,
41    pub topics: Vec<String>,
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct LanguageUsage {
46    pub language: Language,
47    pub bytes: u64,
48}
49
50/// Repository summary returned by search.
51#[derive(Debug, Clone, Default, Serialize, Deserialize)]
52pub struct SearchRepository {
53    pub node_id: String,
54    pub database_id: Option<u64>,
55    pub name: String,
56    pub name_with_owner: String,
57    pub description: Option<String>,
58    pub url: String,
59    pub stargazer_count: u32,
60    pub fork_count: u32,
61    /// Creation timestamp supplied by GitHub, in ISO 8601 format.
62    pub created_at: String,
63    /// Last metadata update timestamp, in ISO 8601 format.
64    pub updated_at: String,
65    /// Last push timestamp in ISO 8601 format, when available.
66    pub pushed_at: Option<String>,
67    pub primary_language: Option<Language>,
68    pub license_info: Option<License>,
69    pub topics: Vec<String>,
70}
71
72macro_rules! repository_helpers {
73    ($model:ty) => {
74        impl $model {
75            pub fn language(&self) -> Option<&str> {
76                self.primary_language
77                    .as_ref()
78                    .map(|language| language.name.as_str())
79            }
80            pub fn license(&self) -> Option<&str> {
81                self.license_info
82                    .as_ref()
83                    .map(|license| license.name.as_str())
84            }
85            pub fn license_spdx(&self) -> Option<&str> {
86                self.license_info
87                    .as_ref()
88                    .and_then(|license| license.spdx_id.as_deref())
89            }
90            pub fn topics(&self) -> Vec<&str> {
91                self.topics.iter().map(String::as_str).collect()
92            }
93            pub fn owner(&self) -> &str {
94                self.name_with_owner
95                    .split_once('/')
96                    .map_or(self.name_with_owner.as_str(), |(owner, _)| owner)
97            }
98        }
99    };
100}
101repository_helpers!(Repository);
102repository_helpers!(SearchRepository);
103
104impl Repository {
105    pub fn default_branch(&self) -> Option<&str> {
106        self.default_branch.as_deref()
107    }
108    pub fn open_issues(&self) -> Option<u32> {
109        self.open_issue_count
110    }
111    pub fn watcher_count(&self) -> Option<u32> {
112        self.watcher_count
113    }
114}