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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use lazy_static::lazy_static;
use rand::{seq::SliceRandom, Rng};
use std::collections::BTreeMap;
use std::rc::Rc;

use crate::{parser::parse_str, Error, Execute, Result, Rule};

lazy_static! {
    pub(crate) static ref ORIGIN: String = String::from("origin");
}

/// Represents a single, complete tracery grammar.
///
/// See the [`crate-level documentation`] for a usage overview.
///
/// [`crate-level documentation`]: index.html
#[derive(Clone)]
pub struct Grammar {
    map: BTreeMap<String, Vec<Vec<Rule>>>,
    default_rule: String,
    modifier_registry: BTreeMap<String, Rc<dyn Fn(&str) -> String>>,
}

impl Grammar {
    pub(crate) fn get_modifier(&self, modifier: &str) -> Option<&dyn Fn(&str) -> String> {
        self.modifier_registry.get(modifier).map(|x| x.as_ref())
    }

    /// Pushes a new rule onto the rule stack for a given key
    pub(crate) fn push_rule(&mut self, key: String, rule_str: String) {
        use crate::Node;
        use std::collections::btree_map::Entry;
        let rule = vec![Rule::new(vec![Node::from(rule_str)])];
        match self.map.entry(key) {
            Entry::Occupied(mut occ) => {
                let stack = occ.get_mut();
                stack.push(rule);
            }
            Entry::Vacant(vac) => {
                vac.insert(vec![rule]);
            }
        }
    }

    /// Pops a rule off the rule stack for a given key, removing the key
    /// entirely if there are no rules left
    pub(crate) fn pop_rule(&mut self, key: String) {
        use std::collections::btree_map::Entry;
        if let Entry::Occupied(mut occ) = self.map.entry(key) {
            let stack = occ.get_mut();
            if stack.len() < 2 {
                occ.remove_entry();
            } else {
                stack.pop();
            }
        }
    }

    /// Gets a rule with the given key, if it exists
    pub(crate) fn get_rule(&self, key: &str) -> Option<&Vec<Rule>> {
        self.map.get(key).and_then(|stack| stack.last())
    }

    /// Creates a new grammar from a JSON grammar string
    ///
    /// # Examples
    /// ```
    /// use tracery::Grammar;
    /// # use tracery::Result;
    /// # use maplit::hashmap;
    /// # fn main() -> Result<()> {
    /// let json = r##"{
    ///     "origin": [ "#tool# is #description#!" ],
    ///     "tool": [ "tracery" ],
    ///     "description": [ "fun", "awesome" ]
    /// }"##;
    /// let g = Grammar::from_json(json)?;
    /// # let output = g.flatten(&mut rand::thread_rng())?;
    /// # assert!(match output.as_str() {
    /// #     "tracery is fun!" | "tracery is awesome!" => true,
    /// #     _ => false,
    /// # });
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [`Error`]: enum.Error.html
    /// [`Grammar`]: struct.Grammar.html
    #[cfg(feature = "tracery_json")]
    pub fn from_json<S: AsRef<str>>(s: S) -> Result<Grammar> {
        let source: BTreeMap<String, Vec<String>> = serde_json::from_str(s.as_ref())?;
        let mut map: BTreeMap<String, Vec<Vec<Rule>>> = BTreeMap::new();
        for (key, value) in source.into_iter() {
            let rules: Vec<Rule> = value.iter().map(parse_str).collect::<Result<Vec<_>>>()?;
            map.insert(key, vec![rules]);
        }

        Ok(Grammar {
            map,
            default_rule: ORIGIN.clone(),
            modifier_registry: crate::modifiers::get_default_modifiers(),
        })
    }

