Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sorairolake committed Nov 24, 2023
1 parent 7c5557d commit e776d03
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
18 changes: 9 additions & 9 deletions examples/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ use std::io::Read;

#[cfg(feature = "std")]
fn main() -> std::process::ExitCode {
let args: Vec<_> = std::env::args_os().skip(1).collect();
use std::{env, fs, io, process::ExitCode};

let contents: std::io::Result<Vec<_>> = if args.is_empty() {
let args: Vec<_> = env::args_os().skip(1).collect();

let contents: io::Result<Vec<_>> = if args.is_empty() {
let mut buf = String::new();
vec![std::io::stdin().read_to_string(&mut buf).map(|_| buf)]
vec![io::stdin().read_to_string(&mut buf).map(|_| buf)]
.into_iter()
.collect()
} else {
args.into_iter().map(std::fs::read_to_string).collect()
args.into_iter().map(fs::read_to_string).collect()
};
let contents = match contents {
Ok(strings) => strings,
Err(err) => {
eprintln!("Error: {err}");
return sysexits::ExitCode::try_from(err.kind()).map_or(
std::process::ExitCode::FAILURE,
std::process::ExitCode::from,
);
return sysexits::ExitCode::try_from(err.kind())
.map_or(ExitCode::FAILURE, ExitCode::from);
}
};

for output in contents {
print!("{output}");
}
std::process::ExitCode::SUCCESS
ExitCode::SUCCESS
}

#[cfg(not(feature = "std"))]
Expand Down
18 changes: 11 additions & 7 deletions examples/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,30 @@ impl From<sysexits::ExitCode> for ExitCode {
#[cfg(feature = "std")]
impl Termination for ExitCode {
fn report(self) -> std::process::ExitCode {
use std::process::ExitCode;

match self {
Self::Same => std::process::ExitCode::from(u8::MIN),
Self::Different => std::process::ExitCode::from(1),
Self::Trouble => std::process::ExitCode::from(2),
Self::Other(code) => std::process::ExitCode::from(code),
Self::Same => ExitCode::from(u8::MIN),
Self::Different => ExitCode::from(1),
Self::Trouble => ExitCode::from(2),
Self::Other(code) => ExitCode::from(code),
}
}
}

#[cfg(feature = "std")]
fn main() -> ExitCode {
let args: Vec<_> = std::env::args_os().skip(1).take(2).collect();
use std::{env, fs, io, path::PathBuf};

let args: Vec<_> = env::args_os().skip(1).take(2).collect();

let files = if let (Some(from), Some(to)) = (args.get(0), args.get(1)) {
(std::path::PathBuf::from(from), std::path::PathBuf::from(to))
(PathBuf::from(from), PathBuf::from(to))
} else {
eprintln!("Error: files are missing");
return ExitCode::Trouble;
};
let contents: std::io::Result<Vec<_>> = args.into_iter().map(std::fs::read).collect();
let contents: io::Result<Vec<_>> = args.into_iter().map(fs::read).collect();
let contents = match contents {
Ok(bytes) => bytes,
Err(err) => {
Expand Down
16 changes: 10 additions & 6 deletions examples/isutf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@ use std::io::Read;

#[cfg(feature = "std")]
fn main() -> sysexits::ExitCode {
let input = std::env::args_os()
use std::{env, fs, io, str};

use sysexits::ExitCode;

let input = env::args_os()
.nth(1)
.map_or_else(
|| {
let mut buf = Vec::new();
std::io::stdin().read_to_end(&mut buf).map(|_| buf)
io::stdin().read_to_end(&mut buf).map(|_| buf)
},
std::fs::read,
fs::read,
)
.unwrap_or_else(|err| panic!("{err}"));
if let Err(err) = std::str::from_utf8(&input) {
if let Err(err) = str::from_utf8(&input) {
eprintln!("Error: {err}");
sysexits::ExitCode::DataErr
ExitCode::DataErr
} else {
println!("OK");
sysexits::ExitCode::Ok
ExitCode::Ok
}
}

Expand Down

0 comments on commit e776d03

Please sign in to comment.