Skip to content

Commit

Permalink
Release version 0.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
sorairolake committed Sep 18, 2022
2 parents f0c5bb7 + ebd730a commit be104e0
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 48 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ project adheres to https://semver.org/[Semantic Versioning].

toc::[]

== {compare-url}/v0.3.1\...v0.3.2[0.3.2] - 2022-09-18

=== Added

* Add lint attributes to examples
* Add the example that returns original exit code

== {compare-url}/v0.3.0\...v0.3.1[0.3.1] - 2022-09-06

=== Removed
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ cff-version: 1.2.0
message: Please cite this software using these meta data.

# Version information.
date-released: 2022-09-06
version: 0.3.1
date-released: 2022-09-18
version: 0.3.2

# Project information.
abstract: The system exit codes as defined by <sysexits.h> for Rust
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sysexits"
version = "0.3.1"
version = "0.3.2"
authors = ["Shun Sakai <sorairolake@protonmail.ch>"]
edition = "2021"
rust-version = "1.61.0"
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,21 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
sysexits = "0.3.1"
sysexits = "0.3.2"
```

### Example

```rust
fn main() -> sysexits::ExitCode {
let bytes = [0xf0, 0x9f, 0x92, 0x96];

match std::str::from_utf8(&bytes) {
Ok(string) => {
println!("{string}");

sysexits::ExitCode::Ok
}
Err(err) => {
eprintln!("{err}");

sysexits::ExitCode::DataErr
}
}
Expand Down
3 changes: 3 additions & 0 deletions examples/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ link:isutf8.rs[]::

link:cat.rs[]::
An example that returns `std::process::ExitCode`.

link:cmp.rs[]::
An example that returns original exit code.
47 changes: 23 additions & 24 deletions examples/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,41 @@
//! An example of concatenating files and print on the standard output.
//! The contents of the file must be valid UTF-8.

// Lint levels of rustc.
#![forbid(unsafe_code)]
#![deny(missing_debug_implementations)]
#![warn(rust_2018_idioms)]
// Lint levels of Clippy.
#![warn(clippy::cargo, clippy::nursery, clippy::pedantic)]

use std::io::Read;

fn main() -> std::process::ExitCode {
let args: Vec<_> = std::env::args().skip(1).collect();
let args: Vec<_> = std::env::args_os().skip(1).collect();

let contents = if args.is_empty() {
let contents: std::io::Result<Vec<_>> = if args.is_empty() {
let mut buf = String::new();

if let Err(err) = std::io::stdin().read_to_string(&mut buf) {
eprintln!("{err}");

return sysexits::ExitCode::DataErr.into();
} else {
vec![buf]
}
vec![std::io::stdin().read_to_string(&mut buf).map(|_| buf)]
.into_iter()
.collect()
} else {
match args.into_iter().map(std::fs::read_to_string).collect() {
Ok(strings) => strings,
Err(err) => {
eprintln!("{err}");

match err.kind() {
std::io::ErrorKind::NotFound => return sysexits::ExitCode::NoInput.into(),
std::io::ErrorKind::PermissionDenied => {
return sysexits::ExitCode::NoPerm.into()
}
std::io::ErrorKind::InvalidData => return sysexits::ExitCode::DataErr.into(),
_ => return std::process::ExitCode::FAILURE,
}
args.into_iter().map(std::fs::read_to_string).collect()
};
let contents = match contents {
Ok(strings) => strings,
Err(err) => {
eprintln!("Error: {err}");
match err.kind() {
std::io::ErrorKind::NotFound => return sysexits::ExitCode::NoInput.into(),
std::io::ErrorKind::PermissionDenied => return sysexits::ExitCode::NoPerm.into(),
std::io::ErrorKind::InvalidData => return sysexits::ExitCode::DataErr.into(),
_ => return std::process::ExitCode::FAILURE,
}
}
};

for output in contents {
print!("{output}");
}

std::process::ExitCode::SUCCESS
}
80 changes: 80 additions & 0 deletions examples/cmp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
//
// Copyright (C) 2022 Shun Sakai and Contributors
//

//! An example of comparing two files byte by byte.

// Lint levels of rustc.
#![forbid(unsafe_code)]
#![deny(missing_debug_implementations)]
#![warn(rust_2018_idioms)]
// Lint levels of Clippy.
#![warn(clippy::cargo, clippy::nursery, clippy::pedantic)]

use std::process::Termination;

enum ExitCode {
Same,
Different,
Trouble,
Other(sysexits::ExitCode),
}

impl From<sysexits::ExitCode> for ExitCode {
fn from(code: sysexits::ExitCode) -> Self {
Self::Other(code)
}
}

impl Termination for ExitCode {
fn report(self) -> 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),
}
}
}

fn main() -> ExitCode {
let args: Vec<_> = std::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))
} 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 = match contents {
Ok(bytes) => bytes,
Err(err) => {
eprintln!("Error: {err}");
match err.kind() {
std::io::ErrorKind::NotFound => return sysexits::ExitCode::NoInput.into(),
std::io::ErrorKind::PermissionDenied => return sysexits::ExitCode::NoPerm.into(),
std::io::ErrorKind::InvalidData => return sysexits::ExitCode::DataErr.into(),
_ => return ExitCode::Trouble,
}
}
};

