use std::fmt;
use crate::types::project::{
CommitId, GitTag, GitUrl, Pin, ProjectError, Source, SourceError, SubPath,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GitOption {
Tag,
Rev,
Subdir,
}
impl fmt::Display for GitOption {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Tag => "--tag",
Self::Rev => "--rev",
Self::Subdir => "--subdir",
})
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct GitFlags<'a> {
pub tag: Option<&'a str>,
pub rev: Option<&'a str>,
pub subdir: Option<&'a str>,
}
impl GitFlags<'_> {
#[must_use]
pub fn without_git(&self) -> Vec<ProjectError> {
[
(GitOption::Tag, self.tag),
(GitOption::Rev, self.rev),
(GitOption::Subdir, self.subdir),
]
.into_iter()
.filter(|(_, value)| value.is_some())
.map(|(flag, _)| ProjectError::FlagNeedsGit { flag })
.collect()
}
pub fn source(&self, url: GitUrl) -> Result<Source, Vec<ProjectError>> {
let mut refused = Vec::new();
let pin = match (self.tag, self.rev) {
(Some(_), Some(_)) => {
refused.push(ProjectError::PinConflict);
None
}
(Some(tag), None) => {
value(GitOption::Tag, GitTag::new(tag), &mut refused).map(Pin::Tag)
}
(None, Some(rev)) => {
value(GitOption::Rev, CommitId::new(rev), &mut refused).map(Pin::Rev)
}
(None, None) => {
refused.push(ProjectError::GitNeedsPin);
None
}
};
let subdir = self.subdir.map_or(Some(None), |text| {
value(GitOption::Subdir, SubPath::new(text), &mut refused).map(Some)
});
match (pin, subdir) {
(Some(pin), Some(subdir)) if refused.is_empty() => Ok(Source::Git { url, pin, subdir }),
_ => Err(refused),
}
}
}
fn value<T, E: Into<SourceError>>(
flag: GitOption,
result: Result<T, E>,
refused: &mut Vec<ProjectError>,
) -> Option<T> {
match result {
Ok(found) => Some(found),
Err(error) => {
refused.push(ProjectError::FlagValue {
flag,
error: error.into(),
});
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
type TestResult = Result<(), Box<dyn std::error::Error>>;
fn mytool() -> Result<Source, Box<dyn std::error::Error>> {
Ok(Source::Git {
url: GitUrl::new("https://example.com/x.git")?,
pin: Pin::Tag(GitTag::new("v1.2.0")?),
subdir: Some(SubPath::new("ext/mytool")?),
})
}
const REV: &str = "0123456789abcdef0123456789abcdef01234567";
fn flags_source(flags: GitFlags<'_>) -> Result<Source, Vec<String>> {
let url = GitUrl::new("https://example.com/x.git").map_err(|e| vec![e.to_string()])?;
flags
.source(url)
.map_err(|refused| refused.iter().map(ToString::to_string).collect())
}
#[test]
fn git_flags_build_the_source() -> TestResult {
let flags = GitFlags {
tag: Some("v1.2.0"),
rev: None,
subdir: Some("ext/mytool"),
};
assert_eq!(flags_source(flags), Ok(mytool()?));
let by_rev = flags_source(GitFlags {
tag: None,
rev: Some(REV),
subdir: None,
});
assert_eq!(
by_rev,
Ok(Source::Git {
url: GitUrl::new("https://example.com/x.git")?,
pin: Pin::Rev(CommitId::new(REV)?),
subdir: None,
})
);
Ok(())
}
#[test]
fn git_flags_refuse_each_bad_value_under_its_flag() {
let cases = [
(GitFlags::default(), "a git URL needs --tag or --rev"),
(
GitFlags {
tag: Some("v1"),
rev: Some(REV),
subdir: None,
},
"--tag and --rev cannot be used together",
),
(
GitFlags {
tag: Some(" "),
..GitFlags::default()
},
"--tag: empty",
),
(
GitFlags {
rev: Some("abc"),
..GitFlags::default()
},
"--rev: not a commit id",
),
(
GitFlags {
tag: Some("v1"),
subdir: Some("../x"),
..GitFlags::default()
},
"--subdir: must stay inside the repository",
),
(
GitFlags {
tag: Some("v1"),
subdir: Some(""),
..GitFlags::default()
},
"--subdir: empty",
),
];
for (flags, message) in cases {
assert_eq!(flags_source(flags), Err(vec![message.to_owned()]));
}
}
#[test]
fn git_flags_report_the_pin_and_the_subdir_together() {
let flags = GitFlags {
rev: Some("abc"),
subdir: Some("/x"),
..GitFlags::default()
};
assert_eq!(
flags_source(flags),
Err(vec![
"--rev: not a commit id".to_owned(),
"--subdir: must stay inside the repository".to_owned(),
])
);
}
#[test]
fn git_flags_without_git_refuse_each_option_given() {
let flags = GitFlags {
tag: Some("v1"),
rev: None,
subdir: Some(""),
};
let shown: Vec<String> = flags
.without_git()
.iter()
.map(ToString::to_string)
.collect();
assert_eq!(
shown,
[
"--tag applies only to a git URL",
"--subdir applies only to a git URL"
]
);
assert!(GitFlags::default().without_git().is_empty());
}
}