use std::path::PathBuf;
#[derive(Debug, thiserror::Error)]
pub enum KitError {
#[error("plugin {name} is not installed")]
NotInstalled {
name: String,
},
#[error("directory for plugin {name} already exists: {path}")]
AlreadyInstalled {
name: String,
path: PathBuf,
},
#[error(
"the dynamic library of plugin {name} is still loaded in this process and cannot be \
deleted. Retry in a new terminal (a new process does not load it), or exit the \
current session and try again"
)]
PluginInUse {
name: String,
},
#[error("failed to read manifest ({path}): {source}")]
ManifestRead {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("failed to parse manifest ({path}): {source}")]
ManifestParse {
path: PathBuf,
#[source]
source: Box<toml::de::Error>,
},
#[error("manifest is missing field `{field}` ({path})")]
ManifestMissingField {
field: String,
path: PathBuf,
},
#[error("could not find the dynamic library of plugin {name}; tried: {tried}")]
LibraryNotFound {
name: String,
tried: String,
},
#[error("failed to load the dynamic library ({path}): {source}")]
LibraryLoad {
path: PathBuf,
#[source]
source: Box<libloading::Error>,
},
#[error("the dynamic library does not export symbol `{symbol}`")]
SymbolMissing {
symbol: String,
},
#[error("the plugin's entry function returned a null pointer")]
NullEntry,
#[error("timed out waiting for the file lock ({path}, waited {secs}s)")]
LockTimeout {
path: PathBuf,
secs: u64,
},
#[error("file lock operation failed ({path}): {source}")]
LockIo {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("could not find `cargo` on PATH; build-host installation needs a Rust toolchain")]
CargoNotFound,
#[error("build-host compilation failed ({})", describe_code(.code))]
BuildFailed {
code: Option<i32>,
},
#[error("no cdylib among the build artifacts (looked for {stem} in {dir})")]
BuildArtifactMissing {
stem: String,
dir: String,
},
#[error("no Cargo.toml in {dir}")]
NoManifest {
dir: PathBuf,
},
#[error("cargo metadata does not describe the package at {manifest}")]
NoPackage {
manifest: PathBuf,
},
#[error(
"the plugin manifest {path} declares version {declared}, but the crate version is \
{crate_version}"
)]
VersionMismatch {
path: PathBuf,
declared: String,
crate_version: String,
},
#[error("network request failed: {0}")]
Http(String),
#[error("registry returned an unexpected response: {0}")]
Registry(String),
#[error("plugin panicked in {what} (caught by catch_unwind)")]
PluginPanicked {
what: String,
},
#[error("cannot determine the host application's data dir (no HOME and no system equivalent)")]
NoDataDir,
#[error(transparent)]
Io(#[from] std::io::Error),
}
pub type KitResult<T> = std::result::Result<T, KitError>;
fn describe_code(code: &Option<i32>) -> String {
match code {
Some(code) => format!("exit code {code}"),
None => "killed by a signal".to_string(),
}
}