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 178 179 180 181 182 183 184 185 186 187 188 189 190 191
#![deny(missing_docs)] //! 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: //! //! ``` //! # use tracery::{from_json, Flatten, Result}; //! # use std::collections::BTreeMap; //! # fn main() -> Result<()> { //! let source = r##" //! { //! "origin": ["foo #bar#", "#baz# quux #qux#"], //! "bar": ["bar", "BAR"], //! "baz": ["baz", "BaZ", "bAZ"], //! "qux": ["qux", "QUX"] //! } //! "##; //! //! let grammar = tracery::from_json(source).unwrap(); //! // Starting from the "origin" rule, which is selected by default, fills in //! // random entries from the "bar", "baz", and "qux" rules, where called for //! // in the "origin" text: //! let flattened = grammar.flatten(&grammar, &mut BTreeMap::new())?; //! let matches = flattened.eq_ignore_ascii_case("foo bar") || flattened.eq_ignore_ascii_case("baz quux qux"); //! assert!(matches); //! # Ok(()) //! # } //! ``` //! or, even shorter: //! //! ``` //! # use tracery::{flatten, Flatten, Result}; //! # use std::collections::BTreeMap; //! # fn main() -> Result<()> { //! let source = r##" //! { //! "origin": ["foo #bar#", "#baz# quux #qux#"], //! "bar": ["bar", "BAR"], //! "baz": ["baz", "BaZ", "bAZ"], //! "qux": ["qux", "QUX"] //! } //! "##; //! let flattened = tracery::flatten(source)?; //! let matches = flattened.eq_ignore_ascii_case("foo bar") || flattened.eq_ignore_ascii_case("baz quux qux"); //! assert!(matches); //! # Ok(()) //! # } //! ``` //! //! 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: //! //! ``` //! # use tracery::{from_json, Flatten, Grammar, Result}; //! # use std::collections::BTreeMap; //! # fn main() -> Result<()> { //! let grammar = tracery::from_json(r##"{ //! "origin": [ "#foo# is #bar#" ], //! "foo": [ "tracery" ], //! "bar": [ "fun" ] //! }"##)?; //! let flattened = grammar.flatten(&grammar, &mut BTreeMap::new())?; //! assert_eq!(flattened, "tracery is fun"); //! # Ok(()) //! # } //! ``` //! //! `.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: //! //! ``` //! # use tracery::{from_json, Flatten, Grammar, Result}; //! # use std::collections::BTreeMap; //! # fn main() -> Result<()> { //! let source = r##" //! { //! "origin": ["this word is in plural form: #noun.s#"], //! "noun": ["apple"] //! }"##; //! //! let grammar = tracery::from_json(source)?; //! let flattened = grammar.flatten(&grammar, &mut BTreeMap::new())?; //! assert_eq!("this word is in plural form: apples", flattened); //! # Ok(()) //! # } //! ``` //! //! 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) //! //! ``` //! # use tracery::{flatten, Result}; //! # fn main() -> Result<()> { //! 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#"] //! }"##; //! println!("{}", tracery::flatten(source)?); //! # Ok(()) //! # } //! ``` //! //! 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::collections::BTreeMap; mod error; pub use crate::error::Error; mod flatten; pub use crate::flatten::Flatten; mod grammar; pub use crate::grammar::Grammar; mod node; pub use crate::node::Node; mod parser; mod rule; pub use crate::rule::Rule; mod tag; pub use crate::tag::Tag; /// Creates a new grammar from a JSON grammar string pub fn from_json<S: AsRef<str>>(s: S) -> Result<Grammar> { Grammar::from_json(s) } /// Creates a new grammar from a JSON grammar string, then uses it to create a /// random output string pub fn flatten<S: AsRef<str>>(s: S) -> Result<String> { from_json(s)?.flatten(&Grammar::new(), &mut BTreeMap::new()) } /// A convenience type for a `Result` of `T` or [`Error`] /// /// [`Error`]: enum.Error.html pub type Result<T> = ::std::result::Result<T, Error>; #[cfg(test)] mod tests { use super::from_json; use crate::grammar::Grammar; use crate::Flatten; 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), }; } #[test] fn malformed_json() { let input = r#"{ "a": ["a"],}"#; let res = from_json(input); assert!(matches!(res, Err(crate::Error::JsonError(_)))); } }