#[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 _},
};
#[cfg(feature = "std")]
#[derive(Debug)]
enum Err {
Io(Error),
#[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() {}
#[cfg(feature = "std")]
fn main() -> Result<(), Err> {
#[cfg(target_os = "openbsd")]
fn privsep() -> Result<(), Err> {
Promises::new([Promise::Stdio])
.pledge()
.map_err(Err::Pledge)
}
#[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)
})
})
}