2018-01-26 12:15:53 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2018-02-05 09:23:40 +00:00
|
|
|
use failure::Error;
|
|
|
|
|
2018-02-05 08:38:04 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2018-02-03 09:12:00 +00:00
|
|
|
pub struct Repository {
|
|
|
|
pub path: RepoPath,
|
|
|
|
pub description: String
|
|
|
|
}
|
|
|
|
|
2018-02-11 08:09:49 +00:00
|
|
|
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
2018-01-26 12:15:53 +00:00
|
|
|
pub struct RepoPath {
|
|
|
|
pub site: RepoSite,
|
|
|
|
pub qual: RepoQualifier,
|
|
|
|
pub name: RepoName
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RepoPath {
|
2018-02-05 09:23:40 +00:00
|
|
|
pub fn from_parts(site: &str, qual: &str, name: &str) -> Result<RepoPath, Error> {
|
2018-01-26 12:15:53 +00:00
|
|
|
Ok(RepoPath {
|
|
|
|
site: site.parse()?,
|
|
|
|
qual: qual.parse()?,
|
|
|
|
name: name.parse()?
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 08:09:49 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
|
2018-01-27 11:49:43 +00:00
|
|
|
pub enum RepoSite {
|
|
|
|
Github
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RepoSite {
|
|
|
|
pub fn to_base_uri(&self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
&RepoSite::Github => "https://github.com"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-26 12:15:53 +00:00
|
|
|
|
|
|
|
impl FromStr for RepoSite {
|
2018-02-05 09:23:40 +00:00
|
|
|
type Err = Error;
|
2018-01-26 12:15:53 +00:00
|
|
|
|
2018-02-05 09:23:40 +00:00
|
|
|
fn from_str(input: &str) -> Result<RepoSite, Error> {
|
2018-01-27 11:49:43 +00:00
|
|
|
match input {
|
|
|
|
"github" => Ok(RepoSite::Github),
|
2018-02-05 09:23:40 +00:00
|
|
|
_ => Err(format_err!("unknown repo site identifier"))
|
2018-01-26 12:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<str> for RepoSite {
|
|
|
|
fn as_ref(&self) -> &str {
|
2018-01-27 11:49:43 +00:00
|
|
|
match self {
|
|
|
|
&RepoSite::Github => "github"
|
|
|
|
}
|
2018-01-26 12:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 08:09:49 +00:00
|
|
|
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
2018-01-26 12:15:53 +00:00
|
|
|
pub struct RepoQualifier(String);
|
|
|
|
|
|
|
|
impl FromStr for RepoQualifier {
|
2018-02-05 09:23:40 +00:00
|
|
|
type Err = Error;
|
2018-01-26 12:15:53 +00:00
|
|
|
|
2018-02-05 09:23:40 +00:00
|
|
|
fn from_str(input: &str) -> Result<RepoQualifier, Error> {
|
2018-01-26 12:15:53 +00:00
|
|
|
let is_valid = input.chars().all(|c| {
|
2018-01-27 23:08:07 +00:00
|
|
|
c.is_ascii_alphanumeric() || c == '.' || c == '-' || c == '_'
|
2018-01-26 12:15:53 +00:00
|
|
|
});
|
|
|
|
|
2018-02-05 09:23:40 +00:00
|
|
|
ensure!(is_valid, "invalid repo qualifier");
|
|
|
|
Ok(RepoQualifier(input.to_string()))
|
2018-01-26 12:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<str> for RepoQualifier {
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
self.0.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 08:09:49 +00:00
|
|
|
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
2018-01-26 12:15:53 +00:00
|
|
|
pub struct RepoName(String);
|
|
|
|
|
|
|
|
impl FromStr for RepoName {
|
2018-02-05 09:23:40 +00:00
|
|
|
type Err = Error;
|
2018-01-26 12:15:53 +00:00
|
|
|
|
2018-02-05 09:23:40 +00:00
|
|
|
fn from_str(input: &str) -> Result<RepoName, Error> {
|
2018-01-26 12:15:53 +00:00
|
|
|
let is_valid = input.chars().all(|c| {
|
2018-01-27 23:08:07 +00:00
|
|
|
c.is_ascii_alphanumeric() || c == '.' || c == '-' || c == '_'
|
2018-01-26 12:15:53 +00:00
|
|
|
});
|
|
|
|
|
2018-02-05 09:23:40 +00:00
|
|
|
ensure!(is_valid, "invalid repo name");
|
|
|
|
Ok(RepoName(input.to_string()))
|
2018-01-26 12:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<str> for RepoName {
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
self.0.as_ref()
|
|
|
|
}
|
|
|
|
}
|