Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate main #460

Merged
merged 3 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/clierror.rs → src/clitypes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
#![macro_use]
use std::borrow::ToOwned;
use std::fmt;
use std::io;
use std::process::{ExitCode, Termination};

macro_rules! wout {
($($arg:tt)*) => ({
use std::io::Write;
(writeln!(&mut ::std::io::stdout(), $($arg)*)).unwrap();
});
}

macro_rules! werr {
($($arg:tt)*) => ({
use std::io::Write;
use log::error;
error!("{}", $($arg)*);
(writeln!(&mut ::std::io::stderr(), $($arg)*)).unwrap();
});
}

// TODO: format parameter so we don't need to do fail!(format!())
macro_rules! fail {
($e:expr) => {{
use log::error;
let err = ::std::convert::From::from($e);
error!("{err}");
Err(err)
}};
}

#[repr(u8)]
pub enum QsvExitCode {
Good = 0,
Bad = 1,
IncorrectUsage = 2,
Abort = 255,
}

impl Termination for QsvExitCode {
fn report(self) -> ExitCode {
ExitCode::from(self as u8)
}
}

pub type CliResult<T> = Result<T, CliError>;

#[derive(Debug)]
pub enum CliError {
Expand Down
52 changes: 5 additions & 47 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@

extern crate crossbeam_channel as channel;

use crate::clierror::CliError;
use crate::clitypes::{CliError, CliResult, QsvExitCode};
use std::env;
use std::io;
use std::process::{ExitCode, Termination};
use std::time::Instant;

use docopt::Docopt;
Expand All @@ -59,32 +58,6 @@ use pyo3::Python;
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

macro_rules! wout {
($($arg:tt)*) => ({
use std::io::Write;
(writeln!(&mut ::std::io::stdout(), $($arg)*)).unwrap();
});
}

macro_rules! werr {
($($arg:tt)*) => ({
use std::io::Write;
use log::error;
error!("{}", $($arg)*);
(writeln!(&mut ::std::io::stderr(), $($arg)*)).unwrap();
});
}

// TODO: format parameter so we don't need to do fail!(format!())
macro_rules! fail {
($e:expr) => {{
use log::error;
let err = ::std::convert::From::from($e);
error!("{err}");
Err(err)
}};
}

macro_rules! command_list {
() => {
"
Expand Down Expand Up @@ -142,7 +115,7 @@ macro_rules! command_list {
"
};
}
mod clierror;
mod clitypes;
mod cmd;
mod config;
mod index;
Expand All @@ -168,20 +141,6 @@ Options:
"
);

#[repr(u8)]
pub enum QsvExitCode {
Good = 0,
Bad = 1,
IncorrectUsage = 2,
Abort = 255,
}

impl Termination for QsvExitCode {
fn report(self) -> ExitCode {
ExitCode::from(self as u8)
}
}

#[derive(Deserialize)]
struct Args {
arg_command: Option<Command>,
Expand All @@ -201,7 +160,7 @@ fn main() -> QsvExitCode {
#[cfg(feature = "python")]
if !check_python() {
werr!("Python 3.8+ required.");
return QsvExitCode::IncorrectUsage;
return QsvExitCode::Abort;
}

let now = Instant::now();
Expand Down Expand Up @@ -261,8 +220,9 @@ Please choose one of the following commands:",
QsvExitCode::Bad
}
Err(CliError::Io(ref err)) if err.kind() == io::ErrorKind::BrokenPipe => {
werr!("Broken pipe: {err}");
util::log_end(qsv_args, now);
QsvExitCode::Good
QsvExitCode::Abort
}
Err(CliError::Io(err)) => {
werr!("{err}");
Expand Down Expand Up @@ -415,5 +375,3 @@ impl Command {
}
}
}

pub type CliResult<T> = Result<T, CliError>;
49 changes: 4 additions & 45 deletions src/maindp.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#![allow(dead_code)]
extern crate crossbeam_channel as channel;

use crate::clierror::CliError;
use crate::clitypes::{CliError, CliResult, QsvExitCode};
use std::env;
use std::io;
use std::process::{ExitCode, Termination};
use std::time::Instant;

use docopt::Docopt;
Expand All @@ -15,31 +14,6 @@ use serde::Deserialize;
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

macro_rules! wout {
($($arg:tt)*) => ({
use std::io::Write;
(writeln!(&mut ::std::io::stdout(), $($arg)*)).unwrap();
});
}

macro_rules! werr {
($($arg:tt)*) => ({
use std::io::Write;
use log::error;
error!("{}", $($arg)*);
(writeln!(&mut ::std::io::stderr(), $($arg)*)).unwrap();
});
}

macro_rules! fail {
($e:expr) => {{
use log::error;
let err = ::std::convert::From::from($e);
error!("{err}");
Err(err)
}};
}

macro_rules! command_list {
() => {
"
Expand Down Expand Up @@ -70,7 +44,7 @@ macro_rules! command_list {
"
};
}
mod clierror;
mod clitypes;
mod cmd;
mod config;
mod index;
Expand All @@ -94,20 +68,6 @@ Options:
* sponsored by datHere - Data Infrastructure Engineering
"
);

pub enum QsvExitCode {
Good = 0,
Bad = 1,
IncorrectUsage = 2,
Abort = 255,
}

impl Termination for QsvExitCode {
fn report(self) -> ExitCode {
ExitCode::from(self as u8)
}
}

#[derive(Deserialize)]
struct Args {
arg_command: Option<Command>,
Expand Down Expand Up @@ -168,8 +128,9 @@ Please choose one of the following commands:",
QsvExitCode::Bad
}
Err(CliError::Io(ref err)) if err.kind() == io::ErrorKind::BrokenPipe => {
werr!("Broken pipe: {err}");
util::log_end(qsv_args, now);
QsvExitCode::Good
QsvExitCode::Abort
}
Err(CliError::Io(err)) => {
werr!("{err}");
Expand Down Expand Up @@ -257,5 +218,3 @@ impl Command {
}
}
}

