-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathrepo.rs
More file actions
284 lines (240 loc) · 8.86 KB
/
Copy pathrepo.rs
File metadata and controls
284 lines (240 loc) · 8.86 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use std::{fmt, str::FromStr};
use anyhow::{anyhow, ensure, Error};
use relative_path::RelativePath;
#[derive(Clone, Debug)]
pub struct Repository {
pub path: RepoPath,
pub description: String,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct RepoPath {
pub site: RepoSite,
pub qual: RepoQualifier,
pub name: RepoName,
}
impl RepoPath {
pub fn from_parts(site: &str, qual: &str, name: &str) -> Result<RepoPath, Error> {
Ok(RepoPath {
site: site.parse()?,
qual: qual.parse()?,
name: name.parse()?,
})
}
pub fn to_usercontent_file_url(&self, path: &RelativePath) -> String {
format!(
"{}/{}/{}/{}/{}",
self.site.to_usercontent_base_uri(),
self.qual.as_ref(),
self.name.as_ref(),
self.site.to_usercontent_repo_suffix(),
path.normalize()
)
}
}
impl fmt::Display for RepoPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} => {}/{}",
self.site,
self.qual.as_ref(),
self.name.as_ref()
)
}
}
#[allow(clippy::similar_names)]
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum RepoSite {
Github,
Gitlab(Option<GiteaDomain>),
Bitbucket,
Sourcehut,
Codeberg,
Gitea(GiteaDomain),
}
impl RepoSite {
pub fn to_base_uri(&self) -> &str {
match self {
RepoSite::Github => "https://github.com",
RepoSite::Gitlab(None) => "https://gitlab.com",
RepoSite::Gitlab(Some(domain)) => domain.as_ref(),
RepoSite::Bitbucket => "https://bitbucket.org",
RepoSite::Sourcehut => "https://git.sr.ht",
RepoSite::Codeberg => "https://codeberg.org",
RepoSite::Gitea(domain) => domain.as_ref(),
}
}
pub fn to_usercontent_base_uri(&self) -> &str {
match self {
RepoSite::Github => "https://raw.githubusercontent.com",
RepoSite::Gitlab(None) => "https://gitlab.com",
RepoSite::Gitlab(Some(domain)) => domain.as_ref(),
RepoSite::Bitbucket => "https://bitbucket.org",
RepoSite::Sourcehut => "https://git.sr.ht",
RepoSite::Codeberg => "https://codeberg.org",
RepoSite::Gitea(domain) => domain.as_ref(),
}
}
pub fn to_usercontent_repo_suffix(&self) -> &'static str {
match self {
RepoSite::Github => "HEAD",
RepoSite::Gitlab(_) | RepoSite::Bitbucket => "raw/HEAD",
RepoSite::Sourcehut => "blob/HEAD",
RepoSite::Codeberg | RepoSite::Gitea(_) => "raw",
}
}
}
impl FromStr for RepoSite {
type Err = Error;
fn from_str(input: &str) -> Result<RepoSite, Error> {
if let Some((site, domain)) = input.split_once('/') {
match site {
"gitea" => Ok(RepoSite::Gitea(domain.parse()?)),
"gitlab" => Ok(RepoSite::Gitlab(Some(domain.parse()?))),
_ => Err(anyhow!("unknown repo site identifier")),
}
} else {
match input {
"github" => Ok(RepoSite::Github),
"gitlab" => Ok(RepoSite::Gitlab(None)),
"bitbucket" => Ok(RepoSite::Bitbucket),
"sourcehut" => Ok(RepoSite::Sourcehut),
"codeberg" => Ok(RepoSite::Codeberg),
_ => Err(anyhow!("unknown repo site identifier")),
}
}
}
}
impl fmt::Display for RepoSite {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RepoSite::Github => write!(f, "github"),
RepoSite::Gitlab(None) => write!(f, "gitlab"),
RepoSite::Gitlab(Some(s)) => write!(f, "gitlab/{s}"),
RepoSite::Bitbucket => write!(f, "bitbucket"),
RepoSite::Sourcehut => write!(f, "sourcehut"),
RepoSite::Codeberg => write!(f, "codeberg"),
RepoSite::Gitea(s) => write!(f, "gitea/{s}"),
}
}
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct GiteaDomain(String);
impl FromStr for GiteaDomain {
type Err = Error;
fn from_str(input: &str) -> Result<GiteaDomain, Error> {
if input.starts_with("https://") || input.starts_with("http://") {
Ok(GiteaDomain(input.to_string()))
} else {
Ok(GiteaDomain(format!("https://{input}")))
}
}
}
impl AsRef<str> for GiteaDomain {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl fmt::Display for GiteaDomain {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.0.starts_with("https://") {
f.write_str(&self.0["https://".len()..])
} else {
self.0.fmt(f)
}
}
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct RepoQualifier(String);
impl FromStr for RepoQualifier {
type Err = Error;
fn from_str(input: &str) -> Result<RepoQualifier, Error> {
let is_valid = input.chars().all(|c| {
c.is_ascii_alphanumeric() || c == '.' || c == '-' || c == '_'
// Sourcehut projects have the form
// https://git.sr.ht/~user/project.
|| c == '~'
});
ensure!(is_valid, "invalid repo qualifier");
Ok(RepoQualifier(input.to_string()))
}
}
impl AsRef<str> for RepoQualifier {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct RepoName(String);
impl FromStr for RepoName {
type Err = Error;
fn from_str(input: &str) -> Result<RepoName, Error> {
let is_valid = input
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-' || c == '_');
ensure!(is_valid, "invalid repo name");
Ok(RepoName(input.to_string()))
}
}
impl AsRef<str> for RepoName {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn correct_raw_url_generation() {
let paths = [
("Cargo.toml", "Cargo.toml"),
("/Cargo.toml", "Cargo.toml"),
("libs/badge/Cargo.toml", "libs/badge/Cargo.toml"),
("/libs/badge/Cargo.toml", "libs/badge/Cargo.toml"),
("src/../libs/badge/Cargo.toml", "libs/badge/Cargo.toml"),
("/src/../libs/badge/Cargo.toml", "libs/badge/Cargo.toml"),
];
for (input, expected) in &paths {
let repo = RepoPath::from_parts("github", "deps-rs", "deps.rs").unwrap();
let out = repo.to_usercontent_file_url(RelativePath::new(input));
let exp = format!("https://raw.githubusercontent.com/deps-rs/deps.rs/HEAD/{expected}");
assert_eq!(out.to_string(), exp);
}
for (input, expected) in &paths {
let repo = RepoPath::from_parts("gitlab", "deps-rs", "deps.rs").unwrap();
let out = repo.to_usercontent_file_url(RelativePath::new(input));
let exp = format!("https://gitlab.com/deps-rs/deps.rs/raw/HEAD/{expected}");
assert_eq!(out.to_string(), exp);
}
for (input, expected) in &paths {
let repo = RepoPath::from_parts("bitbucket", "deps-rs", "deps.rs").unwrap();
let out = repo.to_usercontent_file_url(RelativePath::new(input));
let exp = format!("https://bitbucket.org/deps-rs/deps.rs/raw/HEAD/{expected}");
assert_eq!(out.to_string(), exp);
}
for (input, expected) in &paths {
let repo = RepoPath::from_parts("codeberg", "deps-rs", "deps.rs").unwrap();
let out = repo.to_usercontent_file_url(RelativePath::new(input));
let exp = format!("https://codeberg.org/deps-rs/deps.rs/raw/{expected}");
assert_eq!(out.to_string(), exp);
}
for (input, expected) in &paths {
let repo = RepoPath::from_parts("gitea/gitea.com", "deps-rs", "deps.rs").unwrap();
let out = repo.to_usercontent_file_url(RelativePath::new(input));
let exp = format!("https://gitea.com/deps-rs/deps.rs/raw/{expected}");
assert_eq!(out.to_string(), exp);
}
for (input, expected) in &paths {
let repo = RepoPath::from_parts("gitea/example.com/git", "deps-rs", "deps.rs").unwrap();
let out = repo.to_usercontent_file_url(RelativePath::new(input));
let exp = format!("https://example.com/git/deps-rs/deps.rs/raw/{expected}");
assert_eq!(out.to_string(), exp);
}
for (input, expected) in &paths {
let repo = RepoPath::from_parts("gitlab/gitlab.com", "deps-rs", "deps.rs").unwrap();
let out = repo.to_usercontent_file_url(RelativePath::new(input));
let exp = format!("https://gitlab.com/deps-rs/deps.rs/raw/HEAD/{expected}");
assert_eq!(out.to_string(), exp);
}
}
}