use crate::github::types::{Language, License};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Repository {
pub node_id: String,
pub database_id: Option<u64>,
pub name: String,
pub name_with_owner: String,
pub description: Option<String>,
pub url: String,
pub homepage_url: Option<String>,
pub created_at: String,
pub updated_at: String,
pub pushed_at: Option<String>,
pub is_private: bool,
pub is_fork: bool,
pub is_archived: bool,
pub stargazer_count: u32,
pub fork_count: u32,
pub watcher_count: Option<u32>,
pub open_issue_count: Option<u32>,
pub pull_request_count: Option<u32>,
pub release_count: Option<u32>,
pub primary_language: Option<Language>,
pub languages: Option<Vec<LanguageUsage>>,
pub languages_complete: bool,
pub license_info: Option<License>,
pub default_branch: Option<String>,
pub topics: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LanguageUsage {
pub language: Language,
pub bytes: u64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SearchRepository {
pub node_id: String,
pub database_id: Option<u64>,
pub name: String,
pub name_with_owner: String,
pub description: Option<String>,
pub url: String,
pub stargazer_count: u32,
pub fork_count: u32,
pub created_at: String,
pub updated_at: String,
pub pushed_at: Option<String>,
pub primary_language: Option<Language>,
pub license_info: Option<License>,
pub topics: Vec<String>,
}
macro_rules! repository_helpers {
($model:ty) => {
impl $model {
pub fn language(&self) -> Option<&str> {
self.primary_language
.as_ref()
.map(|language| language.name.as_str())
}
pub fn license(&self) -> Option<&str> {
self.license_info
.as_ref()
.map(|license| license.name.as_str())
}
pub fn license_spdx(&self) -> Option<&str> {
self.license_info
.as_ref()
.and_then(|license| license.spdx_id.as_deref())
}
pub fn topics(&self) -> Vec<&str> {
self.topics.iter().map(String::as_str).collect()
}
pub fn owner(&self) -> &str {
self.name_with_owner
.split_once('/')
.map_or(self.name_with_owner.as_str(), |(owner, _)| owner)
}
}
};
}
repository_helpers!(Repository);
repository_helpers!(SearchRepository);
impl Repository {
pub fn default_branch(&self) -> Option<&str> {
self.default_branch.as_deref()
}
pub fn open_issues(&self) -> Option<u32> {
self.open_issue_count
}
pub fn watcher_count(&self) -> Option<u32> {
self.watcher_count
}
}