use std::path::Path;
pub const EXIT_UNAVAILABLE: i32 = 69;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum CliError {
#[error("failed to access `{path}`: {source}")]
Io {
path: String,
#[source]
source: std::io::Error,
},
#[error(transparent)]
Manifest(#[from] astrs_manifest::ManifestError),
#[error("manifest failed validation:\n{0}")]
Validation(#[from] astrs_manifest::ValidationErrors),
#[error(transparent)]
Expand(#[from] astrs_manifest::expand::ExpandError),
#[error(transparent)]
Graph(#[from] astrs_graph::GraphBuildError),
#[error(transparent)]
DoraMigrate(#[from] astrs_migrate::DoraMigrateError),
#[error(transparent)]
Ros2Migrate(#[from] astrs_migrate::Ros2MigrateError),
#[error(transparent)]
Daemon(#[from] astrs_daemon::DaemonError),
#[error(transparent)]
Coordinator(#[from] astrs_coordinator::CoordinatorError),
#[error("the coordinator store is unusable: {0}")]
Store(#[from] astrs_store::Error),
#[error(transparent)]
Transport(#[from] astrs_transport::TransportError),
#[error(transparent)]
Runtime(#[from] astrs_runtime::RuntimeError),
#[error(transparent)]
Node(#[from] astrs_node_api::NodeError),
#[error(transparent)]
Recording(#[from] astrs_recording::RecordingError),
#[error(transparent)]
Rosbag(#[from] astrs_rosbag::RosbagError),
#[error("the ROS 2 probe failed: {0}")]
Ros2(String),
#[error(transparent)]
Tui(#[from] astrs_tui::TuiError),
#[error(transparent)]
CoordinatorSource(#[from] astrs_tui::CoordinatorSourceError),
#[error(transparent)]
Replay(#[from] astrs_tui::ReplaySourceError),
#[error("the coordinator refused `{request}`: {message}")]
Refused {
request: &'static str,
message: String,
},
#[error("the coordinator answered `{request}` with an unexpected `{reply}`")]
UnexpectedReply {
request: &'static str,
reply: &'static str,
},
#[error(
"no cluster token: pass `--token`/`--token-file`, set `{env}`, or run `astrs up` in a directory whose `{file}` this process can read"
)]
NoToken {
env: &'static str,
file: &'static str,
},
#[error("the cluster token in `{path}` is not a 64-hex value: {source}")]
BadToken {
path: String,
#[source]
source: astrs_wire::AuthTokenError,
},
#[error("`{input}` is not a usable coordinator address: {reason}")]
BadAddress {
input: String,
reason: String,
},
#[error("`--{flag} {value}` is not valid: {reason}")]
BadArgument {
flag: &'static str,
value: String,
reason: String,
},
#[error("could not {action} the {process}: {reason}")]
Cluster {
action: &'static str,
process: &'static str,
reason: String,
},
#[error("no cluster is running here (`{detail}`); start one with `astrs up`")]
NoCluster {
detail: String,
},
#[error("`{reference}` does not name one dataflow: {reason}")]
UnknownDataflow {
reference: String,
reason: String,
},
#[error("a {process} is already running here (pid {pid}{address}); stop it with `astrs down`")]
AlreadyRunning {
process: &'static str,
pid: u32,
address: String,
},
#[error(
"`{verb}` is not implemented yet (scheduled for {wave}); every other verb `astrs --help` lists is available in this build"
)]
NotImplementedYet {
verb: &'static str,
wave: &'static str,
},
#[error(
"{feature} is not enabled in this build (scheduled for {wave}); rebuild with `--features {feature}` once it ships"
)]
FeatureNotEnabled {
feature: &'static str,
wave: &'static str,
},
#[error(
"--prove needs the `verify` feature, which this build does not enable; the obligations above were encoded but not solved. Rebuild with `cargo install astrs-cli --features verify` (or `cargo build -p astrs-cli --features verify`)."
)]
ProofUnavailable,
#[error(
"--deterministic needs --from-recording <path>: a deterministic run fixes the timer wheel to a recording's HLC stream, and none was given"
)]
DeterministicNeedsRecording,
#[error("refusing to write outside the target directory: `{path}`")]
UnsafePath {
path: String,
},
#[error("`{path}` already exists; pass `--force` to overwrite, or choose an empty directory")]
TargetExists {
path: String,
},
}
impl CliError {
#[must_use]
pub fn io(path: impl AsRef<Path>, source: std::io::Error) -> Self {
Self::Io {
path: path.as_ref().display().to_string(),
source,
}
}
#[must_use]
pub fn exit_code(&self) -> i32 {
match self {
Self::NotImplementedYet { .. }
| Self::FeatureNotEnabled { .. }
| Self::ProofUnavailable => EXIT_UNAVAILABLE,
_ => 1,
}
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
#[test]
fn proof_unavailable_uses_the_unavailable_exit_code() {
let err = CliError::ProofUnavailable;
assert_eq!(err.exit_code(), EXIT_UNAVAILABLE);
assert!(err.to_string().contains("--features verify"));
}
#[test]
fn not_implemented_yet_uses_the_unavailable_exit_code() {
let err = CliError::NotImplementedYet {
verb: "bag info",
wave: crate::command::stub::WAVE_BAG,
};
assert_eq!(err.exit_code(), EXIT_UNAVAILABLE);
assert!(err.to_string().contains("bag info"));
assert!(err.to_string().contains("rosbag"));
}
#[test]
fn feature_not_enabled_uses_the_unavailable_exit_code() {
let err = CliError::FeatureNotEnabled {
feature: "verify",
wave: "W5/W6",
};
assert_eq!(err.exit_code(), EXIT_UNAVAILABLE);
assert!(err.to_string().contains("verify"));
}
#[test]
fn ordinary_errors_use_exit_code_one() {
let err = CliError::UnsafePath {
path: "/tmp/x".to_string(),
};
assert_eq!(err.exit_code(), 1);
}
#[test]
fn io_helper_carries_the_display_path() {
let err = CliError::io(
"/tmp/nope",
std::io::Error::new(std::io::ErrorKind::NotFound, "nope"),
);
assert!(err.to_string().contains("/tmp/nope"));
}
#[test]
fn manifest_error_converts_via_from() {
let source = astrs_manifest::Manifest::from_yaml_str("not: [a, manifest").unwrap_err();
let err: CliError = source.into();
assert!(matches!(err, CliError::Manifest(_)));
}
}