mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-06-17 08:06:38 +00:00
Add GhRepo::try_extract_from_url
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
7e283cc407
commit
81bdc3ba6e
1 changed files with 39 additions and 3 deletions
|
@ -10,7 +10,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use binstalk_downloader::{download::Download, remote};
|
use binstalk_downloader::{download::Download, remote};
|
||||||
use compact_str::{format_compact, CompactString};
|
use compact_str::{format_compact, CompactString, ToCompactString};
|
||||||
use tokio::sync::OnceCell;
|
use tokio::sync::OnceCell;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
@ -32,8 +32,25 @@ pub struct GhRepo {
|
||||||
pub repo: CompactString,
|
pub repo: CompactString,
|
||||||
}
|
}
|
||||||
impl GhRepo {
|
impl GhRepo {
|
||||||
pub fn repo_url(&self) -> CompactString {
|
pub fn repo_url(&self) -> Result<Url, url::ParseError> {
|
||||||
format_compact!("https://github.com/{}/{}", self.owner, self.repo)
|
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> {
|
fn try_extract_artifact_from_str(s: &str) -> Option<GhReleaseArtifact> {
|
||||||
GhReleaseArtifact::try_extract_from_url(&url::Url::parse(s).unwrap())
|
GhReleaseArtifact::try_extract_from_url(&url::Url::parse(s).unwrap())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue