tracery

Struct Grammar

Source
pub struct Grammar { /* private fields */ }
Expand description

Represents a single, complete tracery grammar.

See the crate-level documentation for a usage overview.

Implementations§

Source§

impl Grammar

Source

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

Creates a new grammar from a JSON grammar string

§Examples
use tracery::Grammar;
let json = r##"{
    "origin": [ "#tool# is #description#!" ],
    "tool": [ "tracery" ],
    "description": [ "fun", "awesome" ]
}"##;
let g = Grammar::from_json(json)?;
Source

pub fn with_default_rule<S: Into<String>>(self, s: S) -> Grammar

Sets a default rule, then returns the modified Grammar

§Examples
use tracery::grammar;
let g = grammar! {
    "start" => "#tool# is #description#!",
    "tool" => "tracery",
    "description" => [ "fun", "awesome" ]
}?.with_default_rule("start");
Source

pub fn set_default_rule<S: Into<String>>(&mut self, s: S)

Sets a default rule

§Examples
use tracery::grammar;
let mut g = grammar! {
    "start" => "#tool# is #description#!",
    "tool" => "tracery",
    "description" => [ "fun", "awesome" ]
}?;
g.set_default_rule("start");
Source

pub fn flatten<R: ?Sized + Rng>(&self, rng: &mut R) -> Result<String>

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;
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())?;
Source

pub fn execute<R>(&mut self, key: &String, rng: &mut R) -> Result<String>
where R: ?Sized + 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;
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())?;

Using a key created during a previous execution:

use tracery::grammar;
// 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())?;

// 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())?;
Source

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>,

Creates a new Grammar from an input map of keys to rule lists

§Examples
let map = hashmap! {
    "origin" => vec![ "#tool# is #description#!" ],
    "tool" => vec![ "tracery" ],
    "description" => vec![ "fun", "awesome" ]
};
let g = tracery::from_map(map)?;

Any object implementing IntoIterator<Item = (Into<String>, Into<Vec<Into<String>>>)> will be accepted by this function, despite its name:

let map = vec![ ("origin", vec![ "#tool# is #description#!" ]),
                ("tool", vec![ "tracery" ]),
                ("description", vec![ "fun", "awesome" ]) ];
let g = tracery::from_map(map)?;

Trait Implementations§

Source§

impl Clone for Grammar

Source§

fn clone(&self) -> Grammar

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Grammar

§

impl !RefUnwindSafe for Grammar

§

impl !Send for Grammar

§

impl !Sync for Grammar

§

impl Unpin for Grammar

§

impl !UnwindSafe for Grammar

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V