pub type CliResult<T> = Result<T, CliError>;
48 changes: 4 additions & 44 deletions src/mainlite.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#![allow(dead_code)]
extern crate crossbeam_channel as channel;

use crate::clierror::CliError;
use crate::clitypes::{CliError, CliResult, QsvExitCode};
use std::env;
use std::io;
use std::process::{ExitCode, Termination};
use std::time::Instant;

use docopt::Docopt;
Expand All @@ -15,31 +14,6 @@ use serde::Deserialize;
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

macro_rules! wout {
($($arg:tt)*) => ({
use std::io::Write;
(writeln!(&mut ::std::io::stdout(), $($arg)*)).unwrap();
});
}

macro_rules! werr {
($($arg:tt)*) => ({
use std::io::Write;
use log::error;
error!("{}", $($arg)*);
(writeln!(&mut ::std::io::stderr(), $($arg)*)).unwrap();
});
}

macro_rules! fail {
($e:expr) => {{
use log::error;
let err = ::std::convert::From::from($e);
error!("{err}");
Err(err)
}};
}

macro_rules! command_list {
() => {
"
Expand Down Expand Up @@ -88,7 +62,7 @@ macro_rules! command_list {
"
};
}
mod clierror;
mod clitypes;
mod cmd;
mod config;
mod index;
Expand All @@ -114,19 +88,6 @@ Options:
"
);

pub enum QsvExitCode {
Good = 0,
Bad = 1,
IncorrectUsage = 2,
Abort = 255,
}

impl Termination for QsvExitCode {
fn report(self) -> ExitCode {
ExitCode::from(self as u8)
}
}

#[derive(Deserialize)]
struct Args {
arg_command: Option<Command>,
Expand Down Expand Up @@ -195,8 +156,9 @@ Please choose one of the following commands:",
QsvExitCode::Bad
}
Err(CliError::Io(ref err)) if err.kind() == io::ErrorKind::BrokenPipe => {
werr!("Broken pipe: {err}");
util::log_end(qsv_args, now);
QsvExitCode::Good
QsvExitCode::Abort
}
Err(CliError::Io(err)) => {
werr!("{err}");
Expand Down Expand Up @@ -321,5 +283,3 @@ impl Command {
}
}
}

pub type CliResult<T> = Result<T, CliError>;