[−][src]Struct genere::Generator
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 theGenerator
.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]
S: Into<String>,
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
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,