1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! Rust port of `tracery`
//!
//! This library is a port of https://github.com/galaxykate/tracery, which implements Generative
//! grammars. Given a set of rules, written in a specific syntax, it will generate strings of text.
//!
//! Example:
//!
//! ```ignore
//! let source = r##"
//! {
//!     "origin": ["foo #bar#", "#baz# quux #quuux#"],
//!     "bar": ["bar", "BAR"],
//!     "baz": ["baz", "BaZ", "bAAZ"],
//!     "quuux": ["quick brown fox", "lazy dog"]
//! }
//! "##;
//!
//! let grammar = tracery::from_json(source).unwrap();
//! println!(grammar.flatten()) // => starting from the "origin" rule (which is selected by
//!                             //    default), fills in random
//!                             //    entries from the "bar", "baz", and "quuux" rules,
//!                             //    where called for in the "origin" text.
//! ```
//! or, even shorter:
//!
//! ```ignore
//! let source = r##"
//! {
//!     "origin": ["foo #bar#", "#baz# quux #quuux#"],
//!     "bar": ["bar", "BAR"],
//!     "baz": ["baz", "BaZ", "bAAZ"],
//!     "quuux": ["quick brown fox", "lazy dog"]
//! }"##;
//! tracery::flatten(source).unwrap();
//! ```
//!
//! So, in the example above, we might end up with `"foo bar"` or `"BaZ quux lazy dog"`, etc
//!
//! ## API
//!
//! In the example above, we used `Grammar.flatten`, but that is a convenience function that
//! does the following:
//!
//! ```ignore
//! let grammar = tracery::from_json(source).unwrap();
//! let flattened = grammar.flatten();
//! ```
//!
//! `.from_json` will parse the rule set out into a tree-like structure, and `.flatten` collapses that
//! tree-like structure into a single string.
//!
//! ## More `tracery` syntax
//!
//! Tracery allows for more than just word replacement. You can attach "actions" and "modifiers" to
//! rules as well. There are quite a few modifiers built-in to this library. Here is one:
//!
//! ```ignore
//! let source = r##"
//! {
//!     "origin": ["this word is in plural form: #noun.s#"],
//!     "noun": ["apple", "bear", "cat", "dog", "equine", "fish", "garbage"]
//! }"##;
//!
//! let grammar = tracery::from_json(source).unwrap();
//! println!(grammar.flatten());
//! ```
//!
//! This will generate sentences like:
//!
//! > "this word is in plural form: bears"
//!
//! or
//!
//! > "this word is in plural form: fishes"
//!
//! etc...
//!
//! Actions allow you to, for example, lock in a specific value for a `#tag#`, so that you can refer to it multiple
//! times in your story. Here is an example (modified from @galaxykate's official tutorial
//! http://www.crystalcodepalace.com/traceryTut.html)
//!
//! ```ignore
//! let source = r##"{
//!     "name": ["Arjun","Yuuma","Darcy","Mia","Chiaki","Izzi","Azra","Lina"],
//!     "animal": ["unicorn","raven","sparrow","scorpion","coyote","eagle","owl","lizard","zebra","duck","kitten"],
//!     "mood": ["vexed","indignant","impassioned","wistful","astute","courteous"],
//!     "story": ["#hero# traveled with her pet #heroPet#.  #hero# was never #mood#, for the #heroPet# was always too #mood#."],
//!     "origin": ["#[hero:#name#][heroPet:#animal#]story#"]
//! }"##;
//! ```
//!
//! We see, in the "origin" rule, the use of actions to lock-in the value of `#hero#` and
//! `#heroPet#`, so that we can use those tags in the "story" rule, and know that the same
//! generated value will be used in all cases.

use std::fmt;
use std::error::Error as StdError;
use std::collections::BTreeMap;

mod tag;
mod grammar;
mod parser;

pub use crate::grammar::{Flatten, Grammar};

pub fn from_json<S: AsRef<str>>(s: S) -> Result<Grammar> {
    Grammar::from_json(s)
}

pub fn flatten<S: AsRef<str>>(s: S) -> Result<String> {
    from_json(s)?.flatten(&Grammar::new(), &mut BTreeMap::new())
}

#[derive(Debug, Clone)]
pub enum Error {
    ParseError(String),
    MissingKeyError(String),
    JsonError(String),
}

impl From<serde_json::Error> for Error {
    fn from(e: serde_json::Error) -> Error {
        Error::JsonError(format!("{}", e))
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::ParseError(ref s) => write!(f, "parse error: {}", s),
            Error::MissingKeyError(ref s) => write!(f, "missing key error: {}", s),
            Error::JsonError(ref s) => write!(f, "json error: {}", s),
        }
    }
}

impl StdError for Error {
    fn description(&self) -> &str {
        match *self {
            Error::ParseError(ref s) => s,
            Error::MissingKeyError(ref s) => s,
            Error::JsonError(ref s) => s,
        }
    }
}

pub type Result<T> = ::std::result::Result<T, Error>;

#[cfg(test)]
mod tests {
    use super::from_json;
    use crate::grammar::{Flatten, Grammar};
    use std::collections::BTreeMap;

    #[test]
    fn test_flatten() {
        let source = " { \"origin\": [\"foo #bar#\"], \"bar\": [\"bar\"] } ";
        assert_eq!(super::flatten(source).unwrap(), "foo bar".to_string());
    }

    #[test]
    fn test_with_actions() {
        let source = r##"{
                "name": ["Arjun","Yuuma","Darcy","Mia","Chiaki","Izzi","Azra","Lina"],
                "animal": ["unicorn","raven","sparrow","scorpion","coyote","eagle","owl","lizard","zebra","duck","kitten"],
                "mood": ["vexed","indignant","impassioned","wistful","astute","courteous"],
                "story": ["#hero# traveled with her pet #heroPet#.  #hero# was never #mood#, for the #heroPet# was always too #mood#."],
                "origin": ["#[hero:#name#][heroPet:#animal#]story#"]
            }"##;
        match from_json(source) {
            Ok(g) => {
                g.flatten(&Grammar::new(), &mut BTreeMap::new()).unwrap();
            }
            Err(e) => println!("Error was {}", e),
        };
    }
}