Skip to main content

vix_lorem/
lib.rs

1//! Lorem ipsum placeholder text generation for Tools → Insert → Lorem ipsum.
2//!
3//! Deterministic (no randomness): words, sentences, and paragraphs are derived
4//! from a fixed canonical passage so output is stable and testable.
5
6#![warn(clippy::pedantic)]
7#![forbid(unsafe_code)]
8#![deny(missing_docs)]
9
10/// The canonical lorem ipsum passage the generators draw from.
11const PARAGRAPH: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, \
12sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad \
13minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea \
14commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit \
15esse cillum dolore eu fugiat nulla pariatur.";
16
17/// The first `n` words of the passage (at least one), stripped of punctuation
18/// and joined by spaces.
19#[must_use]
20pub fn words(n: usize) -> String {
21    PARAGRAPH
22        .split_whitespace()
23        .map(|w| w.trim_end_matches([',', '.']))
24        .take(n.max(1))
25        .collect::<Vec<_>>()
26        .join(" ")
27}
28
29/// The passage's first sentence (ending in a period).
30#[must_use]
31pub fn sentence() -> String {
32    PARAGRAPH
33        .split_once(". ")
34        .map_or_else(|| PARAGRAPH.to_string(), |(head, _)| format!("{head}."))
35}
36
37/// A full lorem ipsum paragraph.
38#[must_use]
39pub fn paragraph() -> String {
40    PARAGRAPH.to_string()
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn words_takes_the_first_n_without_punctuation() {
49        assert_eq!(words(3), "Lorem ipsum dolor");
50        assert!(!words(5).contains(','), "punctuation stripped");
51        assert_eq!(words(0), "Lorem", "zero clamps to one word");
52    }
53
54    #[test]
55    fn sentence_ends_with_a_single_period() {
56        let s = sentence();
57        assert!(s.ends_with('.'));
58        assert!(!s[..s.len() - 1].contains('.'), "only the final period");
59        assert!(s.starts_with("Lorem ipsum"));
60    }
61
62    #[test]
63    fn paragraph_is_the_full_passage() {
64        assert!(paragraph().contains("consectetur"));
65        assert!(paragraph().contains("pariatur"));
66    }
67}