| Crates.io | derive-termination |
| lib.rs | derive-termination |
| version | 1.0.5 |
| created_at | 2025-11-13 21:25:53.971771+00 |
| updated_at | 2025-11-13 22:41:29.525457+00 |
| description | Derive the std::process::Termination trait for an enum (annotate the variants with `#[exit_code(n)`). |
| homepage | |
| repository | https://github.com/barafael/derive-termination |
| max_upload_size | |
| id | 1931878 |
| size | 7,050 |
Derive the std::process::Termination trait for an enum (annotate the variants with #[exit_code(n)).
use std::process::{ExitCode, Termination};
use derive_termination::Termination;
#[derive(Termination)]
pub enum Error {
#[exit_code(3)]
Fatal(bool, u8),
#[exit_code(4)]
Whatever { name: String },
#[exit_code(5)]
Anyhow,
}
#[test]
fn should_report_3() {
assert_eq!(Error::Fatal(true, 4).report(), ExitCode::from(3));
}
The Termination derive macro above would generate:
pub enum Error {
Fatal(bool, u8),
Whatever { name: String },
Anyhow,
}
impl ::std::process::Termination for Error {
fn report(self) -> ::std::process::ExitCode {
match self {
Self::Fatal(..) => ::std::process::ExitCode::from(3),
Self::Whatever { .. } => ::std::process::ExitCode::from(4),
Self::Anyhow => ::std::process::ExitCode::from(5),
}
}
}
The std::process::Termination trait is a trait marking any type which is allowed to be returned from main.