Skip to main content

Exit

Struct Exit 

Source
pub struct Exit { /* private fields */ }
Expand description

The exit code and the text with which a command ends the process

A wrapper, such as a script or a CI job, reads the exit code of a program to decide what to do next. A command therefore needs to choose the code. The chosen code is not always a failure: a check that finds a problem ends as planned, and it can still exit with a code other than 0 to tell the wrapper what it found.

The text is what the command wants the user to read last, such as a diagnostic that the application rendered itself. Clawless does not render the text. It writes the text to the standard error once, exactly as it is, and adds no prefix and no line break. End the text with a line break unless the text must not have one.

Clawless writes the text after it has rendered all the output that the command sent, and the process exits after the text. The text therefore appears last on the standard error. The standard output carries only the result of the command, so JSON output stays valid. The verbosity does not apply to the text, so --quiet does not hide it.

An error that a command returns always ends the process with the exit code 1. A command that needs another code for a failure turns the error into an Exit itself, and returns the Exit in Ok.

Clawless itself uses these exit codes. A command can still use them, because Clawless reserves none of them.

CodeMeaning
0The command returned ()
1The command returned an error
2The command line has an error, which the argument parser reports
101The command or Clawless panicked, for example on a closed standard output
130The user pressed Ctrl+C for the second time

§Examples

use std::process::ExitCode;

use clawless_core::exit::Exit;

let exit = Exit::builder()
    .code(ExitCode::from(3))
    .text("error: 0.7.0 is not a stable release\n")
    .build();

assert_eq!(exit.code(), ExitCode::from(3));
assert_eq!(exit.text(), Some("error: 0.7.0 is not a stable release\n"));

A command that only chooses the code converts an ExitCode into an Exit without text:

use std::process::ExitCode;

use clawless_core::exit::Exit;

let exit = Exit::from(ExitCode::from(3));

assert_eq!(exit.text(), None);

Implementations§

Source§

impl Exit

Source

pub fn builder() -> ExitBuilder

Create an instance of Exit using the builder syntax

Source§

impl Exit

Source

pub fn code(&self) -> ExitCode

The exit code of the process

Source§

impl Exit

Source

pub fn from_error(error: &impl Debug) -> Exit

Returns the exit of a command that failed with the given error

The exit code is 1. The text is Error: and the debug representation of the error, the same as when the main function of a Rust program returns the error.

§Examples
use std::process::ExitCode;

use clawless_core::exit::Exit;

#[derive(Debug)]
struct MissingFile;

let exit = Exit::from_error(&MissingFile);

assert_eq!(exit.code(), ExitCode::FAILURE);
assert_eq!(exit.text(), Some("Error: MissingFile\n"));
Source

pub fn text(&self) -> Option<&str>

Returns the text that the process writes to the standard error before it exits

§Examples
use std::process::ExitCode;

use clawless_core::exit::Exit;

let exit = Exit::builder()
    .code(ExitCode::from(3))
    .text("error: the check failed\n")
    .build();

assert_eq!(exit.text(), Some("error: the check failed\n"));

Trait Implementations§

Source§

impl Clone for Exit

Source§

fn clone(&self) -> Exit

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Exit

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<()> for Exit

Returns the exit of a command that ended without a choice of its own

The exit code is 0, and the process writes no text.

Source§

fn from(_: ()) -> Exit

Converts to this type from the input type.
Source§

impl From<Exit> for ProcessExit

Returns the process exit that ends the process with the given exit

Source§

fn from(exit: Exit) -> Self

Converts to this type from the input type.
Source§

impl From<ExitCode> for Exit

Returns the exit with the given code and without text

Source§

fn from(code: ExitCode) -> Exit

Converts to this type from the input type.
Source§

impl PartialEq for Exit

Source§

fn eq(&self, other: &Exit) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Exit

Auto Trait Implementations§

§

impl Freeze for Exit

§

impl RefUnwindSafe for Exit

§

impl Send for Exit

§

impl Sync for Exit

§

impl Unpin for Exit

§

impl UnsafeUnpin for Exit

§

impl UnwindSafe for Exit

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.