    /// Sets a default rule, then returns the modified Grammar
    ///
    /// # Examples
    /// ```
    /// use tracery::grammar;
    /// # use tracery::Result;
    /// # fn main() -> Result<()> {
    /// let g = grammar! {
    ///     "start" => "#tool# is #description#!",
    ///     "tool" => "tracery",
    ///     "description" => [ "fun", "awesome" ]
    /// }?.with_default_rule("start");
    /// # let output = g.flatten(&mut rand::thread_rng())?;
    /// # assert!(match output.as_str() {
    /// #     "tracery is fun!" | "tracery is awesome!" => true,
    /// #     _ => false,
    /// # });
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_default_rule<S: Into<String>>(mut self, s: S) -> Grammar {
        self.set_default_rule(s);
        self
    }

    /// Sets a default rule
    ///
    /// # Examples
    /// ```
    /// use tracery::grammar;
    /// # use tracery::Result;
    /// # fn main() -> Result<()> {
    /// let mut g = grammar! {
    ///     "start" => "#tool# is #description#!",
    ///     "tool" => "tracery",
    ///     "description" => [ "fun", "awesome" ]
    /// }?;
    /// g.set_default_rule("start");
    /// # let output = g.flatten(&mut rand::thread_rng())?;
    /// # assert!(match output.as_str() {
    /// #     "tracery is fun!" | "tracery is awesome!" => true,
    /// #     _ => false,
    /// # });
    /// # Ok(())
    /// # }
    /// ```
    pub fn set_default_rule<S: Into<String>>(&mut self, s: S) {
        self.default_rule = s.into();
    }

    /// Attempts to use the Grammar to produce an output String.
    ///
    /// This method clones the Grammar, so any changes made in the course of
    /// producing an output string (such as pushing a new rule onto a stack
    /// using a labeled action such as `[foo:bar]`) will be discarded after
    /// the output is produced.
    ///
    /// If you wish to preserve changes use [`execute`]
    ///
    /// # Examples
    /// ```
    /// use tracery::grammar;
    /// # use tracery::Result;
    /// # fn main() -> Result<()> {
    /// let g = grammar! {
    ///     "origin" => "#tool# is #description#!",
    ///     "tool" => "tracery",
    ///     "description" => [ "fun", "awesome" ]
    /// }?;
    ///
    /// // Generate output (either "tracery is fun!" or "tracery is awesome!")
    /// let output = g.flatten(&mut rand::thread_rng())?;
    /// # assert!(match output.as_str() {
    /// #     "tracery is fun!" | "tracery is awesome!" => true,
    /// #     _ => false,
    /// # });
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [`execute`]: struct.Grammar.html#method.execute
    pub fn flatten<R: ?Sized + Rng>(&self, rng: &mut R) -> Result<String> {
        self.clone().execute(&self.default_rule, rng)
    }

