Skip to content

Instantly share code, notes, and snippets.

@autarch
Created March 14, 2019 20:33
Show Gist options
  • Save autarch/8071923edbb45fdb6e66f3feae29e236 to your computer and use it in GitHub Desktop.
Save autarch/8071923edbb45fdb6e66f3feae29e236 to your computer and use it in GitHub Desktop.
#[macro_use]
extern crate failure_derive;
...
#[derive(Debug, Fail)]
enum RuntimeError {
CannotFindRoot { cwd: PathBuf },
}
impl fmt::Display for RuntimeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RuntimeError::CannotFindRoot { cwd: cwd } => write!(
f,
"Could not find a VCS checkout root starting from {}",
cwd.to_string_lossy()
),
}
}
}
...
fn set_root(&mut self) -> Result<(), Error> {
let cwd = env::current_dir()?;
let mut root = PathBuf::new();
for anc in cwd.ancestors() {
if self.is_checkout_root(&anc) {
root.push(anc);
self.root = Some(root);
return Ok(());
}
}
Err(RuntimeError::CannotFindRoot { cwd: cwd })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment