github_ureq/
utils.rs

1use thiserror::Error;
2use url::Url;
3
4#[derive(Clone, Debug, Error)]
5pub enum Error {
6    #[error("the URL provided appears to be malformed")]
7    ParseError(#[from] Option<url::ParseError>),
8    #[error("the URL provided has no separator to indicate an owner and repository")]
9    MissingSeparator,
10}
11
12pub type Result<T> = std::result::Result<T, Error>;
13
14#[derive(Clone, Debug)]
15pub struct RepositoryNamespace {
16    pub owner: String,
17    pub repository: String,
18}
19
20pub fn namespace<S>(url: S) -> Result<RepositoryNamespace>
21where
22    S: AsRef<str>,
23{
24    let url = Url::parse(url.as_ref())?;
25    let path = url.path();
26    let (owner, repository) = path
27        .strip_prefix('/')
28        .unwrap_or(path)
29        .split_once('/')
30        .ok_or(Error::MissingSeparator)?;
31
32    Ok(RepositoryNamespace {
33        owner: owner.to_owned(),
34        repository: repository.to_owned(),
35    })
36}