inkling/story/
utils.rs

1//! Utilities for story content.
2
3use crate::story::types::LineBuffer;
4
5/// Read all text from lines in a buffer into a single string and return it.
6///
7/// # Examples
8/// ```
9/// # use inkling::{copy_lines_into_string, read_story_from_string};
10/// let content = "\
11/// Gamle gode Väinämöinen
12/// rustade sig nu att resa
13/// bort till kyligare trakter
14/// till de dunkla Nordanlanden.
15/// ";
16///
17/// let mut story = read_story_from_string(content).unwrap();
18/// let mut line_buffer = Vec::new();
19///
20/// story.start();
21/// story.resume(&mut line_buffer);
22///
23/// let text = copy_lines_into_string(&line_buffer);
24/// assert_eq!(&text, content);
25/// ```
26pub fn copy_lines_into_string(line_buffer: &LineBuffer) -> String {
27    line_buffer
28        .iter()
29        .map(|line| line.text.clone())
30        .collect::<Vec<_>>()
31        .join("")
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    use crate::story::Line;
39
40    #[test]
41    fn string_from_line_buffer_joins_without_extra_newlines() {
42        let lines = vec![
43            Line {
44                text: "Start of line, ".to_string(),
45                tags: Vec::new(),
46            },
47            Line {
48                text: "end of line without new lines".to_string(),
49                tags: Vec::new(),
50            },
51        ];
52
53        assert_eq!(
54            &copy_lines_into_string(&lines),
55            "Start of line, end of line without new lines"
56        );
57    }
58}