Add GhRepo::try_extract_from_url

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2024-06-04 23:23:48 +10:00
parent 7e283cc407
commit 81bdc3ba6e
No known key found for this signature in database
GPG key ID: 76D1E687CA3C4928

View file

@ -10,7 +10,7 @@ use std::{
};
use binstalk_downloader::{download::Download, remote};
use compact_str::{format_compact, CompactString};
use compact_str::{format_compact, CompactString, ToCompactString};
use tokio::sync::OnceCell;
use url::Url;
@ -32,8 +32,25 @@ pub struct GhRepo {
pub repo: CompactString,
}
impl GhRepo {
pub fn repo_url(&self) -> CompactString {
format_compact!("https://github.com/{}/{}", self.owner, self.repo)
pub fn repo_url(&self) -> Result<Url, url::ParseError> {
Url::parse(&format_compact!(
"https://github.com/{}/{}",
self.owner,
self.repo
))
}
pub fn try_extract_from_url(url: &Url) -> Option<Self> {
if url.domain() != Some("github.com") {
return None;
}
let mut path_segments = url.path_segments()?;
Some(Self {
owner: path_segments.next()?.to_compact_string(),
repo: path_segments.next()?.to_compact_string(),
})
}
}
@ -373,6 +390,25 @@ mod test {
}
}
#[test]
fn gh_repo_extract_from_and_to_url() {
[
"https://github.com/cargo-bins/cargo-binstall",
"https://github.com/rustsec/rustsec",
]
.into_iter()
.for_each(|url| {
let url = Url::parse(&url).unwrap();
assert_eq!(
GhRepo::try_extract_from_url(&url)
.unwrap()
.repo_url()
.unwrap(),
url
);
})
}
fn try_extract_artifact_from_str(s: &str) -> Option<GhReleaseArtifact> {
GhReleaseArtifact::try_extract_from_url(&url::Url::parse(s).unwrap())
}