TextBlocks

Trait TextBlocks 

Source
pub trait TextBlocks: AsRef<str> + Sized
where Self: AsRef<str> + Sized,
{ // Provided methods fn as_blocks(&self, block_delimiter: &BlockDelimiter) -> Vec<Vec<&str>> { ... } fn block_parse_lines<INNER, LP>( &self, block_delimiter: &BlockDelimiter, line_parser: LP, ) -> Vec<Vec<INNER>> where LP: Fn(&str) -> INNER { ... } fn block_parse<INNER, BLOCK, LP, BP>( &self, block_delimiter: &BlockDelimiter, line_parser: LP, block_parser: BP, ) -> Vec<BLOCK> where LP: Fn(&str) -> INNER, BP: Fn(Vec<INNER>) -> BLOCK { ... } }

Provided Methods§

Source

fn as_blocks(&self, block_delimiter: &BlockDelimiter) -> Vec<Vec<&str>>

Parse a string into blocks, where a block is a vector of lines. Blocks are separated by a blank line. Works well with \n or \r\n line endings.

§Example
use textblocks::*;
let s = "100\n200\n\n300\n400\n\n500\n600";
let block_delimiter = BlockDelimiter::DoubleLineGeneric;
assert_eq!(s.as_blocks(&block_delimiter), vec![vec!["100", "200"], vec!["300", "400"], vec!["500", "600"]]);
Source

fn block_parse_lines<INNER, LP>( &self, block_delimiter: &BlockDelimiter, line_parser: LP, ) -> Vec<Vec<INNER>>
where LP: Fn(&str) -> INNER,

Parse a block into a vector of lines, where each line is parsed into a type T, using the provided line parser. If some lines cannot be parsed, make sure to use a type that can handle that (e.g. Option<T> or Result<T, E>) and then use filter_map to remove the lines that could not be parsed.

§Example
use textblocks::*;
let s = "100\n200\n\n300\n400\n\n500\n600";
let block_delimiter = BlockDelimiter::DoubleLineGeneric;
let result = s.block_parse_lines(&block_delimiter, |line| line.parse::<u32>().unwrap());
assert_eq!(result, vec![vec![100, 200], vec![300, 400], vec![500, 600]]);
Source

fn block_parse<INNER, BLOCK, LP, BP>( &self, block_delimiter: &BlockDelimiter, line_parser: LP, block_parser: BP, ) -> Vec<BLOCK>
where LP: Fn(&str) -> INNER, BP: Fn(Vec<INNER>) -> BLOCK,

Parse a block using the provided block parser. Blocks may be reduced to a single value, or parsed into a vector, using the provided block parser. Similar to parse_lines, if some blocks cannot be parsed, make sure to use a type that can handle that (e.g. Option<T> or Result<T, E>) and then use filter_map to remove the blocks that could not be parsed.

§Example
use textblocks::*;
let s = "abcde\nwow\n\n11111\n22222\n33333";
let block_delimiter = BlockDelimiter::DoubleLineGeneric;
let result = s.block_parse(
   &block_delimiter,
   |line| line.chars().next().unwrap(),
   |block| block.iter().collect::<String>(),
);
assert_eq!(result, vec!["aw", "123"]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> TextBlocks for T
where T: AsRef<str> + Sized,