Add AsSpannedStr trait

For nicer calls to resolve
This commit is contained in:
Alexandre Bury 2018-02-16 16:32:54 -08:00
parent 25e65a87e8
commit 7772cd146d
6 changed files with 27 additions and 20 deletions

View File

@ -2,7 +2,7 @@ use super::chunk::{Chunk, ChunkPart};
use super::chunk_iterator::ChunkIterator; use super::chunk_iterator::ChunkIterator;
use super::prefix::prefix; use super::prefix::prefix;
use super::row::Row; use super::row::Row;
use super::segment::{Segment, SegmentWithText}; use super::segment::{Segment};
use super::segment_merge_iterator::SegmentMergeIterator; use super::segment_merge_iterator::SegmentMergeIterator;
use std::iter::Peekable; use std::iter::Peekable;
use std::rc::Rc; use std::rc::Rc;

View File

@ -1,5 +1,5 @@
use super::Segment; use super::Segment;
use utils::span::{Span, SpannedStr}; use utils::span::{Span, AsSpannedStr};
/// A list of segments representing a row of text /// A list of segments representing a row of text
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -12,9 +12,12 @@ pub struct Row {
impl Row { impl Row {
/// Resolve the row indices into string slices and attributes. /// Resolve the row indices into string slices and attributes.
pub fn resolve<'a, T>( pub fn resolve<'a, T, S>(&self, source: S) -> Vec<Span<'a, T>>
&self, source: SpannedStr<'a, T> where
) -> Vec<Span<'a, T>> { S: 'a + AsSpannedStr<'a, T>,
{
let source = source.as_spanned_str();
self.segments self.segments
.iter() .iter()
.map(|seg| seg.resolve(source.clone())) .map(|seg| seg.resolve(source.clone()))

View File

@ -16,11 +16,6 @@ pub struct Segment {
} }
impl 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. /// Resolve this segment to a string slice and an attribute.
pub fn resolve<'a, T>( pub fn resolve<'a, T>(
&self, source: SpannedStr<'a, T> &self, source: SpannedStr<'a, T>
@ -49,9 +44,3 @@ impl Segment {
content content
} }
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SegmentWithText<'a> {
pub seg: Segment,
pub text: &'a str,
}

View File

@ -21,7 +21,8 @@ fn test_line_breaks() {
let iter = LinesIterator::new(&input, 17); 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!( assert_eq!(
&rows[..], &rows[..],

View File

@ -23,6 +23,12 @@ where
spans: &'a [IndexedSpan<T>], spans: &'a [IndexedSpan<T>],
} }
/// 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`. /// Describes an object that appears like a `SpannedStr`.
pub trait SpannedText { pub trait SpannedText {
/// Type of span returned by `SpannedText::spans()`. /// Type of span returned by `SpannedText::spans()`.
@ -252,13 +258,21 @@ impl<T> SpannedString<T> {
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.source.is_empty() || self.spans.is_empty() self.source.is_empty() || self.spans.is_empty()
} }
}
/// Returns a `SpannedStr` referencing `self`. impl<'a, T> AsSpannedStr<'a, T> for &'a SpannedString<T> {
pub fn as_spanned_str<'a>(&'a self) -> SpannedStr<'a, T> { fn as_spanned_str(&self) -> SpannedStr<'a, T> {
SpannedStr::new(&self.source, &self.spans) 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. /// An indexed span with an associated attribute.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct IndexedSpan<T> { pub struct IndexedSpan<T> {

View File

@ -461,7 +461,7 @@ impl View for TextView {
let l = row.width; let l = row.width;
let mut x = self.align.h.get_offset(l, printer.size.x); 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.with_style(*span.attr, |printer| {
printer.print((x, 0), span.content); printer.print((x, 0), span.content);
x += span.content.width(); x += span.content.width();