Skip to content

Commit

Permalink
change: Restore lipsum_from_seed & lipsum_words_from_seed (#98)
Browse files Browse the repository at this point in the history
I introduced the default feature `std` which enables the `rand`
package features `std` & `std_rng` for the `thread_rng()` function.
This allows user to use the rand package without adding it with the
right features to own `Cargo.toml`.
  • Loading branch information
kalkin committed Apr 14, 2023
1 parent e584c54 commit 1b5f058
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ rand_chacha = "0.3.1"
[dev-dependencies]
version-sync = "0.9.4"
rand = "0.8.5"

[features]
default = ["std"]
std_rng = ["rand/std", "rand/std_rng"]
62 changes: 62 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,30 @@ pub fn lipsum_with_rng(rng: impl Rng, n: usize) -> String {
LOREM_IPSUM_CHAIN.with(|chain| chain.generate_with_rng_from(rng, n, ("Lorem", "ipsum")))
}

/// Generate `n` words of lorem ipsum text. The output will always start with
/// "Lorem ipsum". The seed makes the sequence deterministic.
///
/// Deterministic sequences are useful for unit tests where you need random but
/// consistent inputs or when users expect an infinitely extendable blind text
/// string that does not change.
///
/// # Examples
///
/// ```
/// use lipsum::lipsum_from_seed;
///
/// assert_eq!(lipsum_from_seed(23, 16),
/// "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim.");
/// ```
///
/// [`LOREM_IPSUM`]: constant.LOREM_IPSUM.html
/// [`lipsum`]: fn.lipsum.html
pub fn lipsum_from_seed(n: usize, seed: u64) -> String {
let rng = ChaCha20Rng::seed_from_u64(seed);
LOREM_IPSUM_CHAIN.with(|chain| chain.generate_with_rng_from(rng, n, ("Lorem", "ipsum")))
}


/// Generate `n` words of lorem ipsum text.
///
/// The text is deterministically sampled from a Markov chain based on
Expand Down Expand Up @@ -499,6 +523,30 @@ pub fn lipsum_words_with_rng(rng: impl Rng, n: usize) -> String {
LOREM_IPSUM_CHAIN.with(|chain| chain.generate_with_rng(rng, n))
}

/// Generate `n` random words of lorem ipsum text. The seed makes the sequence
/// deterministic.
///
/// Deterministic sequences are useful for unit tests where you need random but
/// consistent inputs or when users expect an infinitely extendable blind text
/// string that does not change.
///
/// # Examples
///
/// ```
/// use lipsum::lipsum_words_from_seed;
///
/// assert_eq!(lipsum_words_from_seed(7, 1234),
/// "Anteponant iis, quae recordamur. Stulti autem malorum.");
/// ```
///
/// [`LOREM_IPSUM`]: constant.LOREM_IPSUM.html
/// [`lipsum_words`]: fn.lipsum_words.html
pub fn lipsum_words_from_seed(n: usize, seed: u64) -> String {
let rng = ChaCha20Rng::seed_from_u64(seed);
LOREM_IPSUM_CHAIN.with(|chain| chain.generate_with_rng(rng, n))
}


/// Minimum number of words to include in a title.
const TITLE_MIN_WORDS: usize = 3;
/// Maximum number of words to include in a title.
Expand Down Expand Up @@ -681,4 +729,18 @@ mod tests {
"A b bar a b a b bar a b x y b y x."
);
}

#[test]
fn seed_works() {
assert_eq!(
lipsum_words_from_seed(10, 100_000),
lipsum_words_from_seed(10, 100_000)
);
assert_eq!(lipsum_from_seed(30, 100_000), lipsum_from_seed(30, 100_000));
assert_ne!(
lipsum_words_from_seed(10, 100_000),
lipsum_words_from_seed(10, 100_001)
);
assert_ne!(lipsum_from_seed(30, 100_000), lipsum_from_seed(30, 100_001));
}
}

0 comments on commit 1b5f058

Please sign in to comment.