From 1ff1e3da63dea4abdf8818ca6f1ebd91d4d7c896 Mon Sep 17 00:00:00 2001 From: Jens Getreu Date: Thu, 19 Mar 2020 22:30:25 +0100 Subject: [PATCH] replace macros with constants --- src/finding.rs | 4 ++-- src/help.rs | 4 ++-- src/mission.rs | 33 ++++++++++++++++----------------- src/options.rs | 41 ++++++++--------------------------------- 4 files changed, 28 insertions(+), 54 deletions(-) diff --git a/src/finding.rs b/src/finding.rs index 45d92d4..a932886 100644 --- a/src/finding.rs +++ b/src/finding.rs @@ -2,12 +2,12 @@ extern crate encoding_rs; -use crate::ascii_enc_label; use crate::input::ByteCounter; use crate::input::INPUT_BUF_LEN; use crate::mission::Mission; use crate::options::Radix; use crate::options::ARGS; +use crate::options::ASCII_ENC_LABEL; use std::io::Write; use std::ops::Deref; use std::str; @@ -251,7 +251,7 @@ impl<'a> Finding<'a> { // map 0 -> 'a', 1 -> 'b', 2 -> 'c' ... out.write_all(&[b'(', self.mission.mission_id + 97 as u8, b' '])?; out.write_all(if self.mission.print_encoding_as_ascii { - ascii_enc_label!().as_bytes() + ASCII_ENC_LABEL.as_bytes() } else { self.mission.encoding.name().as_bytes() })?; diff --git a/src/help.rs b/src/help.rs index 49d4d87..faf5c9d 100644 --- a/src/help.rs +++ b/src/help.rs @@ -1,10 +1,10 @@ //! Help the user with command-line-arguments. -use crate::ascii_enc_label; use crate::mission::ASCII_FILTER_ALIASSE; use crate::mission::UNICODE_BLOCK_FILTER_ALIASSE; use crate::mission::{Missions, MISSIONS}; use crate::options::ARGS; +use crate::options::ASCII_ENC_LABEL; use crate::AUTHOR; use crate::VERSION; use std::process; @@ -52,7 +52,7 @@ pub fn help() { println!("Format: --encoding=[ENC_NAME],[MIN],[AF,UBF],[GREP]\n\n"); println!("ENC_NAME (Encoding)="); let list: [&'static str; 41] = [ - ascii_enc_label!(), + ASCII_ENC_LABEL, "Big5", "EUC-JP", "EUC-KR", diff --git a/src/mission.rs b/src/mission.rs index 83322fa..64153d4 100644 --- a/src/mission.rs +++ b/src/mission.rs @@ -3,14 +3,14 @@ extern crate anyhow; extern crate encoding_rs; -use crate::ascii_enc_label; -use crate::chars_min_default; -use crate::counter_offset_default; -use crate::encoding_default; use crate::input::ByteCounter; use crate::options::ARGS; +use crate::options::ASCII_ENC_LABEL; +use crate::options::CHARS_MIN_DEFAULT; +use crate::options::COUNTER_OFFSET_DEFAULT; +use crate::options::ENCODING_DEFAULT; +use crate::options::OUTPUT_LINE_CHAR_NB_MAX_DEFAULT; use crate::options::OUTPUT_LINE_CHAR_NB_MIN; -use crate::output_line_char_nb_max_default; use anyhow::{anyhow, Context, Result}; use encoding_rs::*; use lazy_static::lazy_static; @@ -572,7 +572,7 @@ impl Missions { } let mut v = Vec::new(); - let encoding_default: &[String; 1] = &[String::from_str(&*encoding_default!()).unwrap()]; + let encoding_default: &[String; 1] = &[ENCODING_DEFAULT.to_string()]; let enc_iter = if flag_encoding.is_empty() { encoding_default.iter() @@ -588,12 +588,12 @@ impl Missions { let mut enc_name = match enc_name { Some(s) => s, - None => encoding_default!(), + None => ENCODING_DEFAULT, }; let counter_offset = match flag_counter_offset { Some(n) => n, - None => counter_offset_default!(), + None => COUNTER_OFFSET_DEFAULT, }; // If `char_min_nb` is not defined in `enc_opt` @@ -602,7 +602,7 @@ impl Missions { Some(n) => n, None => match flag_chars_min_nb { Some(n) => n, - None => chars_min_default!(), + None => CHARS_MIN_DEFAULT, }, }; @@ -610,7 +610,7 @@ impl Missions { let output_line_char_nb_max = match flag_output_line_len { Some(n) => n, - None => output_line_char_nb_max_default!(), + None => OUTPUT_LINE_CHAR_NB_MAX_DEFAULT, }; if output_line_char_nb_max < OUTPUT_LINE_CHAR_NB_MIN { @@ -628,16 +628,15 @@ impl Missions { // "x-user-defined" and the `UTF8_FILTER_ASCII_MODE_DEFAULT`-filter, // if not otherwise specified. - let filter_af = filter_af.unwrap_or(flag_ascii_filter.unwrap_or( - if enc_name == ascii_enc_label!() { + let filter_af = + filter_af.unwrap_or(flag_ascii_filter.unwrap_or(if enc_name == ASCII_ENC_LABEL { UTF8_FILTER_ASCII_MODE_DEFAULT.af } else { UTF8_FILTER_NON_ASCII_MODE_DEFAULT.af - }, - )); + })); let filter_ubf = filter_ubf.unwrap_or(flag_unicode_block_filter.unwrap_or( - if enc_name == ascii_enc_label!() { + if enc_name == ASCII_ENC_LABEL { UTF8_FILTER_ASCII_MODE_DEFAULT.ubf } else { UTF8_FILTER_NON_ASCII_MODE_DEFAULT.ubf @@ -649,7 +648,7 @@ impl Missions { None => match flag_grep_char { Some(f) => Some(f), None => { - if enc_name == ascii_enc_label!() { + if enc_name == ASCII_ENC_LABEL { UTF8_FILTER_ASCII_MODE_DEFAULT.grep_char } else { UTF8_FILTER_NON_ASCII_MODE_DEFAULT.grep_char @@ -677,7 +676,7 @@ impl Missions { }; let mut print_encoding_as_ascii = false; - if enc_name == ascii_enc_label!() { + if enc_name == ASCII_ENC_LABEL { print_encoding_as_ascii = true; enc_name = "x-user-defined" }; diff --git a/src/options.rs b/src/options.rs index c52878b..9daa064 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,6 +1,7 @@ //! This module deals with command-line arguments and directly related data //! structures. +use crate::input::ByteCounter; use clap::arg_enum; use lazy_static::lazy_static; use std::path::PathBuf; @@ -8,50 +9,24 @@ use structopt::StructOpt; /// Encoding name literal used when simulating non-built-in /// ASCII-decoder. -#[macro_export] -macro_rules! ascii_enc_label { - () => { - "ascii" - }; -} +pub const ASCII_ENC_LABEL: &str = "ascii"; /// If no command-line argument `--chars_min` is given /// and none is specified in `--encoding` use this. /// Must be one of `--list-encodings`. -#[macro_export] -macro_rules! encoding_default { - () => { - //ascii_enc_label!() - "UTF-8" - }; -} +pub const ENCODING_DEFAULT: &str = "UTF-8"; /// Default value, when no `--chars-min` command-line-argument /// is given. Must be `u8`. -#[macro_export] -macro_rules! chars_min_default { - () => { - 4u8 - }; -} +pub const CHARS_MIN_DEFAULT: u8 = 4; /// Default value, when no `--counter-offset` command-line-argument -/// is given. Must be of type `ByteCounter`. -#[macro_export] -macro_rules! counter_offset_default { - () => { - 0 - }; -} +/// is given. +pub const COUNTER_OFFSET_DEFAULT: ByteCounter = 0; /// Default value when no `--output-line-len` -/// command-line-argument is given. Must be `usize`. -#[macro_export] -macro_rules! output_line_char_nb_max_default { - () => { - 64usize - }; -} +/// command-line-argument is given. +pub const OUTPUT_LINE_CHAR_NB_MAX_DEFAULT: usize = 64; /// There must be space for at least 3 long Unicode characters, /// to guarantee progress in streaming. You want much longer lines.