use std::{io, path::PathBuf};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum BuilderError {
#[error("Could not find Cargo.toml file starting from current folder: {0:?}")]
LocateCargoManifest(#[from] locate_cargo_manifest::LocateManifestError),
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum BuildKernelError {
#[error("I/O error: {message}:\n{error}")]
Io {
message: &'static str,
error: io::Error,
},
#[error(
"Failed to run `cargo xbuild`. Perhaps it is not installed?\n\
Run `cargo install cargo-xbuild` to install it."
)]
XbuildNotFound,
#[error("Kernel build failed.\nStderr: {}", String::from_utf8_lossy(.stderr))]
BuildFailed {
stderr: Vec<u8>,
},
#[error("Output of kernel build with --message-format=json is not valid UTF-8:\n{0}")]
BuildJsonOutputInvalidUtf8(std::string::FromUtf8Error),
#[error("Output of kernel build with --message-format=json is not valid JSON:\n{0}")]
BuildJsonOutputInvalidJson(json::Error),
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum CreateBootimageError {
#[error("An error occurred while trying to build the bootloader: {0}")]
Bootloader(#[from] BootloaderError),
#[error("Error while running `cargo metadata` for current project: {0:?}")]
CargoMetadata(#[from] cargo_metadata::Error),
#[error("Bootloader build failed.\nStderr: {}", String::from_utf8_lossy(.stderr))]
BootloaderBuildFailed {
stderr: Vec<u8>,
},
#[error("An error occurred while trying to create the disk image: {0}")]
DiskImage(#[from] DiskImageError),
#[error("I/O error: {message}:\n{error}")]
Io {
message: &'static str,
error: io::Error,
},
#[error("Output of bootloader build with --message-format=json is not valid UTF-8:\n{0}")]
BuildJsonOutputInvalidUtf8(std::string::FromUtf8Error),
#[error("Output of bootloader build with --message-format=json is not valid JSON:\n{0}")]
BuildJsonOutputInvalidJson(json::Error),
}
#[derive(Debug, Error)]
pub enum BootloaderError {
#[error(
"Bootloader dependency not found\n\n\
You need to add a dependency on a crate named `bootloader` in your Cargo.toml."
)]
BootloaderNotFound,
#[error("The `bootloader` dependency has not the right format: {0}")]
BootloaderInvalid(String),
#[error(
"Could not find package with manifest path `{manifest_path}` in cargo metadata output"
)]
KernelPackageNotFound {
manifest_path: PathBuf,
},
#[error("Could not find required key `{key}` in cargo metadata output")]
CargoMetadataIncomplete {
key: String,
},
}
#[derive(Debug, Error)]
pub enum DiskImageError {
#[error(
"Could not find the `llvm-tools-preview` rustup component.\n\n\
You can install by executing `rustup component add llvm-tools-preview`."
)]
LlvmToolsNotFound,
#[error("Failed to locate the `llvm-tools-preview` rustup component: {0:?}")]
LlvmTools(llvm_tools::Error),
#[error("Could not find `llvm-objcopy` in the `llvm-tools-preview` rustup component.")]
LlvmObjcopyNotFound,
#[error("Failed to run `llvm-objcopy`: {}", String::from_utf8_lossy(.stderr))]
ObjcopyFailed {
stderr: Vec<u8>,
},
#[error("I/O error: {message}:\n{error}")]
Io {
message: &'static str,
error: io::Error,
},
}
impl From<llvm_tools::Error> for DiskImageError {
fn from(err: llvm_tools::Error) -> Self {
match err {
llvm_tools::Error::NotFound => DiskImageError::LlvmToolsNotFound,
other => DiskImageError::LlvmTools(other),
}
}
}