Skip to content

Commit

Permalink
replace macros with constants
Browse files Browse the repository at this point in the history
  • Loading branch information
getreu committed Mar 19, 2020
1 parent 253bbd7 commit 1ff1e3d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/finding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
})?;
Expand Down
4 changes: 2 additions & 2 deletions src/help.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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",
Expand Down
33 changes: 16 additions & 17 deletions src/mission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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`
Expand All @@ -602,15 +602,15 @@ impl Missions {
Some(n) => n,
None => match flag_chars_min_nb {
Some(n) => n,
None => chars_min_default!(),
None => CHARS_MIN_DEFAULT,
},
};

let require_same_unicode_block = flag_same_unicode_block;

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 {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"
};
Expand Down
41 changes: 8 additions & 33 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,32 @@
//! 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;
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.
Expand Down

0 comments on commit 1ff1e3d

Please sign in to comment.