Skip to content

Commit

Permalink
IntoIterator<(Style, &str)> seems natural
Browse files Browse the repository at this point in the history
As a return type for `highlight_line`
* `Cow<'_, str>` -> `ansi_str::AnsiBlockIter<'_>` for compatibility with existing code
* `Vec<(anstyle:Style, &str)>` for a default / test implementation
But may be difficult to handle on our side or it's just me...
  • Loading branch information
gwenn committed Aug 19, 2024
1 parent 35cbf33 commit 6b34d32
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions src/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,20 @@ impl StyledBlock for (anstyle::Style, &str) {
}
}

/// Ordered list of styled block
#[cfg(feature = "split-highlight")]
#[cfg_attr(docsrs, doc(cfg(feature = "split-highlight")))]
pub trait StyledBlocks {
/// Styled block
type StyledBlock: StyledBlock
where
Self: Sized;

/// FIXME maybe we can use Iterator trait directly ?
fn next(&mut self) -> Option<Self::StyledBlock>
where
Self: Sized;
}
struct Ansi<'s>(Cow<'s, str>);

Check warning on line 83 in src/highlight.rs

View workflow job for this annotation

GitHub Actions / Test min versions

struct `Ansi` is never constructed

/// Ordered list of styled block
#[cfg(feature = "ansi-str")]
#[cfg_attr(docsrs, doc(cfg(feature = "ansi-str")))]
impl<'l> StyledBlocks for ansi_str::AnsiBlockIter<'l> {
type StyledBlock = ansi_str::AnsiBlock<'l>;

fn next(&mut self) -> Option<Self::StyledBlock> {
Iterator::next(self)
impl<'s> IntoIterator for Ansi<'s> {
type IntoIter = ansi_str::AnsiBlockIter<'s>;
type Item = ansi_str::AnsiBlock<'s>;

fn into_iter(self) -> Self::IntoIter {
match self {
Ansi(Cow::Borrowed(s)) => ansi_str::get_blocks(s),
Ansi(Cow::Owned(s)) => ansi_str::get_blocks(&s), // self_cell ?
}
}
}

Expand All @@ -128,7 +120,7 @@ pub trait Highlighter {
/// returns the styled blocks.
#[cfg(feature = "split-highlight")]
#[cfg_attr(docsrs, doc(cfg(feature = "split-highlight")))]
fn highlight_line<'l>(&self, line: &'l str, pos: usize) -> &dyn StyledBlocks {
fn highlight_line<'l>(&self, line: &'l str, pos: usize) -> dyn IntoIterator {
let _s = self.highlight(line, pos);
// it doesn't seem possible to return an AnsiBlockIter directly
//StyleBlocks::Whole(s)
Expand Down

0 comments on commit 6b34d32

Please sign in to comment.