From b384ee4233dfaa81c6e3e02e3d3ce38df14899d5 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 6 Jan 2020 08:21:12 -0800 Subject: [PATCH] make it possible to re-use the line serialization in other pdus --- src/server/codec.rs | 47 +++++++++++++++++++++++++----------------- src/server/listener.rs | 6 +++--- src/server/tab.rs | 2 +- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/server/codec.rs b/src/server/codec.rs index 50aec5a62..6dedc7556 100644 --- a/src/server/codec.rs +++ b/src/server/codec.rs @@ -446,25 +446,30 @@ struct LineHyperlink { coords: Vec, } +/// What's all this? +/// Cells hold references to Arc and it is important to us to +/// maintain identity of the hyperlinks in the individual cells, while also +/// only sending a single copy of the associated URL. +/// This section of code extracts the hyperlinks from the cells and builds +/// up a mapping that can be used to restore the identity when the `lines()` +/// method is called. #[derive(Deserialize, Serialize, PartialEq, Debug)] -pub struct GetLinesResponse { - pub tab_id: TabId, +pub struct SerializedLines { lines: Vec<(StableRowIndex, Line)>, hyperlinks: Vec, // TODO: image references } -impl GetLinesResponse { - pub fn new(tab_id: TabId, mut lines: Vec<(StableRowIndex, Line)>) -> Self { +impl SerializedLines { + pub fn lines(self) -> Vec<(StableRowIndex, Line)> { + self.into() + } +} + +impl From> for SerializedLines { + fn from(mut lines: Vec<(StableRowIndex, Line)>) -> Self { let mut hyperlinks = vec![]; - // What's all this? - // Cells hold references to Arc and it is important to us to - // maintain identity of the hyperlinks in the individual cells, while also - // only sending a single copy of the associated URL. - // This section of code extracts the hyperlinks from the cells and builds - // up a mapping that can be used to restore the identity when the `lines()` - // method is called. for (line_idx, (_, line)) in lines.iter_mut().enumerate() { let mut current_link: Option> = None; let mut current_range = 0..0; @@ -526,16 +531,14 @@ impl GetLinesResponse { } } - Self { - tab_id, - lines, - hyperlinks, - } + Self { lines, hyperlinks } } +} - /// Reconsitute hyperlinks or other attributes that were decomposed for - /// serialization, and return the line data. - pub fn lines(self) -> Vec<(StableRowIndex, Line)> { +/// Reconsitute hyperlinks or other attributes that were decomposed for +/// serialization, and return the line data. +impl Into> for SerializedLines { + fn into(self) -> Vec<(StableRowIndex, Line)> { if self.hyperlinks.is_empty() { self.lines } else { @@ -562,6 +565,12 @@ impl GetLinesResponse { } } +#[derive(Deserialize, Serialize, PartialEq, Debug)] +pub struct GetLinesResponse { + pub tab_id: TabId, + pub lines: SerializedLines, +} + #[cfg(test)] mod test { use super::*; diff --git a/src/server/listener.rs b/src/server/listener.rs index 42092aed9..f0ce9927a 100644 --- a/src/server/listener.rs +++ b/src/server/listener.rs @@ -715,10 +715,10 @@ impl ClientSession { lines_and_indices.push((stable_row, line)); } } - Ok(Pdu::GetLinesResponse(GetLinesResponse::new( + Ok(Pdu::GetLinesResponse(GetLinesResponse { tab_id, - lines_and_indices, - ))) + lines: lines_and_indices.into(), + })) }) } diff --git a/src/server/tab.rs b/src/server/tab.rs index f93539b23..9d6d3583f 100644 --- a/src/server/tab.rs +++ b/src/server/tab.rs @@ -399,7 +399,7 @@ impl RenderableInner { match result { Ok(result) => { let config = configuration(); - let lines = result.lines(); + let lines = result.lines.lines(); log::trace!("got {} lines", lines.len()); for (stable_row, mut line) in lines.into_iter() {