Add titles and attributions
This commit is contained in:
parent
c2e59d08aa
commit
369f90a68d
23
src/lib.rs
23
src/lib.rs
@ -5,7 +5,9 @@ use std::collections::HashMap;
|
|||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct Book {
|
pub struct Book {
|
||||||
pages: HashMap<String, Page>,
|
pages: HashMap<String, Page>,
|
||||||
current_page: Option<String>
|
current_page: Option<String>,
|
||||||
|
pub title: String,
|
||||||
|
pub attribution: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
@ -47,7 +49,11 @@ pub struct StatChange {
|
|||||||
peg::parser! {
|
peg::parser! {
|
||||||
grammar storybook_parser() for str {
|
grammar storybook_parser() for str {
|
||||||
pub rule story() -> Book
|
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
|
rule page() -> Page
|
||||||
= h:header() b:body() f:footer() blankline()* {
|
= h:header() b:body() f:footer() blankline()* {
|
||||||
Page {
|
Page {
|
||||||
@ -154,13 +160,15 @@ impl Book {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Vec<Page>> for Book {
|
impl From<(String, String, Vec<Page>)> for Book {
|
||||||
fn from(v: Vec<Page>) -> Book {
|
fn from((title, attribution, v) : (String, String, Vec<Page>)) -> Book {
|
||||||
let it = v.into_iter().map(|p| (p.id.clone(), p));
|
let it = v.into_iter().map(|p| (p.id.clone(), p));
|
||||||
let pages = HashMap::from_iter(it);
|
let pages = HashMap::from_iter(it);
|
||||||
let mut book = Book {
|
let mut book = Book {
|
||||||
pages,
|
pages,
|
||||||
current_page: None,
|
current_page: None,
|
||||||
|
title,
|
||||||
|
attribution
|
||||||
};
|
};
|
||||||
|
|
||||||
book.find("START");
|
book.find("START");
|
||||||
@ -363,6 +371,8 @@ pub mod ffi {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
const STORY: &str = r"
|
const STORY: &str = r"
|
||||||
|
Dane's Big Day
|
||||||
|
By Austin Lee
|
||||||
START
|
START
|
||||||
|
|
||||||
One day, Dane took a really big shit.
|
One day, Dane took a really big shit.
|
||||||
@ -426,10 +436,15 @@ THE END
|
|||||||
footer: Footer::Ending,
|
footer: Footer::Ending,
|
||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
|
title: "Dane's Big Day".to_string(),
|
||||||
|
attribution: "Austin Lee".to_string(),
|
||||||
current_page: None,
|
current_page: None,
|
||||||
};
|
};
|
||||||
let result = Book::new(STORY);
|
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() {
|
for (key, expected_page) in expected.pages.iter() {
|
||||||
let result_page = result.pages.get(key).unwrap();
|
let result_page = result.pages.get(key).unwrap();
|
||||||
assert_eq!(expected_page, result_page);
|
assert_eq!(expected_page, result_page);
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
use storybook;
|
|
||||||
use gdnative::prelude::*;
|
use gdnative::prelude::*;
|
||||||
|
|
||||||
fn init(handle: InitHandle){
|
fn init(handle: InitHandle){
|
||||||
@ -84,23 +83,30 @@ impl Book {
|
|||||||
storybook::Footer::Goto(_) => Footer::Goto,
|
storybook::Footer::Goto(_) => Footer::Goto,
|
||||||
storybook::Footer::Choices(choices) => {
|
storybook::Footer::Choices(choices) => {
|
||||||
Footer::Choices(choices.iter().map(|(o, c)| Choice {
|
Footer::Choices(choices.iter().map(|(o, c)| Choice {
|
||||||
option: o.clone(),
|
option: *o,
|
||||||
flavor: c.flavor.clone(),
|
flavor: c.flavor.clone(),
|
||||||
stat_check: match &c.stat_check {
|
stat_check: c.stat_check.as_ref().map(StatCheck::new),
|
||||||
Some(c) => Some(StatCheck::new(c)),
|
stat_change: c.stat_change.as_ref().map(StatChange::new),
|
||||||
None => None,
|
|
||||||
},
|
|
||||||
stat_change: match &c.stat_change {
|
|
||||||
Some(c) => Some(StatChange::new(c)),
|
|
||||||
None => None,
|
|
||||||
},
|
|
||||||
|
|
||||||
}).collect())
|
}).collect())
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
None => Footer::None,
|
None => Footer::None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[export]
|
||||||
|
fn get_title(&self, _owner: &Reference) -> String {
|
||||||
|
match &self._book {
|
||||||
|
Some(b) => b.title.clone(),
|
||||||
|
None => String::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[export]
|
||||||
|
fn get_attribution(&self, _owner: &Reference) -> String {
|
||||||
|
match &self._book {
|
||||||
|
Some(b) => b.attribution.clone(),
|
||||||
|
None => String::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StatCheck {
|
impl StatCheck {
|
||||||
|
Loading…
Reference in New Issue
Block a user