use std::{io, path::PathBuf};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum CliError {
#[error("invalid arguments: {message}")]
InvalidArguments { message: String },
#[error("could not find kayro.toml from `{start}` or any parent directory")]
ProjectNotFound { start: String },
#[error("project path `{path}` does not exist or cannot be accessed: {source}")]
InvalidProjectPath { path: PathBuf, source: io::Error },
#[error("could not read `{path}`: {source}")]
ReadConfig { path: PathBuf, source: io::Error },
#[error("could not parse `{path}`: {source}")]
ParseConfig {
path: PathBuf,
source: toml::de::Error,
},
#[error("invalid build configuration `{field}`: {message}")]
InvalidConfig {
field: &'static str,
message: String,
},
#[error("required {kind} does not exist at `{path}`")]
MissingProjectPath { kind: &'static str, path: PathBuf },
#[error(
"Vite cannot reliably run from `{path}` because the path contains {characters}; move or rename the project so its path avoids `#`, `?`, and `*`"
)]
UnsupportedVitePath { path: PathBuf, characters: String },
#[error("cannot create a new project at `{path}` because it already exists; use `kayro init`")]
ScaffoldTargetExists { path: PathBuf },
#[error("cannot initialize `{path}` because it is not a directory")]
ScaffoldTargetNotDirectory { path: PathBuf },
#[error("initialization would overwrite `{path}`")]
ScaffoldConflict { path: PathBuf },
#[error("could not parse existing package.json at `{path}`: {message}")]
InvalidPackageJson { path: PathBuf, message: String },
#[error("existing package.json needs a `{script}` script so Kayro can run the Vite frontend")]
MissingPackageScript { script: &'static str },
#[error("could not write scaffold file `{path}`: {source}")]
WriteScaffold { path: PathBuf, source: io::Error },
#[error("could not start `{program}` for phase `{phase}`: {source}")]
Spawn {
phase: String,
program: String,
source: io::Error,
},
#[error("phase `{phase}` failed with exit code {exit_code}: {command}")]
CommandFailed {
phase: String,
command: String,
exit_code: i32,
},
#[error("Cargo completed successfully but did not report an executable artifact")]
NoExecutableArtifact,
#[error("native packaging failed: {message}")]
Packaging { message: String },
#[error("development process `{phase}` exited unexpectedly with code {exit_code}")]
DevProcessExited { phase: String, exit_code: i32 },
#[error("Vite did not become reachable at {url} within {seconds} seconds")]
DevServerTimeout { url: String, seconds: u64 },
#[error("cannot derive a target triple for architecture `{arch}` on `{os}`; use --target")]
UnsupportedArchitecture { arch: String, os: String },
#[error("invalid development URL `{url}`: {message}")]
InvalidDevUrl { url: String, message: String },
#[error("failed to install the Ctrl-C handler: {0}")]
SignalHandler(#[from] ctrlc::Error),
#[error("operation interrupted")]
Interrupted,
#[error("I/O error while {action}: {source}")]
Io {
action: &'static str,
source: io::Error,
},
}
impl CliError {
pub fn code(&self) -> &'static str {
match self {
Self::InvalidArguments { .. } => "invalid_arguments",
Self::ProjectNotFound { .. } => "project_not_found",
Self::ScaffoldTargetExists { .. }
| Self::ScaffoldTargetNotDirectory { .. }
| Self::ScaffoldConflict { .. }
| Self::InvalidPackageJson { .. }
| Self::MissingPackageScript { .. } => "scaffold_conflict",
Self::UnsupportedVitePath { .. } => "unsupported_vite_path",
Self::InvalidProjectPath { .. }
| Self::ReadConfig { .. }
| Self::ParseConfig { .. }
| Self::InvalidConfig { .. }
| Self::MissingProjectPath { .. }
| Self::UnsupportedArchitecture { .. }
| Self::InvalidDevUrl { .. } => "invalid_project",
Self::Spawn { source, .. } if source.kind() == io::ErrorKind::NotFound => {
"tool_not_found"
}
Self::Spawn { .. } => "process_spawn_failed",
Self::CommandFailed { .. } | Self::NoExecutableArtifact | Self::Packaging { .. } => {
"command_failed"
}
Self::DevProcessExited { .. } => "dev_process_exited",
Self::DevServerTimeout { .. } => "dev_server_timeout",
Self::SignalHandler(_) | Self::Io { .. } | Self::WriteScaffold { .. } => {
"internal_error"
}
Self::Interrupted => "interrupted",
}
}
pub fn exit_code(&self) -> u8 {
match self {
Self::Interrupted => 130,
Self::Spawn { source, .. } if source.kind() == io::ErrorKind::NotFound => 3,
Self::CommandFailed { .. }
| Self::NoExecutableArtifact
| Self::Packaging { .. }
| Self::DevProcessExited { .. }
| Self::DevServerTimeout { .. }
| Self::Spawn { .. } => 4,
Self::InvalidArguments { .. }
| Self::ProjectNotFound { .. }
| Self::InvalidProjectPath { .. }
| Self::ReadConfig { .. }
| Self::ParseConfig { .. }
| Self::InvalidConfig { .. }
| Self::MissingProjectPath { .. }
| Self::UnsupportedArchitecture { .. }
| Self::InvalidDevUrl { .. }
| Self::ScaffoldTargetExists { .. }
| Self::ScaffoldTargetNotDirectory { .. }
| Self::ScaffoldConflict { .. }
| Self::InvalidPackageJson { .. }
| Self::MissingPackageScript { .. }
| Self::UnsupportedVitePath { .. } => 2,
Self::SignalHandler(_) | Self::Io { .. } | Self::WriteScaffold { .. } => 1,
}
}
}