TextBlocks

Rust Release Dependency status crates.io Downloads crates.io

A simple crate for parsing text blocks. Can be used to parse text files with blocks of data separated by blank lines. Works well with \n or \r\n line endings.

Contains the TextBlocks trait which adds the methods as_blocks, block_parse_lines and block_parse to str and String.

Install

Run the following command in your project directory:

bash cargo add textblocks

Or add the following to your Cargo.toml:

toml [dependencies] textblocks = "0.1.0"

Check the crates.io page for the latest version.

Usage

To parse text into blocks, you need to provide a block delimiter, a line parser and a block parser.

Examples

[!IMPORTANT] This will allocate a vector of vectors of &str. If you want to avoid these allocations, use block_parse_lines or block_parse. In that case, A vector will only be allocated for the requested result type.

rust 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"]]); assert_eq!(s.as_blocks(&block_delimiter), [["100", "200"], ["300", "400"], ["500", "600"]]);

rust 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, [[100, 200], [300, 400], [500, 600]]);

rust use textblocks::*; let s = "100\n200\n\n300\n400\n\n500\n600"; let block_delimiter = BlockDelimiter::DoubleLineGeneric; let result = s.block_parse( &block_delimiter, |line| line.parse::<u32>().unwrap(), |block| block.iter().sum::<u32>() ); assert_eq!(result, [300, 700, 1100]);