use std::ffi::NulError;
use std::io;
use std::path::PathBuf;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error)]
pub enum Error {
#[error("{field} contains an interior NUL byte")]
InvalidCString {
field: &'static str,
#[source]
source: NulError,
},
#[error("{field}: {source}: {}", path.display())]
Io {
field: &'static str,
path: PathBuf,
#[source]
source: io::Error,
},
#[error("osslsigncode runtime error: {message}")]
Runtime {
message: String,
},
#[error("{operation} failed with status {code}{detail}")]
Failed {
operation: &'static str,
code: i32,
detail: String,
},
}
impl Error {
#[must_use]
pub fn status_code(&self) -> Option<i32> {
match self {
Self::Failed { code, .. } => Some(*code),
_ => None,
}
}
}
pub(crate) fn failed(operation: &'static str, code: i32, message: Option<String>) -> Error {
let detail = message
.filter(|text| !text.is_empty())
.map(|text| format!(": {text}"))
.unwrap_or_default();
Error::Failed {
operation,
code,
detail,
}
}
pub(crate) fn io(field: &'static str, path: impl Into<PathBuf>, source: io::Error) -> Error {
Error::Io {
field,
path: path.into(),
source,
}
}