1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use super::story::LineBuffer;

/// Read all text from lines in a buffer into a single string and return it.
///
/// # Examples
/// ```
/// # use inkling::{copy_lines_into_string, read_story_from_string};
/// let content = "\
/// Gamle gode Väinämöinen
/// rustade sig nu att resa
/// bort till kyligare trakter
/// till de dunkla Nordanlanden.
/// ";
///
/// let mut story = read_story_from_string(content).unwrap();
/// let mut line_buffer = Vec::new();
///
/// story.start(&mut line_buffer);
///
/// let text = copy_lines_into_string(&line_buffer);
/// assert_eq!(&text, content);
/// ```
pub fn copy_lines_into_string(line_buffer: &LineBuffer) -> String {
    line_buffer
        .iter()
        .map(|line| line.text.clone())
        .collect::<Vec<_>>()
        .join("")
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::story::Line;

    #[test]
    fn string_from_line_buffer_joins_without_extra_newlines() {
        let lines = vec![
            Line {
                text: "Start of line, ".to_string(),
                tags: Vec::new(),
            },
            Line {
                text: "end of line without new lines".to_string(),
                tags: Vec::new(),
            },
        ];

        assert_eq!(
            &copy_lines_into_string(&lines),
            "Start of line, end of line without new lines"
        );
    }
}