cod_endpoints/
endpoint_generator.rs1use std::fmt::Display;
2use std::str::FromStr;
3
4use cod_git_info::repo_owner::{get_repo_owner, RepoAndOwner};
5use reqwest::Url;
6
7use crate::api::CODEBERG_API_BASE;
8
9#[derive(Debug, Clone, Copy)]
10pub struct EndpointGenerator;
11
12macro_rules! generator_method {
13 ($method_name:ident, $endpoint:ident) => {
14 impl EndpointGenerator {
15 pub fn $method_name() -> anyhow::Result<Url> {
16 use crate::api::$endpoint;
17 Url::from_str(CODEBERG_API_BASE)?
18 .join($endpoint)
19 .map_err(anyhow::Error::from)
20 }
21 }
22 };
23}
24
25macro_rules! generator_owner_repo_method {
26 ($method_name:ident, $endpoint:ident) => {
27 impl EndpointGenerator {
28 pub fn $method_name() -> anyhow::Result<Url> {
29 use crate::api::$endpoint;
30 Self::repos_owner_repo($endpoint)
31 }
32 }
33 };
34}
35
36generator_method!(verify, AUTHENTIFICATION_VERIFICATION);
37generator_method!(user_info, USER_INFO);
38generator_method!(user_followers, USER_FOLLOWERS);
39generator_method!(user_following, USER_FOLLOWING);
40generator_method!(user_repos, USER_REPOS);
41generator_method!(user_search, USER_SEARCH);
42generator_method!(repo_search, REPO_SEARCH);
43generator_method!(all_notifications, NOTIFICATIONS);
44
45generator_owner_repo_method!(repo_infos, REPO_INFOS);
46generator_owner_repo_method!(repo_assignees, REPO_ASSIGNEES);
47generator_owner_repo_method!(repo_issues, REPO_ISSUES);
48generator_owner_repo_method!(repo_pull_requests, REPO_PULLS);
49generator_owner_repo_method!(repo_labels, REPO_LABELS);
50generator_owner_repo_method!(repo_milestones, REPO_MILESTONES);
51generator_owner_repo_method!(repo_branches, REPO_BRANCHES);
52
53impl EndpointGenerator {
54 fn repos_owner_repo(endpoint: impl Display) -> anyhow::Result<Url> {
55 use crate::api::REPO_OWNER_REPOS;
56 let RepoAndOwner { repo, owner } = get_repo_owner()?;
57 Url::from_str(CODEBERG_API_BASE)?
58 .join(format!("{REPO_OWNER_REPOS}/{owner}/{repo}/{endpoint}").as_str())
59 .map_err(anyhow::Error::from)
60 }
61
62 pub fn repo_update_issue(issue_id: usize) -> anyhow::Result<Url> {
63 use crate::api::REPO_ISSUES;
64 Self::repos_owner_repo(format!("{REPO_ISSUES}/{issue_id}"))
65 }
66
67 pub fn repo_put_issue_labels(issue_id: usize) -> anyhow::Result<Url> {
68 use crate::api::REPO_ISSUES;
69 use crate::api::REPO_LABELS;
70 Self::repos_owner_repo(format!("{REPO_ISSUES}/{issue_id}/{REPO_LABELS}"))
71 }
72
73 pub fn repo_update_milestone(milestone_id: usize) -> anyhow::Result<Url> {
74 use crate::api::REPO_MILESTONES;
75 Self::repos_owner_repo(format!("{REPO_MILESTONES}/{milestone_id}"))
76 }
77
78 pub fn repo_forks(ownername: &str, reponame: &str) -> anyhow::Result<Url> {
79 use crate::api::REPO_FORK;
80 Self::repos_owner_repo(format!("{ownername}/{reponame}/{REPO_FORK}"))
81 }
82
83 pub fn repo_comments_for_id(issue_or_pr_id: usize) -> anyhow::Result<Url> {
84 use crate::api::REPO_ISSUES;
85 use crate::api::REPO_ISSUES_COMMENTS;
86 Self::repos_owner_repo(format!(
87 "{REPO_ISSUES}/{issue_or_pr_id}/{REPO_ISSUES_COMMENTS}"
88 ))
89 }
90
91 pub fn repo_labels_with_id(label_id: usize) -> anyhow::Result<Url> {
92 use crate::api::REPO_LABELS;
93 Self::repos_owner_repo(format!("{REPO_LABELS}/{label_id}"))
94 }
95
96 pub fn repo_update_pull_request(pull_request_id: usize) -> anyhow::Result<Url> {
97 use crate::api::REPO_PULLS;
98 Self::repos_owner_repo(format!("{REPO_PULLS}/{pull_request_id}"))
99 }
100
101 pub fn get_user_repos(username: String) -> anyhow::Result<Url> {
102 use crate::api::USERS_BASE;
103 Url::from_str(CODEBERG_API_BASE)?
104 .join(format!("{USERS_BASE}/{username}/repos").as_str())
105 .map_err(anyhow::Error::from)
106 }
107
108 pub fn get_org_repos(orgname: String) -> anyhow::Result<Url> {
109 use crate::api::ORG_BASE;
110 Url::from_str(CODEBERG_API_BASE)?
111 .join(format!("{ORG_BASE}/{orgname}/repos").as_str())
112 .map_err(anyhow::Error::from)
113 }
114
115 pub fn get_notification_thread(thread_id: usize) -> anyhow::Result<Url> {
116 use crate::api::NOTIFICATIONS_INFO;
117 Url::from_str(CODEBERG_API_BASE)?
118 .join(format!("{NOTIFICATIONS_INFO}/{thread_id}").as_str())
119 .map_err(anyhow::Error::from)
120 }
121}