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