Add titles and attributions
This commit is contained in:
23
src/lib.rs
23
src/lib.rs
@@ -5,7 +5,9 @@ use std::collections::HashMap;
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Book {
|
||||
pages: HashMap<String, Page>,
|
||||
current_page: Option<String>
|
||||
current_page: Option<String>,
|
||||
pub title: String,
|
||||
pub attribution: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@@ -47,7 +49,11 @@ pub struct StatChange {
|
||||
peg::parser! {
|
||||
grammar storybook_parser() for str {
|
||||
pub rule story() -> Book
|
||||
= blankline()* ps:page()+ eof() { Book::from(ps) }
|
||||
= blankline()* t:title() a:attribution() ps:page()+ eof() { Book::from((t, a, ps)) }
|
||||
rule title() -> String
|
||||
= t:$((!attribution() [_])+) &attribution() { String::from(t.trim()) }
|
||||
rule attribution() -> String
|
||||
= ("by" / "By" ) _ a:$((!__ [_])+) __ blankline()* { String::from(a.trim()) }
|
||||
rule page() -> Page
|
||||
= h:header() b:body() f:footer() blankline()* {
|
||||
Page {
|
||||
@@ -154,13 +160,15 @@ impl Book {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Page>> for Book {
|
||||
fn from(v: Vec<Page>) -> Book {
|
||||
impl From<(String, String, Vec<Page>)> for Book {
|
||||
fn from((title, attribution, v) : (String, String, Vec<Page>)) -> Book {
|
||||
let it = v.into_iter().map(|p| (p.id.clone(), p));
|
||||
let pages = HashMap::from_iter(it);
|
||||
let mut book = Book {
|
||||
pages,
|
||||
current_page: None,
|
||||
title,
|
||||
attribution
|
||||
};
|
||||
|
||||
book.find("START");
|
||||
@@ -363,6 +371,8 @@ pub mod ffi {
|
||||
mod tests {
|
||||
use super::*;
|
||||
const STORY: &str = r"
|
||||
Dane's Big Day
|
||||
By Austin Lee
|
||||
START
|
||||
|
||||
One day, Dane took a really big shit.
|
||||
@@ -426,10 +436,15 @@ THE END
|
||||
footer: Footer::Ending,
|
||||
}),
|
||||
]),
|
||||
title: "Dane's Big Day".to_string(),
|
||||
attribution: "Austin Lee".to_string(),
|
||||
current_page: None,
|
||||
};
|
||||
let result = Book::new(STORY);
|
||||
|
||||
assert_eq!(expected.title, result.title);
|
||||
assert_eq!(expected.attribution, result.attribution);
|
||||
|
||||
for (key, expected_page) in expected.pages.iter() {
|
||||
let result_page = result.pages.get(key).unwrap();
|
||||
assert_eq!(expected_page, result_page);
|
||||
|
||||
Reference in New Issue
Block a user