pub trait TextBlocks: AsRef<str> + Sizedwhere
    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"]);

Implementors§

source§

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