scarb_metadata/command/
scarb_command.rs1use std::ffi::OsStr;
2use std::io;
3use std::path::PathBuf;
4
5use crate::command::internal_command::InternalScarbCommandBuilder;
6use thiserror::Error;
7
8#[derive(Error, Debug)]
10#[non_exhaustive]
11pub enum ScarbCommandError {
12 #[error("failed to read `scarb` output")]
14 Io(#[from] io::Error),
15 #[error("`scarb` command exited with error")]
17 ScarbError,
18}
19
20#[derive(Clone, Debug, Default)]
22pub struct ScarbCommand {
23 inner: InternalScarbCommandBuilder,
24}
25
26impl ScarbCommand {
27 pub fn new() -> Self {
30 let mut cmd = InternalScarbCommandBuilder::new();
31 cmd.inherit_stderr();
32 cmd.inherit_stdout();
33 Self { inner: cmd }
34 }
35
36 pub fn scarb_path(&mut self, path: impl Into<PathBuf>) -> &mut Self {
41 self.inner.scarb_path(path);
42 self
43 }
44
45 pub fn manifest_path(&mut self, path: impl Into<PathBuf>) -> &mut Self {
49 self.inner.manifest_path(path);
50 self
51 }
52
53 pub fn current_dir(&mut self, path: impl Into<PathBuf>) -> &mut Self {
55 self.inner.current_dir(path);
56 self
57 }
58
59 pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self {
61 self.inner.arg(arg);
62 self
63 }
64
65 pub fn args<I, S>(&mut self, args: I) -> &mut Self
67 where
68 I: IntoIterator<Item = S>,
69 S: AsRef<OsStr>,
70 {
71 self.inner.args(args);
72 self
73 }
74
75 pub fn env(&mut self, key: impl AsRef<OsStr>, val: impl AsRef<OsStr>) -> &mut Self {
77 self.inner.env(key, val);
78 self
79 }
80
81 pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
83 where
84 I: IntoIterator<Item = (K, V)>,
85 K: AsRef<OsStr>,
86 V: AsRef<OsStr>,
87 {
88 self.inner.envs(vars);
89 self
90 }
91
92 pub fn env_remove(&mut self, key: impl AsRef<OsStr>) -> &mut Self {
94 self.inner.env_remove(key);
95 self
96 }
97
98 pub fn env_clear(&mut self) -> &mut Self {
100 self.inner.env_clear();
101 self
102 }
103
104 pub fn run(&self) -> Result<(), ScarbCommandError> {
106 let mut cmd = self.inner.command();
107 if cmd.status()?.success() {
108 Ok(())
109 } else {
110 Err(ScarbCommandError::ScarbError)
111 }
112 }
113}