From 8143becae6918c249e30a2531e0b268401e91aa9 Mon Sep 17 00:00:00 2001 From: Luis Holanda Date: Wed, 29 Jan 2020 23:04:53 -0300 Subject: [PATCH] remove unneeded clones from brigde::events --- src/bridge/events.rs | 330 +++++++++++++++++++++--------------------- src/bridge/handler.rs | 2 +- 2 files changed, 168 insertions(+), 164 deletions(-) diff --git a/src/bridge/events.rs b/src/bridge/events.rs index 757dad9..14fd2e6 100644 --- a/src/bridge/events.rs +++ b/src/bridge/events.rs @@ -159,95 +159,97 @@ fn unpack_color(packed_color: u64) -> Color4f { } } -fn parse_array(array_value: &Value) -> Result> { - if let Value::Array(content) = array_value.clone() { - Ok(content.to_vec()) +fn parse_array(array_value: &Value) -> Result<&[Value]> { + if let Value::Array(content) = array_value { + Ok(&content[..]) } else { Err(EventParseError::InvalidArray(array_value.clone())) } } -fn parse_map(map_value: &Value) -> Result> { - if let Value::Map(content) = map_value.clone() { - Ok(content) +fn parse_map(map_value: &Value) -> Result<&[(Value, Value)]> { + if let Value::Map(content) = map_value { + Ok(&content[..]) } else { Err(EventParseError::InvalidMap(map_value.clone())) } } -fn parse_string(string_value: &Value) -> Result { - if let Value::String(content) = string_value.clone() { - Ok(content.into_str().ok_or(EventParseError::InvalidString(string_value.clone()))?) +fn parse_string(string_value: &Value) -> Result<&str> { + if let Value::String(content) = string_value { + content.as_str().ok_or_else(|| EventParseError::InvalidString(string_value.clone())) } else { Err(EventParseError::InvalidString(string_value.clone())) } } fn parse_u64(u64_value: &Value) -> Result { - if let Value::Integer(content) = u64_value.clone() { - Ok(content.as_u64().ok_or(EventParseError::InvalidU64(u64_value.clone()))?) + if let Value::Integer(content) = u64_value { + Ok(content.as_u64().ok_or_else(|| EventParseError::InvalidU64(u64_value.clone()))?) } else { Err(EventParseError::InvalidU64(u64_value.clone())) } } fn parse_i64(i64_value: &Value) -> Result { - if let Value::Integer(content) = i64_value.clone() { - Ok(content.as_i64().ok_or(EventParseError::InvalidI64(i64_value.clone()))?) + if let Value::Integer(content) = i64_value { + Ok(content.as_i64().ok_or_else(|| EventParseError::InvalidI64(i64_value.clone()))?) } else { Err(EventParseError::InvalidI64(i64_value.clone())) } } fn parse_bool(bool_value: &Value) -> Result { - if let Value::Boolean(content) = bool_value.clone() { - Ok(content) + if let Value::Boolean(content) = bool_value { + Ok(*content) } else { Err(EventParseError::InvalidBool(bool_value.clone())) } } -fn parse_set_title(set_title_arguments: Vec) -> Result { - if let [title] = set_title_arguments.as_slice() { +fn parse_set_title(set_title_arguments: &[Value]) -> Result { + if let [title] = set_title_arguments { Ok(RedrawEvent::SetTitle { - title: parse_string(title)? + title: parse_string(title)?.to_string() }) } else { Err(EventParseError::InvalidEventFormat) } } -fn parse_mode_info_set(mode_info_set_arguments: Vec) -> Result { - if let [_cursor_style_enabled, mode_info] = mode_info_set_arguments.as_slice() { +fn parse_mode_info_set(mode_info_set_arguments: &[Value]) -> Result { + if let [_cursor_style_enabled, mode_info] = mode_info_set_arguments { let mode_info_values = parse_array(mode_info)?; - let mut cursor_modes = Vec::new(); + let mut cursor_modes = Vec::with_capacity(mode_info_values.len()); + for mode_info_value in mode_info_values { - let info_map = parse_map(&mode_info_value)?; + let info_map = parse_map(mode_info_value)?; let mut mode_info = CursorMode::default(); + for (name, value) in info_map { - let name = parse_string(&name)?; - match name.as_ref() { + match parse_string(name)? { "cursor_shape" => { - mode_info.shape = CursorShape::from_type_name(&parse_string(&value)?); + mode_info.shape = CursorShape::from_type_name(parse_string(value)?); }, "cell_percentage" => { - mode_info.cell_percentage = Some(parse_u64(&value)? as f32 / 100.0); + mode_info.cell_percentage = Some(parse_u64(value)? as f32 / 100.0); }, "blinkwait" => { - mode_info.blinkwait = Some(parse_u64(&value)?); + mode_info.blinkwait = Some(parse_u64(value)?); }, "blinkon" => { - mode_info.blinkon = Some(parse_u64(&value)?); + mode_info.blinkon = Some(parse_u64(value)?); }, "blinkoff" => { - mode_info.blinkoff = Some(parse_u64(&value)?); + mode_info.blinkoff = Some(parse_u64(value)?); } "attr_id" => { - mode_info.style_id = Some(parse_u64(&value)?); + mode_info.style_id = Some(parse_u64(value)?); }, _ => {} } } + cursor_modes.push(mode_info); } Ok(RedrawEvent::ModeInfoSet { @@ -258,20 +260,20 @@ fn parse_mode_info_set(mode_info_set_arguments: Vec) -> Result) -> Result { - if let [name, value] = option_set_arguments.as_slice() { +fn parse_option_set(option_set_arguments: &[Value]) -> Result { + if let [name, value] = option_set_arguments { Ok(RedrawEvent::OptionSet { - gui_option: match parse_string(&name)?.as_ref() { - "arabicshape" => GuiOption::AribicShape(parse_bool(&value)?), - "ambiwidth" => GuiOption::AmbiWidth(parse_string(&value)?), - "emoji" => GuiOption::Emoji(parse_bool(&value)?), - "guifont" => GuiOption::GuiFont(parse_string(&value)?), - "guifontset" => GuiOption::GuiFontSet(parse_string(&value)?), - "guifontwide" => GuiOption::GuiFontWide(parse_string(&value)?), - "linespace" => GuiOption::LineSpace(parse_u64(&value)?), - "pumblend" => GuiOption::Pumblend(parse_u64(&value)?), - "showtabline" => GuiOption::ShowTabLine(parse_u64(&value)?), - "termguicolors" => GuiOption::TermGuiColors(parse_bool(&value)?), + gui_option: match parse_string(name)? { + "arabicshape" => GuiOption::AribicShape(parse_bool(value)?), + "ambiwidth" => GuiOption::AmbiWidth(parse_string(value)?.to_string()), + "emoji" => GuiOption::Emoji(parse_bool(value)?), + "guifont" => GuiOption::GuiFont(parse_string(value)?.to_string()), + "guifontset" => GuiOption::GuiFontSet(parse_string(value)?.to_string()), + "guifontwide" => GuiOption::GuiFontWide(parse_string(value)?.to_string()), + "linespace" => GuiOption::LineSpace(parse_u64(value)?), + "pumblend" => GuiOption::Pumblend(parse_u64(value)?), + "showtabline" => GuiOption::ShowTabLine(parse_u64(value)?), + "termguicolors" => GuiOption::TermGuiColors(parse_bool(value)?), unknown_option => GuiOption::Unknown(unknown_option.to_string(), value.clone()) } }) @@ -280,34 +282,34 @@ fn parse_option_set(option_set_arguments: Vec) -> Result { } } -fn parse_mode_change(mode_change_arguments: Vec) -> Result { - if let [_mode, mode_index] = mode_change_arguments.as_slice() { +fn parse_mode_change(mode_change_arguments: &[Value]) -> Result { + if let [_mode, mode_index] = mode_change_arguments { Ok(RedrawEvent::ModeChange { - mode_index: parse_u64(&mode_index)? + mode_index: parse_u64(mode_index)? }) } else { Err(EventParseError::InvalidEventFormat) } } -fn parse_grid_resize(grid_resize_arguments: Vec) -> Result { - if let [grid_id, width, height] = grid_resize_arguments.as_slice() { +fn parse_grid_resize(grid_resize_arguments: &[Value]) -> Result { + if let [grid_id, width, height] = grid_resize_arguments { Ok(RedrawEvent::Resize { - grid: parse_u64(&grid_id)?, width: parse_u64(&width)?, height: parse_u64(&height)? + grid: parse_u64(grid_id)?, width: parse_u64(width)?, height: parse_u64(height)? }) } else { Err(EventParseError::InvalidEventFormat) } } -fn parse_default_colors(default_colors_arguments: Vec) -> Result { +fn parse_default_colors(default_colors_arguments: &[Value]) -> Result { if let [ foreground, background, special, _term_foreground, _term_background - ] = default_colors_arguments.as_slice() { + ] = default_colors_arguments { Ok(RedrawEvent::DefaultColorsSet { colors: Colors { - foreground: Some(unpack_color(parse_u64(&foreground)?)), - background: Some(unpack_color(parse_u64(&background)?)), + foreground: Some(unpack_color(parse_u64(foreground)?)), + background: Some(unpack_color(parse_u64(background)?)), special: Some(unpack_color(parse_u64(special)?)), } }) @@ -319,6 +321,7 @@ fn parse_default_colors(default_colors_arguments: Vec) -> Result Result