use std::borrow::Cow;
use std::io;
pub mod grep;
pub mod cat;
pub mod netcat;
pub type Result<T> = std::result::Result<T,Error>;
pub struct Error {
exit_code: i32,
msg: Cow<'static, str>,
}
impl Error {
pub fn new(msg: impl Into<Cow<'static,str>>) -> Self {
Self {
exit_code: 1,
msg: msg.into()
}
}
pub fn with_exit_code(mut self, code: i32) -> Self {
self.exit_code = code;
self
}
pub fn msg(&self) -> &str {
&self.msg
}
pub fn exit_code(&self) -> i32 {
self.exit_code
}
}
impl From<io::Error> for Error {
fn from(value: io::Error) -> Self {
Self::new(value.to_string())
}
}
impl From<&'static str> for Error {
fn from(value: &'static str) -> Self {
Self::new(value)
}
}
#[macro_export]
macro_rules! coreutil {
($p:ident) => {
pub fn main() {
if let Err(err) = $crate :: $p :: run () {
eprintln!("{}", err.msg());
std::process::exit(err.exit_code());
}
}
};
}