2019-10-09 01:40:40 -07:00
|
|
|
use crate::common::*;
|
2019-06-01 22:38:03 -07:00
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) enum SearchError {
|
2019-06-01 22:38:03 -07:00
|
|
|
MultipleCandidates {
|
|
|
|
candidates: Vec<PathBuf>,
|
|
|
|
},
|
|
|
|
Io {
|
|
|
|
directory: PathBuf,
|
|
|
|
io_error: io::Error,
|
|
|
|
},
|
|
|
|
NotFound,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for SearchError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
SearchError::Io {
|
|
|
|
directory,
|
|
|
|
io_error,
|
|
|
|
} => write!(
|
|
|
|
f,
|
|
|
|
"I/O error reading directory `{}`: {}",
|
|
|
|
directory.display(),
|
|
|
|
io_error
|
|
|
|
),
|
|
|
|
SearchError::MultipleCandidates { candidates } => write!(
|
|
|
|
f,
|
|
|
|
"Multiple candidate justfiles found in `{}`: {}",
|
|
|
|
candidates[0].parent().unwrap().display(),
|
2019-10-09 01:40:40 -07:00
|
|
|
List::and_ticked(
|
|
|
|
candidates
|
2019-06-01 22:38:03 -07:00
|
|
|
.iter()
|
2019-10-09 01:40:40 -07:00
|
|
|
.map(|candidate| candidate.file_name().unwrap().to_string_lossy())
|
2019-06-01 22:38:03 -07:00
|
|
|
),
|
|
|
|
),
|
|
|
|
SearchError::NotFound => write!(f, "No justfile found"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_candidates_formatting() {
|
|
|
|
let error = SearchError::MultipleCandidates {
|
|
|
|
candidates: vec![
|
|
|
|
PathBuf::from("/foo/justfile"),
|
|
|
|
PathBuf::from("/foo/JUSTFILE"),
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
error.to_string(),
|
|
|
|
"Multiple candidate justfiles found in `/foo`: `justfile` and `JUSTFILE`"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|