Skip to content

Commit

Permalink
move ArgError to types
Browse files Browse the repository at this point in the history
  • Loading branch information
flrgh committed Dec 20, 2023
1 parent 19d56c6 commit ed534c9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 63 deletions.
62 changes: 0 additions & 62 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::process::Command;
use thiserror::Error as ThisError;

const VERSION: &str = "0.1.0";

Expand Down Expand Up @@ -159,67 +158,6 @@ fn main_conf(user: &mut UserArgs) -> Vec<String> {
conf
}

#[derive(ThisError, Debug)]
pub enum ArgError {
#[error("ERROR: could not find {0} include file '{1}'")]
MissingInclude(String, String),

#[error("ERROR: options {0} and {1} cannot be specified at the same time.")]
Conflict(String, String),

#[error("ERROR: Invalid {arg} option value: {value}\n ({err})")]
InvalidValue {
arg: String,
value: String,
err: String,
},

#[error("unknown argument: `{0}`")]
UnknownArgument(String),

#[error("option {0} takes an argument but found none.")]
MissingValue(String),

#[error("Neither Lua input file nor -e \"\" option specified.")]
NoLuaInput,

#[error("duplicate {0} options")]
Duplicate(String),

#[error("Lua input file {0} not found.")]
LuaFileNotFound(String),
}

impl ArgError {
pub fn exit_code(&self) -> i32 {
match self {
// I/O error
Self::MissingInclude(_, _) => 2,

// yup, resty-cli returns 25 (ENOTTY) for mutually-exclusive
// arguments
//
// not on purpose though, it's just a side effect of errno
// having been set from a previous and unrelated error
Self::Conflict(_, _) => 25,

Self::UnknownArgument(_) => 1,

Self::InvalidValue {
arg: _,
value: _,
err: _,
} => 255,
Self::MissingValue(_) => 255,

Self::NoLuaInput => 2,
Self::LuaFileNotFound(_) => 2,

Self::Duplicate(_) => 255,
}
}
}

pub trait CliOpt {
fn get_arg(&self, optarg: &mut Option<String>) -> Result<String, ArgError>;

Expand Down
2 changes: 1 addition & 1 deletion src/nginx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::ArgError;
use crate::types::ArgError;
use crate::util::*;
use std::env;
use std::io;
Expand Down
62 changes: 62 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::io;
use std::net;
use std::path::PathBuf;
use std::string::ToString;
use thiserror::Error as ThisError;

const MKDTEMP_TEMPLATE: &str = "/tmp/resty_XXXXXX";

Expand Down Expand Up @@ -310,3 +311,64 @@ impl Buf {
self.indent -= 1
}
}

#[derive(ThisError, Debug)]
pub enum ArgError {
#[error("ERROR: could not find {0} include file '{1}'")]
MissingInclude(String, String),

#[error("ERROR: options {0} and {1} cannot be specified at the same time.")]
Conflict(String, String),

#[error("ERROR: Invalid {arg} option value: {value}\n ({err})")]
InvalidValue {
arg: String,
value: String,
err: String,
},

#[error("unknown argument: `{0}`")]
UnknownArgument(String),

#[error("option {0} takes an argument but found none.")]
MissingValue(String),

#[error("Neither Lua input file nor -e \"\" option specified.")]
NoLuaInput,

#[error("duplicate {0} options")]
Duplicate(String),

#[error("Lua input file {0} not found.")]
LuaFileNotFound(String),
}

impl ArgError {
pub fn exit_code(&self) -> i32 {
match self {
// I/O error
Self::MissingInclude(_, _) => 2,

// yup, resty-cli returns 25 (ENOTTY) for mutually-exclusive
// arguments
//
// not on purpose though, it's just a side effect of errno
// having been set from a previous and unrelated error
Self::Conflict(_, _) => 25,

Self::UnknownArgument(_) => 1,

Self::InvalidValue {
arg: _,
value: _,
err: _,
} => 255,
Self::MissingValue(_) => 255,

Self::NoLuaInput => 2,
Self::LuaFileNotFound(_) => 2,

Self::Duplicate(_) => 255,
}
}
}

0 comments on commit ed534c9

Please sign in to comment.