renderreport 0.3.0

Data-driven report generation with Typst as embedded render engine — no CLI dependency
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! WordSearch component generator
//!
//! Generates a parameter-driven word search puzzle grid with word placement logic,
//! random letter fill, and optional solution tracking.

use super::Component;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

/// Grid cell model passed to Typst
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WordSearchCell {
    /// Character at this cell
    pub char: String,
    /// Whether this cell is part of a hidden word
    pub is_solution: bool,
}

/// Simple deterministic PRNG (Xorshift64) for reproducible grid generation
struct SimpleRng {
    state: u64,
}

impl SimpleRng {
    fn new(seed: u64) -> Self {
        Self {
            state: if seed == 0 { 0x853c49e6748fea9b } else { seed },
        }
    }

    fn next_u64(&mut self) -> u64 {
        let mut x = self.state;
        x ^= x << 13;
        x ^= x >> 7;
        x ^= x << 17;
        self.state = x;
        x
    }

    fn next_usize(&mut self, max: usize) -> usize {
        if max == 0 {
            0
        } else {
            // Reduce modulo `max` while still widened, so the final cast to usize
            // only ever narrows a value already known to fit (< max).
            (self.next_u64() % max as u64) as usize
        }
    }

    fn random_char(&mut self) -> char {
        // idx is in 0..26 by construction, always fits in u8.
        let idx = self.next_usize(26) as u8;
        (b'A' + idx) as char
    }
}

/// Direction vector for word placement
#[derive(Debug, Clone, Copy)]
struct Direction {
    dx: isize,
    dy: isize,
}

impl Direction {
    const RIGHT: Self = Self { dx: 1, dy: 0 };
    const DOWN: Self = Self { dx: 0, dy: 1 };
    const DOWN_RIGHT: Self = Self { dx: 1, dy: 1 };
    const UP_RIGHT: Self = Self { dx: 1, dy: -1 };
    const LEFT: Self = Self { dx: -1, dy: 0 };
    const UP: Self = Self { dx: 0, dy: -1 };
    const UP_LEFT: Self = Self { dx: -1, dy: -1 };
    const DOWN_LEFT: Self = Self { dx: -1, dy: 1 };
}

/// WordSearch component builder and generator
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WordSearch {
    /// Optional title for the puzzle
    pub title: Option<String>,
    /// Optional description / instructions
    pub description: Option<String>,
    /// Words to hide in the puzzle grid
    pub words: Vec<String>,
    /// Grid width in cells
    pub width: usize,
    /// Grid height in cells
    pub height: usize,
    /// Allow diagonal word placement
    pub allow_diagonal: bool,
    /// Allow reverse (right-to-left, bottom-to-top) placement
    pub allow_reverse: bool,
    /// Seed for random placement (for reproducible PDFs)
    pub seed: Option<u64>,
    /// Whether to highlight the solution cells
    pub show_solution: bool,
    /// Number of columns for displaying the word list
    pub columns_word_list: usize,
    /// Language for the (localized) word list label — "de", "en", "fr", or "es".
    /// Unrecognized codes fall back to German.
    pub language: String,
    /// Optional translation for each entry in `words`, matched by index.
    /// Shorter than `words`, or containing `None`, means "no translation" for
    /// that word.
    pub translations: Vec<Option<String>>,
}

impl Default for WordSearch {
    fn default() -> Self {
        Self {
            title: Some("Wortsuchrätsel".into()),
            description: Some("Finde alle versteckten Wörter im Buchstabengitter!".into()),
            words: Vec::new(),
            width: 12,
            height: 12,
            allow_diagonal: true,
            allow_reverse: false,
            seed: Some(42),
            show_solution: false,
            columns_word_list: 3,
            language: "de".into(),
            translations: Vec::new(),
        }
    }
}

/// Word list heading per language (unrecognized codes fall back to German).
fn word_list_label(language: &str) -> &'static str {
    match language {
        "en" => "Words to find",
        "fr" => "Mots à trouver",
        "es" => "Palabras a encontrar",
        _ => "Zu suchende Wörter",
    }
}

