From 7772cd146d550c1fe0d06675bb797cafb7fbe19a Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Fri, 16 Feb 2018 16:32:54 -0800 Subject: [PATCH] Add AsSpannedStr trait For nicer calls to resolve --- src/utils/lines/spans/lines_iterator.rs | 2 +- src/utils/lines/spans/row.rs | 11 +++++++---- src/utils/lines/spans/segment.rs | 11 ----------- src/utils/lines/spans/tests.rs | 3 ++- src/utils/span.rs | 18 ++++++++++++++++-- src/views/text_view.rs | 2 +- 6 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/utils/lines/spans/lines_iterator.rs b/src/utils/lines/spans/lines_iterator.rs index eddb737..6282da6 100644 --- a/src/utils/lines/spans/lines_iterator.rs +++ b/src/utils/lines/spans/lines_iterator.rs @@ -2,7 +2,7 @@ use super::chunk::{Chunk, ChunkPart}; use super::chunk_iterator::ChunkIterator; use super::prefix::prefix; use super::row::Row; -use super::segment::{Segment, SegmentWithText}; +use super::segment::{Segment}; use super::segment_merge_iterator::SegmentMergeIterator; use std::iter::Peekable; use std::rc::Rc; diff --git a/src/utils/lines/spans/row.rs b/src/utils/lines/spans/row.rs index e27efc4..33d202e 100644 --- a/src/utils/lines/spans/row.rs +++ b/src/utils/lines/spans/row.rs @@ -1,5 +1,5 @@ use super::Segment; -use utils::span::{Span, SpannedStr}; +use utils::span::{Span, AsSpannedStr}; /// A list of segments representing a row of text #[derive(Debug, Clone, PartialEq, Eq)] @@ -12,9 +12,12 @@ pub struct Row { impl Row { /// Resolve the row indices into string slices and attributes. - pub fn resolve<'a, T>( - &self, source: SpannedStr<'a, T> - ) -> Vec> { + pub fn resolve<'a, T, S>(&self, source: S) -> Vec> + where + S: 'a + AsSpannedStr<'a, T>, + { + let source = source.as_spanned_str(); + self.segments .iter() .map(|seg| seg.resolve(source.clone())) diff --git a/src/utils/lines/spans/segment.rs b/src/utils/lines/spans/segment.rs index 0ef167d..104cbe4 100644 --- a/src/utils/lines/spans/segment.rs +++ b/src/utils/lines/spans/segment.rs @@ -16,11 +16,6 @@ pub struct Segment { } impl Segment { - #[cfg(test)] - pub fn with_text<'a>(self, text: &'a str) -> SegmentWithText<'a> { - SegmentWithText { text, seg: self } - } - /// Resolve this segment to a string slice and an attribute. pub fn resolve<'a, T>( &self, source: SpannedStr<'a, T> @@ -49,9 +44,3 @@ impl Segment { content } } - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct SegmentWithText<'a> { - pub seg: Segment, - pub text: &'a str, -} diff --git a/src/utils/lines/spans/tests.rs b/src/utils/lines/spans/tests.rs index 8289bf2..f78dc31 100644 --- a/src/utils/lines/spans/tests.rs +++ b/src/utils/lines/spans/tests.rs @@ -21,7 +21,8 @@ fn test_line_breaks() { let iter = LinesIterator::new(&input, 17); - let rows: Vec<_> = iter.map(|row| row.resolve(input.as_spanned_str())).collect(); + let rows: Vec<_> = iter.map(|row| row.resolve(&input)) + .collect(); assert_eq!( &rows[..], diff --git a/src/utils/span.rs b/src/utils/span.rs index de00b62..ddd14d8 100644 --- a/src/utils/span.rs +++ b/src/utils/span.rs @@ -23,6 +23,12 @@ where spans: &'a [IndexedSpan], } +/// Represents a type that can be converted to a `SpannedStr`. +pub trait AsSpannedStr<'a, T> { + /// Returns a view of `self` + fn as_spanned_str(&self) -> SpannedStr<'a, T>; +} + /// Describes an object that appears like a `SpannedStr`. pub trait SpannedText { /// Type of span returned by `SpannedText::spans()`. @@ -252,13 +258,21 @@ impl SpannedString { pub fn is_empty(&self) -> bool { self.source.is_empty() || self.spans.is_empty() } +} - /// Returns a `SpannedStr` referencing `self`. - pub fn as_spanned_str<'a>(&'a self) -> SpannedStr<'a, T> { +impl<'a, T> AsSpannedStr<'a, T> for &'a SpannedString { + fn as_spanned_str(&self) -> SpannedStr<'a, T> { SpannedStr::new(&self.source, &self.spans) } } +impl<'a, T> AsSpannedStr<'a, T> for SpannedStr<'a, T> { + fn as_spanned_str(&self) -> SpannedStr<'a, T> { + SpannedStr::new(&self.source, &self.spans) + } +} + + /// An indexed span with an associated attribute. #[derive(Debug, Clone, PartialEq, Eq)] pub struct IndexedSpan { diff --git a/src/views/text_view.rs b/src/views/text_view.rs index 484adc5..e0f3370 100644 --- a/src/views/text_view.rs +++ b/src/views/text_view.rs @@ -461,7 +461,7 @@ impl View for TextView { let l = row.width; let mut x = self.align.h.get_offset(l, printer.size.x); - for span in row.resolve(content.content.as_spanned_str()) { + for span in row.resolve(&content.content) { printer.with_style(*span.attr, |printer| { printer.print((x, 0), span.content); x += span.content.width();