From 839f2938a1c446c089ca795712dcb9aee22121de Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Tue, 14 Sep 2021 00:57:31 -0700 Subject: [PATCH] make event handling forwards compatible and use constant generics to simplify parsing logic --- src/bridge/events.rs | 206 +++++++++++++++++-------------------------- 1 file changed, 82 insertions(+), 124 deletions(-) diff --git a/src/bridge/events.rs b/src/bridge/events.rs index 587cf79..ad93d24 100644 --- a/src/bridge/events.rs +++ b/src/bridge/events.rs @@ -228,7 +228,7 @@ pub enum RedrawEvent { bottom_line: f64, current_line: f64, current_column: f64, - line_count: f64, + line_count: Option, }, CommandLineShow { content: StyledContent, @@ -358,17 +358,38 @@ fn unpack_color(packed_color: u64) -> Color4f { } } -fn extract_values>(values: Vec, mut arr: Arr) -> Result { - let arr_ref = arr.as_mut(); +fn extract_values(values: Vec) -> Result<[Value; REQ]> { + if REQ > values.len() { + Err(ParseError::Format(format!("{:?}", values))) + } else { + let mut required_values = vec![Value::Nil; REQ]; + + for (index, value) in values.into_iter().enumerate() { + if index < REQ { + required_values[index] = value; + } + } - if values.len() != arr_ref.len() { + Ok(required_values.try_into().unwrap()) + } +} + +fn extract_values_with_optional(values: Vec) -> Result<([Value; REQ], [Option; OPT])> { + if REQ > values.len() { Err(ParseError::Format(format!("{:?}", values))) } else { - for (i, val) in values.into_iter().enumerate() { - arr_ref[i] = val; + let mut required_values = vec![Value::Nil; REQ]; + let mut optional_values = vec![None; OPT]; + + for (index, value) in values.into_iter().enumerate() { + if index < REQ { + required_values[index] = value; + } else { + optional_values[index - REQ] = Some(value); + } } - Ok(arr) + Ok((required_values.try_into().unwrap(), optional_values.try_into().unwrap())) } } @@ -401,7 +422,7 @@ fn parse_bool(bool_value: Value) -> Result { } fn parse_set_title(set_title_arguments: Vec) -> Result { - let [title] = extract_values(set_title_arguments, [Value::Nil])?; + let [title] = extract_values(set_title_arguments)?; Ok(RedrawEvent::SetTitle { title: parse_string(title)?, @@ -410,7 +431,7 @@ fn parse_set_title(set_title_arguments: Vec) -> Result { fn parse_mode_info_set(mode_info_set_arguments: Vec) -> Result { let [_cursor_style_enabled, mode_info] = - extract_values(mode_info_set_arguments, [Value::Nil, Value::Nil])?; + extract_values(mode_info_set_arguments)?; let mode_info_values = parse_array(mode_info)?; let mut cursor_modes = Vec::with_capacity(mode_info_values.len()); @@ -450,7 +471,7 @@ fn parse_mode_info_set(mode_info_set_arguments: Vec) -> Result) -> Result { - let [name, value] = extract_values(option_set_arguments, [Value::Nil, Value::Nil])?; + let [name, value] = extract_values(option_set_arguments)?; let name = parse_string(name)?; @@ -472,7 +493,7 @@ fn parse_option_set(option_set_arguments: Vec) -> Result { } fn parse_mode_change(mode_change_arguments: Vec) -> Result { - let [mode, mode_index] = extract_values(mode_change_arguments, [Value::Nil, Value::Nil])?; + let [mode, mode_index] = extract_values(mode_change_arguments)?; let mode_name = parse_string(mode)?; Ok(RedrawEvent::ModeChange { @@ -490,7 +511,7 @@ fn parse_mode_change(mode_change_arguments: Vec) -> Result { fn parse_grid_resize(grid_resize_arguments: Vec) -> Result { let [grid_id, width, height] = - extract_values(grid_resize_arguments, [Value::Nil, Value::Nil, Value::Nil])?; + extract_values(grid_resize_arguments)?; Ok(RedrawEvent::Resize { grid: parse_u64(grid_id)?, @@ -500,9 +521,8 @@ fn parse_grid_resize(grid_resize_arguments: Vec) -> Result { } fn parse_default_colors(default_colors_arguments: Vec) -> Result { - let values = [Value::Nil, Value::Nil, Value::Nil, Value::Nil, Value::Nil]; let [foreground, background, special, _term_foreground, _term_background] = - extract_values(default_colors_arguments, values)?; + extract_values(default_colors_arguments)?; Ok(RedrawEvent::DefaultColorsSet { colors: Colors { @@ -550,9 +570,8 @@ fn parse_style(style_map: Value) -> Result