From ed534c92736965dc964d9296889f742f089782ba Mon Sep 17 00:00:00 2001 From: Michael Martin Date: Tue, 19 Dec 2023 17:52:39 -0800 Subject: [PATCH] move ArgError to types --- src/cli.rs | 62 ---------------------------------------------------- src/nginx.rs | 2 +- src/types.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index c8248ea..537f661 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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"; @@ -159,67 +158,6 @@ fn main_conf(user: &mut UserArgs) -> Vec { 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) -> Result; diff --git a/src/nginx.rs b/src/nginx.rs index 9789b94..9af9184 100644 --- a/src/nginx.rs +++ b/src/nginx.rs @@ -1,4 +1,4 @@ -use crate::cli::ArgError; +use crate::types::ArgError; use crate::util::*; use std::env; use std::io; diff --git a/src/types.rs b/src/types.rs index 2376ba7..88047ab 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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"; @@ -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, + } + } +}