diff options
author | Alex Vinyals | 2023-06-07 08:50:16 +0000 |
---|---|---|
committer | GitHub | 2023-06-07 08:50:16 +0000 |
commit | 204bac1706badaf562304b67a4beb63102560e40 (patch) | |
tree | ef6e862f77c6fb592046ea7eaec3a2ab9d041291 /helix-term | |
parent | ba691f4fb0c0ef3baf34ee5f05360dd11ac204bc (diff) |
commands(toggle): use pattern matching on the Value enum (#7240)
Diffstat (limited to 'helix-term')
-rw-r--r-- | helix-term/src/commands/typed.rs | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index fb285e72..824abbf4 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -8,6 +8,7 @@ use super::*; use helix_core::{encoding, shellwords::Shellwords}; use helix_view::document::DEFAULT_LANGUAGE_NAME; use helix_view::editor::{Action, CloseError, ConfigEvent}; +use serde_json::Value; use ui::completers::{self, Completer}; #[derive(Clone)] @@ -1764,7 +1765,7 @@ fn set_option( *value = if value.is_string() { // JSON strings require quotes, so we can't .parse() directly - serde_json::Value::String(arg.to_string()) + Value::String(arg.to_string()) } else { arg.parse().map_err(field_error)? }; @@ -1800,29 +1801,21 @@ fn toggle_option( let pointer = format!("/{}", key.replace('.', "/")); let value = config.pointer_mut(&pointer).ok_or_else(key_error)?; - *value = match value.as_bool() { - Some(value) => { + *value = match value { + Value::Bool(ref value) => { ensure!( args.len() == 1, "Bad arguments. For boolean configurations use: `:toggle key`" ); - serde_json::Value::Bool(!value) + Value::Bool(!value) } - None => { + Value::String(ref value) => { ensure!( args.len() > 2, - "Bad arguments. For non-boolean configurations use: `:toggle key val1 val2 ...`", - ); - ensure!( - value.is_string(), - "Bad configuration. Cannot cycle non-string configurations" + "Bad arguments. For string configurations use: `:toggle key val1 val2 ...`", ); - let value = value - .as_str() - .expect("programming error: should have been ensured before"); - - serde_json::Value::String( + Value::String( args[1..] .iter() .skip_while(|e| *e != value) @@ -1831,6 +1824,9 @@ fn toggle_option( .to_string(), ) } + Value::Null | Value::Object(_) | Value::Array(_) | Value::Number(_) => { + anyhow::bail!("Configuration {key} does not support toggle yet") + } }; let status = format!("'{key}' is now set to {value}"); |