    /// Attempts to use the Grammar to produce an output String, preserving any
    /// side effects that occur while doing so.
    ///
    /// This method produces an output string, but preserves any changes made to
    /// the Grammar in the course of doing so. For instance, if a labeled action
    /// such as `[foo:bar]` is executed, then the Grammar will maintain that
    /// rule after this method returns.
    ///
    /// If you wish to produce an output String without preserving changes, use
    /// [`flatten`].
    /// ```
    /// use tracery::grammar;
    /// # use tracery::Result;
    /// # fn main() -> Result<()> {
    /// let mut g = grammar! {
    ///     "origin" => "#tool# is #description#!",
    ///     "tool" => "tracery",
    ///     "description" => [ "fun", "awesome" ]
    /// }?;
    ///
    /// // Generate output (either "tracery is fun!" or "tracery is awesome!")
    /// let key = String::from("origin");
    /// let output = g.execute(&key, &mut rand::thread_rng())?;
    /// # assert!(match output.as_str() {
    /// #     "tracery is fun!" | "tracery is awesome!" => true,
    /// #     _ => false,
    /// # });
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// Using a key created during a previous execution:
    ///
    /// ```
    /// use tracery::grammar;
    /// # use tracery::Result;
    /// # fn main() -> Result<()> {
    /// // This time, origin has a side-effect: it creates the rule 'aside'
    /// let mut g = grammar! {
    ///     "origin" => "#[aside:Rust is, too]tool# is #description#!",
    ///     "tool" => "tracery",
    ///     "description" => [ "fun", "awesome" ]
    /// }?;
    ///
    /// // Generate output (either "tracery is fun!" or "tracery is awesome!")
    /// let key = String::from("origin");
    /// let output = g.execute(&key, &mut rand::thread_rng())?;
    /// # assert!(match output.as_str() {
    /// #     "tracery is fun!" | "tracery is awesome!" => true,
    /// #     _ => false,
    /// # });
    ///
    /// // The previous call to execute created the 'aside' rule
    /// let key = String::from("aside");
    /// // Generates the string "Rust is, too"
    /// let output = g.execute(&key, &mut rand::thread_rng())?;
    /// # assert!(match output.as_str() {
    /// #     "Rust is, too" => true,
    /// #     _ => false,
    /// # });
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [`flatten`]: struct.Grammar.html#method.flatten
    pub fn execute<R>(&mut self, key: &String, rng: &mut R) -> Result<String>
    where
        R: ?Sized + Rng,
    {
        let rule = match self.map.get(key) {
            Some(rules) => Ok(rules.last().unwrap().choose(rng).unwrap().clone()),
            None => Err(Error::MissingKeyError(key.clone())),
        }?;
        rule.execute(self, rng)
    }

    /// Creates a new Grammar from an input map of keys to rule lists
    ///
    /// # Examples
    /// ```
    /// # use tracery::Result;
    /// # use maplit::hashmap;
    /// # fn main() -> Result<()> {
    /// let map = hashmap! {
    ///     "origin" => vec![ "#tool# is #description#!" ],
    ///     "tool" => vec![ "tracery" ],
    ///     "description" => vec![ "fun", "awesome" ]
    /// };
    /// let g = tracery::from_map(map)?;
    /// # let output = g.flatten(&mut rand::thread_rng())?;
    /// # assert!(match output.as_str() {
    /// #     "tracery is fun!" | "tracery is awesome!" => true,
    /// #     _ => false,
    /// # });
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// Any object implementing
    /// `IntoIterator<Item = (Into<String>, Into<Vec<Into<String>>>)>` will be
    /// accepted by this function, despite its name:
    ///
    /// ```
    /// # use tracery::Result;
    /// # fn main() -> Result<()> {
    /// let map = vec![ ("origin", vec![ "#tool# is #description#!" ]),
    ///                 ("tool", vec![ "tracery" ]),
    ///                 ("description", vec![ "fun", "awesome" ]) ];
    /// let g = tracery::from_map(map)?;
    /// # let output = g.flatten(&mut rand::thread_rng())?;
    /// # assert!(match output.as_str() {
    /// #     "tracery is fun!" | "tracery is awesome!" => true,
    /// #     _ => false,
    /// # });
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_map<I, K, C, S>(iter: I) -> Result<Self>
    where
        I: IntoIterator<Item = (K, C)>,
        K: Into<String>,
        C: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let mut map: BTreeMap<String, Vec<Vec<Rule>>> = BTreeMap::new();

        for (k, v) in iter {
            let rules: Vec<Rule> = v
                .into_iter()
                .map(|x| parse_str(x.into()))
                .collect::<Result<Vec<_>>>()?;
            map.insert(k.into(), vec![rules]);
        }

