2020-04-29 20:17:29 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
2020-04-12 12:52:36 +00:00
|
|
|
// same as a dialogue basically
|
2020-05-31 15:12:23 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2020-04-12 12:52:36 +00:00
|
|
|
pub struct Ending {
|
2020-06-24 12:05:32 +00:00
|
|
|
pub id: String,
|
|
|
|
pub dialogue: String,
|
2020-04-12 12:52:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 11:27:54 +00:00
|
|
|
impl Ending {
|
|
|
|
pub fn from_str(s: &str) -> Result<Self, crate::Error> {
|
|
|
|
let lines: Vec<&str> = s.lines().collect();
|
2020-04-29 20:17:29 +00:00
|
|
|
|
2020-10-18 11:27:54 +00:00
|
|
|
if lines.is_empty() || !lines[0].starts_with("END ") {
|
|
|
|
return Err(crate::Error::Ending);
|
|
|
|
}
|
2020-04-29 20:17:29 +00:00
|
|
|
|
2020-10-15 22:54:23 +00:00
|
|
|
let id = lines[0].replace("END ", "");
|
2020-04-23 05:57:07 +00:00
|
|
|
let dialogue = lines[1..].join("\n");
|
2020-04-12 12:52:36 +00:00
|
|
|
|
2020-04-29 20:17:29 +00:00
|
|
|
Ok(Ending { id, dialogue })
|
2020-04-12 12:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 20:17:29 +00:00
|
|
|
impl fmt::Display for Ending {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f,"END {}\n{}", self.id, self.dialogue)
|
2020-04-12 12:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2020-10-09 10:57:02 +00:00
|
|
|
use crate::Ending;
|
2020-04-12 12:52:36 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
2020-07-26 11:37:41 +00:00
|
|
|
fn ending_from_string() {
|
2020-04-19 07:13:55 +00:00
|
|
|
assert_eq!(
|
2020-04-29 20:17:29 +00:00
|
|
|
Ending::from_str(include_str!("test-resources/ending")).unwrap(),
|
2020-04-19 07:13:55 +00:00
|
|
|
Ending {
|
|
|
|
id: "a".to_string(),
|
|
|
|
dialogue: "This is a long line of dialogue. Blah blah blah".to_string()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-26 11:37:41 +00:00
|
|
|
fn ending_to_string() {
|
2020-04-19 07:13:55 +00:00
|
|
|
assert_eq!(
|
|
|
|
Ending {
|
|
|
|
id: "7".to_string(),
|
|
|
|
dialogue: "This is another long ending. So long, farewell, etc.".to_string()
|
|
|
|
}.to_string(),
|
|
|
|
"END 7\nThis is another long ending. So long, farewell, etc.".to_string()
|
|
|
|
);
|
|
|
|
}
|
2020-04-12 12:52:36 +00:00
|
|
|
}
|