/// Uppercase + strip non-alphanumeric characters, matching the cleanup
/// applied to placed words in `to_data()` — so translation lookups by
/// cleaned word still hit.
fn clean_word(word: &str) -> String {
    word.to_uppercase()
        .chars()
        .filter(|c| c.is_alphanumeric())
        .collect()
}

impl WordSearch {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn builder() -> WordSearchBuilder {
        WordSearchBuilder::new()
    }
}

/// Builder pattern for `WordSearch`
pub struct WordSearchBuilder {
    inner: WordSearch,
}

impl WordSearchBuilder {
    pub fn new() -> Self {
        Self {
            inner: WordSearch::default(),
        }
    }

    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.inner.title = Some(title.into());
        self
    }

    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.inner.description = Some(description.into());
        self
    }

    pub fn words<I, S>(mut self, words: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.inner.words = words.into_iter().map(Into::into).collect();
        self
    }

    pub fn grid_size(mut self, width: usize, height: usize) -> Self {
        self.inner.width = width.max(3);
        self.inner.height = height.max(3);
        self
    }

    pub fn allow_diagonal(mut self, allow: bool) -> Self {
        self.inner.allow_diagonal = allow;
        self
    }

    pub fn allow_reverse(mut self, allow: bool) -> Self {
        self.inner.allow_reverse = allow;
        self
    }

    pub fn seed(mut self, seed: u64) -> Self {
        self.inner.seed = Some(seed);
        self
    }

    pub fn show_solution(mut self, show: bool) -> Self {
        self.inner.show_solution = show;
        self
    }

    pub fn columns_word_list(mut self, cols: usize) -> Self {
        self.inner.columns_word_list = cols.max(1);
        self
    }

    pub fn language(mut self, language: impl Into<String>) -> Self {
        self.inner.language = language.into();
        self
    }

    pub fn translations<I>(mut self, translations: I) -> Self
    where
        I: IntoIterator<Item = Option<String>>,
    {
        self.inner.translations = translations.into_iter().collect();
        self
    }

    pub fn build(self) -> crate::Result<WordSearch> {
        Ok(self.inner)
    }
}

impl Component for WordSearch {
    fn component_id(&self) -> &str {
        "word-search"
    }

