use anyhow for error handling

fix argument bug
This commit is contained in:
LordMZTE 2020-09-13 12:27:56 +02:00
parent f91ea5a845
commit 9b50328940
3 changed files with 22 additions and 16 deletions

View File

@ -15,3 +15,4 @@ async-minecraft-ping = "0.2.1"
tokio = { version = "0.2.22", features = ["full"] }
itertools = "0.9.0"
termcolor = "1"
anyhow = "1.0.32"

View File

@ -19,28 +19,33 @@ args:
takes_value: true
# IMAGE ARGS
# TODO due to a bug in clap, the image argument is always required because size has a default value
- image:
short: i
short: "i"
help: "if the server's favicon should be printed as ASCII art"
required: false
- color:
short: c
short: "c"
help: "if the favicon image should be printed with ANSI color formatting or monochrome"
- size:
short: s
short: "s"
help: "the size of the image"
takes_value: true
default_value: "16"
- deep:
short: d
short: "d"
help: "if provided the ascii image will have more different characters"
- invert:
short: n
short: "n"
help: "inverts the ascii image thickness"
groups:
- img-flags:
requires: "image"
# TODO uncomment unce bug is fixed
# requires:
# - image
multiple: true
required: false
args:
- color
- size

View File

@ -1,9 +1,9 @@
#[macro_use]
extern crate clap;
use std::error::Error;
use std::io::{Cursor, Write};
use anyhow::{Context, Result};
use asciify::AsciiBuilder;
use async_minecraft_ping::ConnectionConfig;
use clap::App;
@ -12,7 +12,7 @@ use itertools::Itertools;
use termcolor::{Buffer, BufferWriter, ColorChoice, WriteColor};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
async fn main() -> Result<()> {
let yaml = load_yaml!("args.yml");
let matches = App::from_yaml(yaml).get_matches();
@ -25,14 +25,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
.parse()
.ok()
.and_then(|p| if p > 0 && p < u16::MAX { Some(p) } else { None })
.expect("invalid port"),
.context("invalid port")?,
)
.with_protocol_version(
matches
.value_of("protocol-version")
.unwrap()
.parse()
.expect("invalid protocol version"),
.context("invalid protocol version")?,
);
let mut connection = config.connect().await?;
let response = connection.status().await?;
@ -43,7 +43,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.value_of("size")
.unwrap()
.parse()
.expect("image size must be number");
.with_context(|| "image size must be number")?;
let mut image = None;
if let (Some(favicon), true) = (response.favicon, matches.is_present("image")) {
//The image parsing and asciifying is done while the table is printing
@ -108,7 +108,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let stdout = std::io::stdout();
let mut handle = stdout.lock();
handle.write_all(&[b'\n'])?;
handle.write_all(&img.await?)?;
handle.write_all(&img.await??)?;
}
//endregion
Ok(())
@ -129,10 +129,10 @@ fn remove_formatting(s: &str) -> String {
}
/// returns the asciifyed image as UTF-8 bytes
async fn get_image(favicon: String, config: AsciiConfig) -> Vec<u8> {
async fn get_image(favicon: String, config: AsciiConfig) -> Result<Vec<u8>> {
let img = image_base64::from_base64(favicon);
let image =
image::load(Cursor::new(img), ImageFormat::Png).expect("favicon has invalid format");
image::load(Cursor::new(img), ImageFormat::Png).context("favicon has invalid format")?;
let builder = config.apply(AsciiBuilder::new_from_image(image));
@ -146,8 +146,8 @@ async fn get_image(favicon: String, config: AsciiConfig) -> Vec<u8> {
builder.to_stream(&mut buf);
buf
};
buf.reset().unwrap();
buf.as_slice().to_vec()
buf.reset()?;
Ok(buf.as_slice().to_vec())
}
struct AsciiConfig {