Skip to main content

dealer/
error.rs

1use std::io;
2use std::path::PathBuf;
3use std::process::Output;
4
5pub(crate) type DealerResult<T> = Result<T, DealerError>;
6
7#[derive(Debug, thiserror::Error)]
8pub(crate) enum DealerError {
9    #[error("{0}")]
10    PackageResolution(String),
11    #[error("{0}")]
12    Compiler(String),
13    #[error("{0}")]
14    Backend(String),
15    #[error("{feature} is recognized but not implemented yet")]
16    NotImplemented { feature: String },
17    #[error("I/O error at '{}': {source}", path.display())]
18    Io { path: PathBuf, source: io::Error },
19}
20
21impl DealerError {
22    pub(crate) fn io(path: impl Into<PathBuf>, source: io::Error) -> Self {
23        Self::Io {
24            path: path.into(),
25            source,
26        }
27    }
28}
29
30pub(crate) fn process_output_message(output: Output, label: &str) -> String {
31    let stdout = String::from_utf8_lossy(&output.stdout);
32    let stderr = String::from_utf8_lossy(&output.stderr);
33    let mut message = String::new();
34    if !stdout.trim().is_empty() {
35        message.push_str(&stdout);
36    }
37    if !stderr.trim().is_empty() {
38        message.push_str(&stderr);
39    }
40    if message.is_empty() {
41        message.push_str(&format!("{label} exited with status {}", output.status));
42    }
43    message
44}