use std::io;
use std::path::PathBuf;
use std::result;
use std::str::Utf8Error;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("{path}: {source}")]
PathIo { source: io::Error, path: PathBuf },
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
Logger(#[from] log::SetLoggerError),
#[error(transparent)]
Utf8(#[from] Utf8Error),
#[error(transparent)]
TomlDeserialization(#[from] toml::de::Error),
#[error(transparent)]
TomlManifest(#[from] cargo_toml::Error),
#[error("Target selection flags (--bin, --example, --bench, --test) are mutually exclusive")]
MultipleTargetsFlagsSpecified,
#[error("Build failed")]
CargoBuildFailed,
#[error("No binary found in 'Cargo.toml'")]
NoBinaryFound,
#[error("The binary to run can't be determined. Use the `--bin` option to specify a binary, or the `default-run` manifest key.{suggestions}")]
BinaryToRunNotDetermined { suggestions: String },
#[error("Failed to locate project")]
CargoLocateProjectFailed,
#[error("Binary not found: {path}")]
BinaryNotFound { path: PathBuf },
#[error("Package '{name}' not found in workspace")]
PackageNotFound { name: String },
#[error("samply is not installed or not in PATH")]
SamplyNotFound,
#[error("Failed to get Rust sysroot: {message}")]
RustSysrootFailed { message: String },
#[error("Failed to get Rust host target: {message}")]
RustHostTargetFailed { message: String },
#[error("Invalid samply arguments: {0}")]
InvalidSamplyArgs(String),
#[error("Failed to capture cargo stdout: {0}")]
CargoStdoutCaptureFailed(String),
#[error("Cargo metadata failed: {0}")]
CargoMetadataFailed(#[from] cargo_metadata::Error),
}
pub type Result<T> = result::Result<T, Error>;
pub trait IOResultExt<T> {
fn path_ctx<P: Into<PathBuf>>(self, path: P) -> Result<T>;
}
impl<T> IOResultExt<T> for io::Result<T> {
fn path_ctx<P: Into<PathBuf>>(self, path: P) -> Result<T> {
self.map_err(|source| Error::PathIo {
source,
path: path.into(),
})
}
}