        Ok(Grammar {
            map,
            default_rule: ORIGIN.clone(),
            modifier_registry: crate::modifiers::get_default_modifiers(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use maplit::hashmap;

    #[test]
    fn flatten_missing_key() -> Result<()> {
        let input = hashmap! {
            "a" => vec![ "a", "aa", "aaa" ]
        };
        let g = Grammar::from_map(input)?;
        let res = g.flatten(&mut rand::thread_rng());
        assert!(matches!(res, Err(Error::MissingKeyError(_))));

        Ok(())
    }

    #[test]
    fn with_default_rule() -> Result<()> {
        let input = hashmap! {
            "a" => vec![ "a", "aa", "aaa" ]
        };
        let g = Grammar::from_map(input)?.with_default_rule("a");
        let res = g.flatten(&mut rand::thread_rng())?;
        assert_eq!(res.chars().next().unwrap(), 'a');

        Ok(())
    }

    #[test]
    fn set_default_rule() -> Result<()> {
        let input = hashmap! {
            "a" => vec![ "a", "aa", "aaa" ]
        };
        let mut g = Grammar::from_map(input)?;
        g.set_default_rule("a");
        let res = g.flatten(&mut rand::thread_rng())?;
        assert_eq!(res.chars().next().unwrap(), 'a');

        Ok(())
    }

    #[test]
    #[cfg(feature = "tracery_json")]
    fn from_json() -> Result<()> {
        let x = r#"{
            "origin": [ "a", "aa" ]
        }"#;
        let g = Grammar::from_json(x)?;
        let res = g.flatten(&mut rand::thread_rng())?;
        assert_eq!(res.chars().next().unwrap(), 'a');

        Ok(())
    }

    #[test]
    fn execute() -> Result<()> {
        let input = hashmap! {
            "origin" => vec!["#[foo:bar]baz#"],
            "baz" => vec!["baz"]
        };
        let mut grammar = Grammar::from_map(input)?;

        // The first invocation should produce the string baz from the rule baz
        let origin = String::from("origin");
        assert_eq!("baz", grammar.execute(&origin, &mut rand::thread_rng())?);

        // It should have also produced a new rule foo with the value bar
        let origin = String::from("foo");
        assert_eq!("bar", grammar.execute(&origin, &mut rand::thread_rng())?);

        Ok(())
    }

    #[test]
    fn execute_function() -> Result<()> {
        let input = hashmap! {
            "origin" => vec!["#setFoo##baz#"],
            "setFoo" => vec!["[foo:bar][bar:#[qux:quux]baz#]"],
            "baz" => vec!["baz"]
        };
        let mut grammar = Grammar::from_map(input)?;

        // The first invocation should produce the string baz from the rule baz
        let origin = String::from("origin");
        assert_eq!("baz", grammar.execute(&origin, &mut rand::thread_rng())?);

        // It should have also produced a new rule foo with the value bar
        let origin = String::from("foo");
        assert_eq!("bar", grammar.execute(&origin, &mut rand::thread_rng())?);

        // ..and a new rule bar with the value baz
        let origin = String::from("bar");
        assert_eq!("baz", grammar.execute(&origin, &mut rand::thread_rng())?);

        // ..aaaand a new rule qux with the value quux
        let origin = String::from("qux");
        assert_eq!("quux", grammar.execute(&origin, &mut rand::thread_rng())?);

        Ok(())
    }

    #[test]
    fn pop_rule() -> Result<()> {
        let input = hashmap! {
            "origin" => vec!["#[foo:baz]foo##[foo:POP]foo#"],
            "foo" => vec!["bar"]
        };
        let mut grammar = Grammar::from_map(input)?;
        assert_eq!(
            "bazbar",
            grammar.execute(&String::from("origin"), &mut rand::thread_rng())?
        );
        Ok(())
    }

    #[test]
    fn pop_and_remove() -> Result<()> {
        let input = hashmap! {
            "origin" => vec!["#foo##popFoo#"],
            "foo" => vec!["bar"],
            "popFoo" => vec!["[foo:POP]"]
        };
        let mut grammar = Grammar::from_map(input)?;
        let mut rng = rand::thread_rng();
        let origin = String::from("origin");
        assert_eq!("bar", grammar.execute(&origin, &mut rng)?);
        let origin = String::from("foo");
        assert!(matches!(
            grammar.execute(&origin, &mut rng),
            Err(Error::MissingKeyError(_))
        ));
        Ok(())
    }
}