pub struct TextParser<'a> { /* private fields */ }Expand description
Parses the text so it can be rendered more easily. It can be used to get next line (or word) from the text using either word wrap or letter wrap. Parses the text so it can be rendered more easily. It can be used to get next line (or word, but it’s mainly for line) from the text using either word wrap or letter wrap.
§Examples
Parsing text with word wrap:
let text = get_text();
let mut text_iter = text.chars();
// Word wrap is set by default
let mut parser = TextParser::new(&mut text_iter);
// Reads lines with maximum length 20 until end of the text
while let Some((line, len)) = parser.next_line(20) {
println!("{} ({} chars)", line, len);
}You can also parse the text with letter wrap, which is mainly used for non-textual content, such as game board and similar:
let text = get_text();
let mut text_iter = text.chars();
let mut parser = TextParser::new(&mut text_iter).wrap(Wrap::Letter);
// Reads lines with maximum length 20 until end of the text
while let Some((line, len)) = parser.next_line(20) {
println!("{} ({} chars)", line, len);
}Implementations§
Source§impl<'a> TextParser<'a>
impl<'a> TextParser<'a>
Sourcepub fn new(text: &'a mut dyn Iterator<Item = char>) -> Self
pub fn new(text: &'a mut dyn Iterator<Item = char>) -> Self
Creates new text parser with given text.
§Example
let mut text_iter = "This is a test of termint text parser".chars();
let parser = TextParser::new(&mut text_iter);Sourcepub fn wrap(self, wrap: Wrap) -> Self
pub fn wrap(self, wrap: Wrap) -> Self
Sets wrap mode of the parser.
Default value is Wrap::Word.
§Example
let mut text_iter = "This is a test of termint text parser".chars();
let parser = TextParser::new(&mut text_iter).wrap(Wrap::Letter);Sourcepub fn next_line(&mut self, max_len: usize) -> Option<(String, usize)>
pub fn next_line(&mut self, max_len: usize) -> Option<(String, usize)>
Gets next line from the text.
Returns None when end of the text is reached, otherwise returns line and its length.
§Example
let mut text_iter = "This is a test of termint text parser".chars();
let mut parser = TextParser::new(&mut text_iter).wrap(Wrap::Letter);
// Gets next line from text with maximum length of 20
if let Some((line, len)) = parser.ww_next_line(20) {
println!("{} ({} chars)", line, len);
}Sourcepub fn ww_next_line(&mut self, max_len: usize) -> Option<(String, usize)>
pub fn ww_next_line(&mut self, max_len: usize) -> Option<(String, usize)>
Gets next line from the text using word wrap. Same as calling
next_line with wrap set to Wrap::Word.
Returns None when end of the text is reached, otherwise returns line and its length.
§Example
let mut text_iter = "This is a test of termint text parser".chars();
let mut parser = TextParser::new(&mut text_iter);
// Gets next line from text with maximum length of 20
if let Some((line, len)) = parser.ww_next_line(20) {
println!("{} ({} chars)", line, len);
}Sourcepub fn lw_next_line(&mut self, max_len: usize) -> Option<(String, usize)>
pub fn lw_next_line(&mut self, max_len: usize) -> Option<(String, usize)>
Gets next line from the text using letter wrap. Same as calling
next_line with wrap set to Wrap::Letter.
Returns None when end of the text is reached, otherwise returns line and its length.
§Example
let mut text_iter = "This is a test of termint text parser".chars();
let mut parser = TextParser::new(&mut text_iter);
// Gets next line from text with maximum length of 20
if let Some((line, len)) = parser.lw_next_line(20) {
println!("{} ({} chars)", line, len);
}Sourcepub fn next_word(&mut self) -> TextToken
pub fn next_word(&mut self) -> TextToken
Gets next word from the text, skips leading whitespaces.
§Example
let mut text_iter = "This is a test of termint text parser".chars();
let mut parser = TextParser::new(&mut text_iter);
// Gets next word from text
match parser.next_word() {
TextToken::Text { text, len } => {
println!("{} ({} chars)", text, len)
},
TextToken::Newline => println!("Newline"),
TextToken::End => println!("End of text"),
}