utils.rs 742 B

123456789101112131415161718192021222324
  1. use std::{cell::OnceCell, process::Command};
  2. use anyhow::{bail, Context as _, Result};
  3. pub fn workspace_root() -> &'static str {
  4. static mut WORKSPACE_ROOT: OnceCell<String> = OnceCell::new();
  5. unsafe { &mut WORKSPACE_ROOT }.get_or_init(|| {
  6. let cmd = cargo_metadata::MetadataCommand::new();
  7. cmd.exec().unwrap().workspace_root.to_string()
  8. })
  9. }
  10. pub fn exec(cmd: &mut Command) -> Result<()> {
  11. let status = cmd
  12. .status()
  13. .with_context(|| format!("failed to run {cmd:?}"))?;
  14. match status.code() {
  15. Some(code) => match code {
  16. 0 => Ok(()),
  17. code => bail!("{cmd:?} exited with code {code}"),
  18. },
  19. None => bail!("{cmd:?} terminated by signal"),
  20. }
  21. }