bitsy-parser/src/ending.rs

59 lines
1.4 KiB
Rust
Raw Normal View History

2020-04-29 20:17:29 +00:00
use std::fmt;
use std::error::Error;
use std::str::FromStr;
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-04-29 20:17:29 +00:00
impl Error for Ending {}
impl FromStr for Ending {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let lines: Vec<&str> = s.lines().collect();
let id = lines[0].replace("END ", "").to_string();
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 {
use crate::Ending;
2020-04-29 20:17:29 +00:00
use std::str::FromStr;
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
}