if contents[0] == contents[1] {
println!(
"Files {} and {} are identical",
files.0.display(),
files.1.display()
);
ExitCode::Same
} else {
println!(
"Files {} and {} are different",
files.0.display(),
files.1.display()
);
ExitCode::Different
}
}
33 changes: 23 additions & 10 deletions examples/isutf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,31 @@
//! An example of checking whether the input is valid UTF-8.
//! The input is a file or the standard input.

// Lint levels of rustc.
#![forbid(unsafe_code)]
#![deny(missing_debug_implementations)]
#![warn(rust_2018_idioms)]
// Lint levels of Clippy.
#![warn(clippy::cargo, clippy::nursery, clippy::pedantic)]

use std::io::Read;

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

std::io::stdin().read_to_end(&mut buf).unwrap();

buf
};

std::str::from_utf8(&input).map_or(sysexits::ExitCode::DataErr, |_| sysexits::ExitCode::Ok)
println!("OK");
sysexits::ExitCode::Ok
}
}
8 changes: 1 addition & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@
//! ```
//! fn main() -> sysexits::ExitCode {
//! let bytes = [0xf0, 0x9f, 0x92, 0x96];
//!
//! match std::str::from_utf8(&bytes) {
//! Ok(string) => {
//! println!("{string}");
//!
//! sysexits::ExitCode::Ok
//! }
//! Err(err) => {
//! eprintln!("{err}");
//!
//! sysexits::ExitCode::DataErr
//! }
//! }
Expand All @@ -48,26 +45,23 @@
//!
//! fn main() -> std::process::ExitCode {
//! let mut buf = String::new();
//!
//! if let Err(err) = std::io::stdin().read_to_string(&mut buf) {
//! eprintln!("{err}");
//!
//! if let std::io::ErrorKind::InvalidData = err.kind() {
//! sysexits::ExitCode::DataErr.into()
//! } else {
//! std::process::ExitCode::FAILURE
//! }
//! } else {
//! print!("{buf}");
//!
//! std::process::ExitCode::SUCCESS
//! }
//! }
//! ```
//!
//! [sysexits-man-url]: https://man.openbsd.org/sysexits

#![doc(html_root_url = "https://docs.rs/sysexits/0.3.1/")]
#![doc(html_root_url = "https://docs.rs/sysexits/0.3.2/")]
// Lint levels of rustc.
#![forbid(unsafe_code)]
#![deny(missing_debug_implementations, missing_docs)]
Expand Down

0 comments on commit be104e0

Please sign in to comment.