From 204bac1706badaf562304b67a4beb63102560e40 Mon Sep 17 00:00:00 2001 From: Alex Vinyals Date: Wed, 7 Jun 2023 10:50:16 +0200 Subject: [PATCH] commands(toggle): use pattern matching on the Value enum (#7240) --- helix-term/src/commands/typed.rs | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index fb285e726..824abbf4c 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}");