Skip to content

Commit

Permalink
wrap error with crate thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkLoc committed May 15, 2024
1 parent 2e6098f commit 4ec300f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 20 deletions.
31 changes: 26 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xtab"
version = "0.0.6"
version = "0.0.7"
authors = ["sharkLoc <[email protected]>"]
edition = "2021"
homepage = "https://github.com/sharkLoc/xtab"
Expand Down Expand Up @@ -29,7 +29,8 @@ rand = "0.8.5"
rand_pcg = "0.3.1"
regex = "1.10.4"
rust_xlsxwriter = "0.64.2"
thiserror = "1.0.60"
xz2 = "0.1.7"

[profile.release]
strip = true
strip = true
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cargo b --release

```bash
xtab -- CSV command line utilities
Version: 0.0.6
Version: 0.0.7

Authors: sharkLoc <[email protected]>
Source code: https://github.com/sharkLoc/xtab.git
Expand Down Expand Up @@ -59,9 +59,6 @@ Commands:
view Show CSV file content
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help (see more with '--help')

Global Arguments:
-d, --delimiter <CHAR> Set delimiter for input csv file, e.g., in linux -d $'\t' for tab, in powershell -d `t for tab [default: ,]
-D, --out-delimite <CHAR> Set delimiter for output CSV file, e.g., in linux -D $'\t' for tab, in powershell -D `t for tab [default: ,]
Expand All @@ -73,6 +70,8 @@ Global Arguments:
Global FLAGS:
-H, --no-header If set, the first row is treated as a special header row, and the original header row excluded from output
-q, --quiet Be quiet and do not show any extra information
-h, --help prints help information
-V, --version prints version information
Use "xtab help [command]" for more information about a command
```
16 changes: 12 additions & 4 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::path::PathBuf;
use clap::{value_parser, Parser};
use clap::{ArgAction, value_parser, Parser};


#[derive(Debug, Parser)]
#[command(
name = "xtab",
author = "sharkLoc",
version = "0.0.6",
version = "0.0.7",
next_line_help = false,
about = "CSV command line utilities",
long_about = "A simple and cross-platform program for CSV file manipulation"
)]
#[command(propagate_version = false, disable_help_flag = false, disable_version_flag = true)]
#[command(propagate_version = true, disable_help_flag = true, disable_version_flag = true)]
#[command(before_help=r"xtab supports reading and writing gzip/bzip2/xz format file.
Compression level:
format range default crate
Expand Down Expand Up @@ -62,6 +62,14 @@ pub struct Args {
/// Be quiet and do not show any extra information
#[arg(short = 'q', long = "quiet", global= true, help_heading = Some("Global FLAGS"))]
pub quiet: bool,

/// prints help information
#[arg(short = 'h', long, action = ArgAction::Help, global= true, help_heading = Some("Global FLAGS"))]
pub help: Option<String>,

/// prints version information
#[arg(short = 'V', long, action = ArgAction::Version, global= true, help_heading = Some("Global FLAGS"))]
pub version: Option<String>,
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -300,4 +308,4 @@ pub enum Cmd {
#[arg(short = 'o', long = "out", value_name = "FILE")]
output: Option<PathBuf>,
},
}
}
11 changes: 11 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use thiserror::Error;
use std::io;

#[derive(Debug, Error)]
pub enum Xerror {
#[error("stdin not detected")]
StdinNotDetected,

#[error("failed to open file: {0}")]
IoError(#[from] io::Error),
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod loger;
mod args;
mod command;
mod utils;
mod error;

use command::{
addheader::addheader_csv, csv2xlsx::csv_xlsx, dim::dim_csv, drop::drop_csv, flatten::flatten_csv, freq::freq_csv, head::head_csv, pretty::pretty_csv, replace::replace_csv, reverse::reverse_csv, sample::sample_csv, search::search_csv, slice::slice_csv, tail::tail_csv, transpose::transpose_csv, uniq::uniq_csv, view::view_csv, xlsx2csv::xlsx_csv
Expand Down
17 changes: 12 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::{
path::Path,
};

use crate::error::Xerror;


const GZ_MAGIC: [u8; 3] = [0x1f, 0x8b, 0x08];
const BZ_MAGIC: [u8; 3] = [0x42, 0x5a, 0x68];
Expand All @@ -20,7 +22,9 @@ where
P: AsRef<Path> + Copy,
{
let mut buffer: [u8; MAGIC_MAX_LEN] = [0; MAGIC_MAX_LEN];
let mut fp = File::open(file_name)?;
let mut fp = File::open(file_name)
.map_err(Xerror::IoError)?;

let _ = fp.read(&mut buffer)?;
Ok(buffer)
}
Expand Down Expand Up @@ -68,7 +72,8 @@ where
let bz_flag = is_bzipped(file_name)?;
let zx_flag = is_xz(file_name)?;

let fp = File::open(file_name)?;
let fp = File::open(file_name)
.map_err(Xerror::IoError)?;

if gz_flag {
Ok(Box::new(BufReader::with_capacity(
Expand All @@ -90,7 +95,7 @@ where
}
} else {
if atty::is(atty::Stream::Stdin) {
error!("stdin not detected");
error!("{}", Xerror::StdinNotDetected);
std::process::exit(1);
}
let fp = BufReader::new(io::stdin());
Expand All @@ -103,7 +108,8 @@ where
P: AsRef<Path> + Copy,
{
if let Some(file_name) = file_out {
let fp = File::create(file_name)?;
let fp = File::create(file_name)
.map_err(Xerror::IoError)?;

if file_name.as_ref().ends_with(".gz") {
Ok(Box::new(BufWriter::with_capacity(
Expand Down Expand Up @@ -136,7 +142,8 @@ where
let fp = OpenOptions::new()
.append(true)
.create(true)
.open(file_out)?;
.open(file_out)
.map_err(Xerror::IoError)?;

if file_out.as_ref().ends_with(".gz") {
Ok(Box::new(BufWriter::with_capacity(
Expand Down

0 comments on commit 4ec300f

Please sign in to comment.