proxit 1.0.1

UNIX-style error messages in Rust
Documentation
//! For documentation on why this crate exists and how to use it, read the
//! [README][1] contained in the root of this crates git repository.
//!
//! [1]: https://git.thomasvoss.com/proxit/about

use std::{
	env,
	fmt::Display,
	process::{ExitCode, Termination},
};

/// A thin wrapper around a `Result` upon which the `Termination` trait is
/// implemented.  This is the type you’re going to want your `main()` function to
/// return.  The generic arguments provided to `MainResult` should match up with
/// those of the inner `Result` enum.
///
/// # Examples
///
/// ```
/// use proxit::MainResult;
///
/// fn main() -> MainResult<(), Error> {
/// 	work().into()
/// }
///
/// fn work() -> Result<(), Error> {
/// 	/* Your code here */
///
/// 	Ok(())
/// }
/// ```
pub struct MainResult<T, E>
where
	T: Termination,
	E: Display,
{
	underlying: Result<T, E>,
}

impl<T, E> Termination for MainResult<T, E>
where
	T: Termination,
	E: Display,
{
	fn report(self) -> ExitCode {
		match self.underlying {
			Ok(x) => x.report(),
			Err(e) => {
				let argv0 = env::args().next().unwrap_or("Error".into());
				eprint!("{argv0}: {e}");
				ExitCode::FAILURE
			}
		}
	}
}

impl<T, E> From<Result<T, E>> for MainResult<T, E>
where
	T: Termination,
	E: Display,
{
	fn from(value: Result<T, E>) -> Self {
		MainResult { underlying: value }
	}
}