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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::entities::line::Line;

/// Structure of novel chapter.
///
/// Chapters are defined below:
/// * Starts from previous chapter or start of document
/// * End with next chapter or end of document
///
pub struct Chapter {
    lines: Vec<Line>,
}

/// Implementation for novel chapter structure
impl Chapter {
    /// Constructor
    ///
    /// # Example
    ///
    /// ```
    /// use naromat::entities::chapter::Chapter;
    ///
    /// Chapter::new("
    /// 我が輩は猫である。名前はまだない。
    /// どこで生まれたのかとんと検討がつかぬ。");
    /// ```
    pub fn new(text: &str) -> Self {
        Self {
            lines: text.split_terminator('\n').map(|line| Line::new(line)).collect(),
        }
    }

    /// Print formatted chapter
    ///
    /// # Example
    ///
    /// ```
    /// use naromat::entities::chapter::Chapter;
    ///
    /// let chapter = Chapter::new("
    /// 我が輩は猫である。名前はまだない。
    /// どこで[生まれた:.]のかとんと[見当:けんとう]がつかぬ。
    /// ");
    /// chapter.print()
    /// ```
    pub fn print(self) {
        for line in self.lines {
            line.print();
        }
    }

    /// Get string of formatted sentence
    ///
    /// # Example
    ///
    /// ```
    /// use naromat::entities::chapter::Chapter;
    ///
    /// let chapter = Chapter::new("
    /// 我が輩は猫である。名前はまだない。
    /// どこで[生まれた:.]のかとんと[見当:けんとう]がつかぬ。
    /// ");
    /// assert_eq!(chapter.get(), "
    ///  我が輩は猫である。名前はまだない。
    ///  どこで|生まれた《・・・・》のかとんと|見当《けんとう》がつかぬ。");
    /// ```
    pub fn get(self) -> String {
        let text: Vec<String> = self.lines.into_iter().map(|line| line.get()).collect();
        text.join("\n")
    }
}
#[cfg(test)]
mod tests {
    use super::Chapter;

    #[test]
    fn get() {
        let source = "我が輩は猫である。名前はまだない。
どこで[生まれた:.]のかとんと[見当:けんとう]がつかぬ。";
        let expected = " 我が輩は猫である。名前はまだない。
 どこで|生まれた《・・・・》のかとんと|見当《けんとう》がつかぬ。";
        let chapter = Chapter::new(&source);
        assert_eq!(chapter.get(), expected);
    }
}