Convert "pure" macros to pure fn in config.rs (#10893)

# Description
These macros simply took a `Span` and a shared reference to `Config` and
returned a Value, for better readability and reasoning about their
behavior convert them to simple function as they don't do anything
relevant with their macro powers.


# User-Facing Changes
None

# Tests + Formatting
(-)
This commit is contained in:
Stefan Holderbach 2023-10-30 19:54:59 +01:00 committed by GitHub
parent 005180f269
commit 3645178ff1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -439,17 +439,16 @@ impl Value {
} }
} }
"history" => { "history" => {
macro_rules! reconstruct_history_file_format { fn reconstruct_history_file_format(config: &Config, span: Span) -> Value {
($span:expr) => { Value::string(
Value::string( match config.history_file_format {
match config.history_file_format { HistoryFileFormat::Sqlite => "sqlite",
HistoryFileFormat::Sqlite => "sqlite", HistoryFileFormat::PlainText => "plaintext",
HistoryFileFormat::PlainText => "plaintext", },
}, span,
$span, )
)
};
} }
if let Value::Record { val, .. } = &mut vals[index] { if let Value::Record { val, .. } = &mut vals[index] {
let Record { cols, vals } = val; let Record { cols, vals } = val;
for index in (0..cols.len()).rev() { for index in (0..cols.len()).rev() {
@ -482,14 +481,16 @@ impl Value {
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'sqlite' or 'plaintext'" "unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'sqlite' or 'plaintext'"
); );
// Reconstruct // Reconstruct
vals[index] = vals[index] = reconstruct_history_file_format(
reconstruct_history_file_format!(span); &config, span,
);
} }
}; };
} else { } else {
invalid!(Some(span), "should be a string"); invalid!(Some(span), "should be a string");
// Reconstruct // Reconstruct
vals[index] = reconstruct_history_file_format!(span); vals[index] =
reconstruct_history_file_format(&config, span);
} }
} }
x => { x => {
@ -510,7 +511,7 @@ impl Value {
record! { record! {
"sync_on_enter" => Value::bool(config.sync_history_on_enter, span), "sync_on_enter" => Value::bool(config.sync_history_on_enter, span),
"max_size" => Value::int(config.max_history_size, span), "max_size" => Value::int(config.max_history_size, span),
"file_format" => reconstruct_history_file_format!(span), "file_format" => reconstruct_history_file_format(&config, span),
"isolation" => Value::bool(config.history_isolation, span), "isolation" => Value::bool(config.history_isolation, span),
}, },
span, span,
@ -518,27 +519,25 @@ impl Value {
} }
} }
"completions" => { "completions" => {
macro_rules! reconstruct_external_completer { fn reconstruct_external_completer(config: &Config, span: Span) -> Value {
($span: expr) => { if let Some(block) = config.external_completer {
if let Some(block) = config.external_completer { Value::block(block, span)
Value::block(block, $span) } else {
} else { Value::nothing(span)
Value::nothing($span) }
}
};
} }
macro_rules! reconstruct_external {
($span: expr) => { fn reconstruct_external(config: &Config, span: Span) -> Value {
Value::record( Value::record(
record! { record! {
"max_results" => Value::int(config.max_external_completion_results, $span), "max_results" => Value::int(config.max_external_completion_results, span),
"completer" => reconstruct_external_completer!($span), "completer" => reconstruct_external_completer(config, span),
"enable" => Value::bool(config.enable_external_completion, $span), "enable" => Value::bool(config.enable_external_completion, span),
}, },
$span, span,
) )
};
} }
if let Value::Record { val, .. } = &mut vals[index] { if let Value::Record { val, .. } = &mut vals[index] {
let Record { cols, vals } = val; let Record { cols, vals } = val;
for index in (0..cols.len()).rev() { for index in (0..cols.len()).rev() {
@ -616,7 +615,7 @@ impl Value {
"should be a block or null" "should be a block or null"
); );
// Reconstruct // Reconstruct
vals[index] = reconstruct_external_completer!( vals[index] = reconstruct_external_completer(&config,
span span
); );
} }
@ -646,7 +645,7 @@ impl Value {
} else { } else {
invalid!(Some(span), "should be a record"); invalid!(Some(span), "should be a record");
// Reconstruct // Reconstruct
vals[index] = reconstruct_external!(span); vals[index] = reconstruct_external(&config, span);
} }
} }
x => { x => {
@ -669,28 +668,29 @@ impl Value {
"partial" => Value::bool(config.partial_completions, span), "partial" => Value::bool(config.partial_completions, span),
"algorithm" => Value::string(config.completion_algorithm.clone(), span), "algorithm" => Value::string(config.completion_algorithm.clone(), span),
"case_sensitive" => Value::bool(config.case_sensitive_completions, span), "case_sensitive" => Value::bool(config.case_sensitive_completions, span),
"external" => reconstruct_external!(span), "external" => reconstruct_external(&config, span),
}, },
span, span,
); );
} }
} }
"cursor_shape" => { "cursor_shape" => {
macro_rules! reconstruct_cursor_shape { fn reconstruct_cursor_shape(
($name:expr, $span:expr) => { name: Option<NuCursorShape>,
Value::string( span: Span,
match $name { ) -> Value {
Some(NuCursorShape::Line) => "line", Value::string(
Some(NuCursorShape::Block) => "block", match name {
Some(NuCursorShape::UnderScore) => "underscore", Some(NuCursorShape::Line) => "line",
Some(NuCursorShape::BlinkLine) => "blink_line", Some(NuCursorShape::Block) => "block",
Some(NuCursorShape::BlinkBlock) => "blink_block", Some(NuCursorShape::UnderScore) => "underscore",
Some(NuCursorShape::BlinkUnderScore) => "blink_underscore", Some(NuCursorShape::BlinkLine) => "blink_line",
None => "inherit", Some(NuCursorShape::BlinkBlock) => "blink_block",
}, Some(NuCursorShape::BlinkUnderScore) => "blink_underscore",
$span, None => "inherit",
) },
}; span,
)
} }
if let Value::Record { val, .. } = &mut vals[index] { if let Value::Record { val, .. } = &mut vals[index] {
let Record { cols, vals } = val; let Record { cols, vals } = val;
@ -734,18 +734,18 @@ impl Value {
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', 'blink_underscore' or 'inherit'" "unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', 'blink_underscore' or 'inherit'"
); );
// Reconstruct // Reconstruct
vals[index] = reconstruct_cursor_shape!( vals[index] = reconstruct_cursor_shape(
config.cursor_shape_vi_insert, config.cursor_shape_vi_insert,
span span,
); );
} }
}; };
} else { } else {
invalid!(Some(span), "should be a string"); invalid!(Some(span), "should be a string");
// Reconstruct // Reconstruct
vals[index] = reconstruct_cursor_shape!( vals[index] = reconstruct_cursor_shape(
config.cursor_shape_vi_insert, config.cursor_shape_vi_insert,
span span,
); );
} }
} }
@ -785,18 +785,18 @@ impl Value {
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', 'blink_underscore' or 'inherit'" "unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', 'blink_underscore' or 'inherit'"
); );
// Reconstruct // Reconstruct
vals[index] = reconstruct_cursor_shape!( vals[index] = reconstruct_cursor_shape(
config.cursor_shape_vi_normal, config.cursor_shape_vi_normal,
span span,
); );
} }
}; };
} else { } else {
invalid!(Some(span), "should be a string"); invalid!(Some(span), "should be a string");
// Reconstruct // Reconstruct
vals[index] = reconstruct_cursor_shape!( vals[index] = reconstruct_cursor_shape(
config.cursor_shape_vi_normal, config.cursor_shape_vi_normal,
span span,
); );
} }
} }
@ -836,18 +836,18 @@ impl Value {
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', 'blink_underscore' or 'inherit'" "unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', 'blink_underscore' or 'inherit'"
); );
// Reconstruct // Reconstruct
vals[index] = reconstruct_cursor_shape!( vals[index] = reconstruct_cursor_shape(
config.cursor_shape_emacs, config.cursor_shape_emacs,
span span,
); );
} }
}; };
} else { } else {
invalid!(Some(span), "should be a string"); invalid!(Some(span), "should be a string");
// Reconstruct // Reconstruct
vals[index] = reconstruct_cursor_shape!( vals[index] = reconstruct_cursor_shape(
config.cursor_shape_emacs, config.cursor_shape_emacs,
span span,
); );
} }
} }
@ -867,53 +867,50 @@ impl Value {
// Reconstruct // Reconstruct
vals[index] = Value::record( vals[index] = Value::record(
record! { record! {
"vi_insert" => reconstruct_cursor_shape!(config.cursor_shape_vi_insert, span), "vi_insert" => reconstruct_cursor_shape(config.cursor_shape_vi_insert, span),
"vi_normal" => reconstruct_cursor_shape!(config.cursor_shape_vi_normal, span), "vi_normal" => reconstruct_cursor_shape(config.cursor_shape_vi_normal, span),
"emacs" => reconstruct_cursor_shape!(config.cursor_shape_emacs, span), "emacs" => reconstruct_cursor_shape(config.cursor_shape_emacs, span),
}, },
span, span,
); );
} }
} }
"table" => { "table" => {
macro_rules! reconstruct_index_mode { fn reconstruct_index_mode(config: &Config, span: Span) -> Value {
($span:expr) => { Value::string(
Value::string( match config.table_index_mode {
match config.table_index_mode { TableIndexMode::Always => "always",
TableIndexMode::Always => "always", TableIndexMode::Never => "never",
TableIndexMode::Never => "never", TableIndexMode::Auto => "auto",
TableIndexMode::Auto => "auto", },
span,
)
}
fn reconstruct_trim_strategy(config: &Config, span: Span) -> Value {
match &config.trim_strategy {
TrimStrategy::Wrap { try_to_keep_words } => Value::record(
record! {
"methodology" => Value::string("wrapping", span),
"wrapping_try_keep_words" => Value::bool(*try_to_keep_words, span),
}, },
$span, span,
) ),
}; TrimStrategy::Truncate { suffix } => Value::record(
} match suffix {
macro_rules! reconstruct_trim_strategy { Some(s) => record! {
($span:expr) => { "methodology" => Value::string("truncating", span),
match &config.trim_strategy { "truncating_suffix" => Value::string(s.clone(), span),
TrimStrategy::Wrap { try_to_keep_words } => Value::record(
record! {
"methodology" => Value::string("wrapping", $span),
"wrapping_try_keep_words" => Value::bool(*try_to_keep_words, $span),
}, },
$span, None => record! {
), "methodology" => Value::string("truncating", span),
TrimStrategy::Truncate { suffix } => Value::record( "truncating_suffix" => Value::nothing(span),
match suffix {
Some(s) => record! {
"methodology" => Value::string("truncating", $span),
"truncating_suffix" => Value::string(s.clone(), $span),
},
None => record! {
"methodology" => Value::string("truncating", $span),
"truncating_suffix" => Value::nothing($span),
},
}, },
$span, },
), span,
} ),
}; }
} }
if let Value::Record { val, .. } = &mut vals[index] { if let Value::Record { val, .. } = &mut vals[index] {
let Record { cols, vals } = val; let Record { cols, vals } = val;
for index in (0..cols.len()).rev() { for index in (0..cols.len()).rev() {
@ -999,12 +996,13 @@ impl Value {
invalid!( Some(span), invalid!( Some(span),
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'never', 'always' or 'auto'" "unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'never', 'always' or 'auto'"
); );
vals[index] = reconstruct_index_mode!(span); vals[index] =
reconstruct_index_mode(&config, span);
} }
} }
} else { } else {
invalid!(Some(span), "should be a string"); invalid!(Some(span), "should be a string");
vals[index] = reconstruct_index_mode!(span); vals[index] = reconstruct_index_mode(&config, span);
} }
} }
"trim" => { "trim" => {
@ -1013,7 +1011,8 @@ impl Value {
Err(e) => { Err(e) => {
// try_parse_trim_strategy() already adds its own errors // try_parse_trim_strategy() already adds its own errors
errors.push(e); errors.push(e);
vals[index] = reconstruct_trim_strategy!(span); vals[index] =
reconstruct_trim_strategy(&config, span);
} }
} }
} }
@ -1048,8 +1047,8 @@ impl Value {
vals[index] = Value::record( vals[index] = Value::record(
record! { record! {
"mode" => Value::string(config.table_mode.clone(), span), "mode" => Value::string(config.table_mode.clone(), span),
"index_mode" => reconstruct_index_mode!(span), "index_mode" => reconstruct_index_mode(&config, span),
"trim" => reconstruct_trim_strategy!(span), "trim" => reconstruct_trim_strategy(&config, span),
"show_empty" => Value::bool(config.table_show_empty, span), "show_empty" => Value::bool(config.table_show_empty, span),
}, },
span, span,