derive-termination

Crates.ioderive-termination
lib.rsderive-termination
version1.0.5
created_at2025-11-13 21:25:53.971771+00
updated_at2025-11-13 22:41:29.525457+00
descriptionDerive the std::process::Termination trait for an enum (annotate the variants with `#[exit_code(n)`).
homepage
repositoryhttps://github.com/barafael/derive-termination
max_upload_size
id1931878
size7,050
Rafael Bachmann (barafael)

documentation

README

derive-termination

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.

Commit count: 0

cargo fmt