1use std::fmt;
2
3#[derive(Clone, Debug, Eq, PartialEq)]
5pub struct Ending {
6 pub id: String,
7 pub dialogue: String,
8}
9
10impl Ending {
11 pub fn from_str(s: &str) -> Result<Self, crate::Error> {
12 let lines: Vec<&str> = s.lines().collect();
13
14 if lines.is_empty() || !lines[0].starts_with("END ") {
15 return Err(crate::Error::Ending);
16 }
17
18 let id = lines[0].replace("END ", "");
19 let dialogue = lines[1..].join("\n");
20
21 Ok(Ending { id, dialogue })
22 }
23}
24
25impl fmt::Display for Ending {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 write!(f,"END {}\n{}", self.id, self.dialogue)
28 }
29}
30
31#[cfg(test)]
32mod test {
33 use crate::Ending;
34
35 #[test]
36 fn ending_from_string() {
37 assert_eq!(
38 Ending::from_str(include_str!("test-resources/ending")).unwrap(),
39 Ending {
40 id: "a".to_string(),
41 dialogue: "This is a long line of dialogue. Blah blah blah".to_string()
42 }
43 );
44 }
45
46 #[test]
47 fn ending_to_string() {
48 assert_eq!(
49 Ending {
50 id: "7".to_string(),
51 dialogue: "This is another long ending. So long, farewell, etc.".to_string()
52 }.to_string(),
53 "END 7\nThis is another long ending. So long, farewell, etc.".to_string()
54 );
55 }
56}