calc_rational 3.0.0

CLI calculator for rational numbers.
Documentation
//! # `calc`
//!
//! Consult [`README.md`](https://crates.io/crates/calc_rational).
#[cfg(not(feature = "std"))]
use calc_lib as _;
#[cfg(feature = "std")]
use calc_lib::{EvalIter, lending_iterator::LendingIterator as _};
#[cfg(feature = "std")]
use core::fmt::{self, Display, Formatter};
use num_bigint as _;
use num_integer as _;
use num_rational as _;
use num_traits as _;
#[cfg(target_os = "openbsd")]
use priv_sep::{Errno, Promise, Promises};
#[cfg(feature = "rand")]
use rand as _;
#[cfg(feature = "std")]
use std::{
    error,
    io::{self, Error, Write as _},
};
/// Error returned by [`main`].
#[cfg(feature = "std")]
#[derive(Debug)]
enum Err {
    /// Error returned from [`writeln`].
    Io(Error),
    /// Error returned from `pledge`.
    #[cfg(target_os = "openbsd")]
    Pledge(Errno),
}
#[cfg(feature = "std")]
impl Display for Err {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match *self {
            Self::Io(ref e) => e.fmt(f),
            #[cfg(target_os = "openbsd")]
            Self::Pledge(ref e) => write!(f, "pledge(2)ing 'stdio' failed with {e}"),
        }
    }
}
#[cfg(feature = "std")]
impl error::Error for Err {}
#[cfg(not(feature = "std"))]
fn main() {}
/// Entry point to the calc program.
///
/// # Errors
///
/// Returns [`Error`] if[`writeln`] returns one when writing
/// to the global standard output stream. Returns [`Error`] if
/// [`pledge`](https://docs.rs/priv_sep/latest/priv_sep/fn.pledge.html)
/// does when compiled with the `priv_sep` feature which
/// currently only works on OpenBSD-stable.
#[cfg(feature = "std")]
fn main() -> Result<(), Err> {
    /// Calls `pledge(2)` with the "stdio" promise.
    #[cfg(target_os = "openbsd")]
    fn privsep() -> Result<(), Err> {
        Promises::new([Promise::Stdio])
            .pledge()
            .map_err(Err::Pledge)
    }
    /// Returns Ok.
    #[expect(
        clippy::unnecessary_wraps,
        reason = "consistent return type with priv_sep feature"
    )]
    #[cfg(not(target_os = "openbsd"))]
    const fn privsep() -> Result<(), Err> {
        Ok(())
    }
    privsep().and_then(|()| {
        let mut out = io::stdout().lock();
        EvalIter::new(io::stdin().lock()).lend_try_fold((), |(), res| {
            match res {
                Ok(o) => writeln!(&mut out, "{o}"),
                Err(e) => writeln!(&mut out, "{e}"),
            }
            .map_err(Err::Io)
        })
    })
}