[][src]Struct genere::Generator

pub struct Generator { /* fields omitted */ }

Generator. Main structure of this library.

The generator is used to add symbols and their replacement grammar, either directly with the add method, or using JSON syntax with add_json.

Symbols can then be instantiated with the instantiate or instantiate_with_seed methods.

It is also possible to use the msg method to quickly transform a message that uses elements added to the generator.

Methods

impl Generator[src]

pub fn new() -> Self[src]

Creates a new, empty Generator.

pub fn add_json(&mut self, json: &str) -> Result<()>[src]

Adds a replacement grammar using JSON format.

pub fn add(&mut self, symbol: &str, content: &[&str]) -> Result<()>[src]

Adds a replacement grammar that will replace given symbol by one of those elements.

Arguments

  • symbol: the name that will be used to accessed the content. It is converted to lowercase before being added to the Generator.
  • content: a list of possible replacements for the symbol, that will be chosen randomly when instantiated. Note that it can contain special marking to refer to other symbol, gender replacements and so on.

pub fn add_move(&mut self, symbol: String, content: Vec<String>) -> Result<()>[src]

Similar to add, but consume the arguments instead of taking a reference.

pub fn set_gender(&mut self, symbol: &str, gender: Gender)[src]

Sets a symbol to a gender

pub fn instantiate(&self, symbol: &str) -> Result<String>[src]

Instantiate a replacement symbol

pub fn msg<S>(&self, s: S, v: &[(&str, &str)]) -> Result<String> where
    S: Into<String>, 
[src]

Instantiate a single message without adding it as a symbol

Sometimes you want to simply generate a message without having to add it to the generator (via add or add_json). This method allows you to do just that. You can optionally pass a set of symbols and their replacement string. While random choice is not supported for these values, you can use the rest of Genere syntax for gender and capitalization.

Arguments

  • s: a string (or &str) containing the text you want to display, which can used the {symbol} syntax to expand other symbols to their replacements.
  • v: a list of pairs containing symbols and replacements values (can be empty).

Example

use genere::Generator;
let gen = Generator::new();
let s = gen.msg("Our hero, {name}. He/She[name] uses a {weapon}.",
           &[("name", "John[m]"),
           ("weapon", "sword")]).unwrap();
assert_eq!(&s, "Our hero, John. He uses a sword.");

pub fn instantiate_from_seed(&self, symbol: &str, seed: u64) -> Result<String>[src]

Instantiate a replacement symbol using a fixed seed.

Useful if you want deterministic behaviour.

Auto Trait Implementations

impl Send for Generator

impl Sync for Generator

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.