3 releases

0.1.2 Nov 18, 2021
0.1.1 Feb 8, 2021
0.1.0 Feb 8, 2021

#864 in Encoding


Used in biogarden

MIT license

22KB
257 lines

allwords 🦀

build badge codecov crates.io badge Documentation

allwords is a Rust crate that allows you to generate words over a given alphabet.

Word generation can be useful in several scenarios:

  • Pseudo-random data generation (e.g. testing / mocking)
  • Brute forcing of keys / passwords
  • Id or Serial number generation

Install

To install the library add the following lines to your Cargo.toml

[dependencies]
allwords = "0"

Or, if you have cargo add, you can run the following command:

cargo add allwords@0

Sample Usage

The basic idea for using this library is that you create an Alphabet with a set of characters and then you can use it to generate a WordsIterator. You can use the iterator to generate all the possible words over the alphabet.

For instance if you want to generate all the possible words containing "a", "b", "c" with a maximum length of 3 chars:

use allwords::{Alphabet};

let a = Alphabet::from_chars_in_str("abc").unwrap();

let words: Vec<String> = a.all_words(Some(3)).collect();

let expected_words: Vec<String> = [
    "a", "b", "c",
    "aa", "ab", "ac", "ba", "bb", "bc", "ca", "cb", "cc",
    "aaa", "aab", "aac", "aba", "abb", "abc", "aca", "acb", "acc",
    "baa", "bab", "bac", "bba", "bbb", "bbc", "bca", "bcb", "bcc",
    "caa", "cab", "cac", "cba", "cbb", "cbc", "cca", "ccb", "ccc"]
    .iter()
    .map(|s| s.to_string())
    .collect();

assert_eq!(words, expected_words);

WordsIterator

Once you create an alphabet a, there are 4 different ways to get an iterator:

Consult the crate documentation for more details and examples.

Contributing

Everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or suggesting improvements by opening an issue on GitHub.

License

Licensed under MIT License. © Luciano Mammino.

No runtime deps