    fn to_data(&self) -> serde_json::Value {
        let mut rng = SimpleRng::new(self.seed.unwrap_or(42));
        let width = self.width;
        let height = self.height;

        let mut grid: Vec<Vec<Option<char>>> = vec![vec![None; width]; height];
        let mut solution_cells: HashSet<(usize, usize)> = HashSet::new();

        // Clean & prepare words (uppercase, ascii/german chars)
        let mut clean_words: Vec<String> = self
            .words
            .iter()
            .map(|w| {
                w.to_uppercase()
                    .chars()
                    .filter(|c| c.is_alphanumeric())
                    .collect::<String>()
            })
            .filter(|w| !w.is_empty())
            .collect();

        // Sort by length descending for better placement density
        clean_words.sort_by(|a, b| b.len().cmp(&a.len()));

        // Determine available directions
        let mut directions = vec![Direction::RIGHT, Direction::DOWN];
        if self.allow_diagonal {
            directions.push(Direction::DOWN_RIGHT);
            directions.push(Direction::UP_RIGHT);
        }
        if self.allow_reverse {
            directions.push(Direction::LEFT);
            directions.push(Direction::UP);
            if self.allow_diagonal {
                directions.push(Direction::UP_LEFT);
                directions.push(Direction::DOWN_LEFT);
            }
        }

        // Place words into grid
        let mut placed_words = Vec::new();
        for word in &clean_words {
            let chars: Vec<char> = word.chars().collect();
            let len = chars.len();

            // Try multiple random attempts to place word
            for _ in 0..300 {
                let dir = directions[rng.next_usize(directions.len())];
                let start_col = rng.next_usize(width);
                let start_row = rng.next_usize(height);

                let end_col = start_col as isize + dir.dx * (len as isize - 1);
                let end_row = start_row as isize + dir.dy * (len as isize - 1);

                if end_col >= 0
                    && end_col < width as isize
                    && end_row >= 0
                    && end_row < height as isize
                {
                    // Position of the i-th letter of the word, in [0, len). Safe to cast
                    // back to usize: bounded between (start_col, start_row) and
                    // (end_col, end_row), both already checked to be within the grid.
                    let cell_at = |i: usize| -> (usize, usize) {
                        let c = start_col as isize + dir.dx * i as isize;
                        let r = start_row as isize + dir.dy * i as isize;
                        (c as usize, r as usize)
                    };

                    // Check if placement is valid (cells empty or match char)
                    let mut fits = true;
                    for (i, &ch) in chars.iter().enumerate() {
                        let (c, r) = cell_at(i);
                        if let Some(existing) = grid[r][c] {
                            if existing != ch {
                                fits = false;
                                break;
                            }
                        }
                    }

                    if fits {
                        // Place characters and record solution
                        for (i, &ch) in chars.iter().enumerate() {
                            let (c, r) = cell_at(i);
                            grid[r][c] = Some(ch);
                            solution_cells.insert((c, r));
                        }
                        placed_words.push(word.clone());
                        break;
                    }
                }
            }
        }

        // Fill remaining empty cells with random letters
        let rendered_grid: Vec<Vec<WordSearchCell>> = (0..height)
            .map(|r| {
                (0..width)
                    .map(|c| {
                        let ch = grid[r][c].unwrap_or_else(|| rng.random_char());
                        let is_solution = solution_cells.contains(&(c, r));
                        WordSearchCell {
                            char: ch.to_string(),
                            is_solution,
                        }
                    })
                    .collect()
            })
            .collect();

        // Use original words or placed clean words
        let display_words = if placed_words.is_empty() {
            self.words.clone()
        } else {
            placed_words
        };

        // Look up each displayed word's translation by its cleaned form, so
        // the mapping survives the uppercasing/sorting/filtering above.
        let translation_by_clean_word: HashMap<String, String> = self
            .words
            .iter()
            .zip(self.translations.iter().cloned().chain(std::iter::repeat(None)))
            .filter_map(|(word, translation)| {
                translation.map(|t| (clean_word(word), t))
            })
            .collect();

        let word_entries: Vec<serde_json::Value> = display_words
            .iter()
            .map(|w| {
                serde_json::json!({
                    "text": w,
                    "translation": translation_by_clean_word.get(&clean_word(w)),
                })
            })
            .collect();

        serde_json::json!({
            "title": self.title,
            "description": self.description,
            "width": width,
            "height": height,
            "grid": rendered_grid,
            "words": word_entries,
            "show_solution": self.show_solution,
            "columns_word_list": self.columns_word_list,
            "word_list_label": word_list_label(&self.language),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_wordsearch_builder_defaults() {
        let ws = WordSearch::builder()
            .words(vec!["RUST", "TYPST"])
            .grid_size(10, 10)
            .build()
            .unwrap();

        assert_eq!(ws.component_id(), "word-search");
        assert_eq!(ws.width, 10);
        assert_eq!(ws.height, 10);
        assert_eq!(ws.words.len(), 2);
    }

    #[test]
    fn test_wordsearch_grid_generation() {
        let ws = WordSearch::builder()
            .words(vec!["APPLE", "BANANA", "CHERRY"])
            .grid_size(8, 8)
            .seed(12345)
            .build()
            .unwrap();

        let data = ws.to_data();
        assert_eq!(data["width"], 8);
        assert_eq!(data["height"], 8);

        let grid = data["grid"].as_array().expect("grid should be array");
        assert_eq!(grid.len(), 8);
        assert_eq!(grid[0].as_array().unwrap().len(), 8);

        // Check solution cells are present
        let mut solution_count = 0;
        for row in grid {
            for cell in row.as_array().unwrap() {
                if cell["is_solution"].as_bool().unwrap_or(false) {
                    solution_count += 1;
                }
            }
        }
        assert!(solution_count > 0);
    }

    #[test]
    fn test_wordsearch_deterministic_seed() {
        let ws1 = WordSearch::builder()
            .words(vec!["ALPHA", "BETA", "GAMMA"])
            .seed(999)
            .build()
            .unwrap();
        let ws2 = WordSearch::builder()
            .words(vec!["ALPHA", "BETA", "GAMMA"])
            .seed(999)
            .build()
            .unwrap();

        assert_eq!(ws1.to_data(), ws2.to_data());
    }
}