hauchiwa 0.15.0

Flexible static website generator library with incremental rebuilds and cached image optimization
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
use std::borrow::Cow;
use std::collections::{BTreeMap, HashSet};
use std::hash::Hash;
use std::marker::PhantomData;

use petgraph::graph::NodeIndex;

use crate::Many;
use crate::core::{Blake3Hasher, Dynamic, Store, TaskContext};
use crate::engine::{
    Dependencies, Map, Provenance, TrackerState, Tracking, TypedCoarse, TypedFine,
};

/// Squash dependencies into one output
/// Dependencies -> One<R>
pub(crate) struct NodeGather<G, R, D, F>
where
    G: Send + Sync,
    R: Send + Sync + 'static,
    D: Dependencies,
    F: for<'a> Fn(&TaskContext<'a, G>, D::Output<'a>) -> anyhow::Result<R> + Send + Sync,
{
    pub name: Cow<'static, str>,
    pub dependencies: D,
    pub callback: F,
    pub _phantom: PhantomData<G>,
}

impl<G, R, D, F> TypedCoarse<G> for NodeGather<G, R, D, F>
where
    G: Send + Sync + 'static,
    R: Send + Sync + 'static,
    D: Dependencies + Send + Sync,
    F: for<'a> Fn(&TaskContext<'a, G>, D::Output<'a>) -> anyhow::Result<R> + Send + Sync + 'static,
{
    type Output = R;

    fn get_name(&self) -> String {
        self.name.to_string()
    }

    fn dependencies(&self) -> Vec<NodeIndex> {
        self.dependencies.dependencies()
    }

    fn get_watched(&self) -> Vec<camino::Utf8PathBuf> {
        vec![]
    }

    fn execute(
        &self,
        context: &TaskContext<G>,
        _: &mut Store,
        dependencies: &[Dynamic],
    ) -> anyhow::Result<(Tracking, Self::Output)> {
        let (tracking, dependencies) = self.dependencies.resolve(dependencies);
        let output = (self.callback)(context, dependencies)?;
        Ok((tracking, output))
    }

    fn is_valid(
        &self,
        old_tracking: &[Option<TrackerState>],
        new_outputs: &[Dynamic],
        updated_nodes: &HashSet<NodeIndex>,
    ) -> bool {
        self.dependencies
            .is_valid(old_tracking, new_outputs, updated_nodes)
    }
}

/// Explode dependencies into multiple outputs
/// Dependencies -> Many<R>
///
/// Constraints:
/// - R must be Hash, because it will be used for tracking
pub(crate) struct NodeScatter<G, R, D, F>
where
    G: Send + Sync,
    R: Send + Sync + std::hash::Hash + 'static,
    D: Dependencies,
    F: for<'a> Fn(&TaskContext<'a, G>, D::Output<'a>) -> anyhow::Result<Vec<(String, R)>>
        + Send
        + Sync,
{
    pub name: Cow<'static, str>,
    pub dependencies: D,
    pub callback: F,
    pub _phantom: PhantomData<G>,
}

impl<G, R, D, F> TypedFine<G> for NodeScatter<G, R, D, F>
where
    G: Send + Sync + 'static,
    R: Send + Sync + Hash + 'static,
    D: Dependencies + Send + Sync,
    F: for<'a> Fn(&TaskContext<'a, G>, D::Output<'a>) -> anyhow::Result<Vec<(String, R)>>
        + Send
        + Sync
        + 'static,
{
    type Output = R;

    fn get_name(&self) -> String {
        self.name.to_string()
    }

    fn dependencies(&self) -> Vec<NodeIndex> {
        self.dependencies.dependencies()
    }

    fn get_watched(&self) -> Vec<camino::Utf8PathBuf> {
        vec![]
    }

    fn execute(
        &self,
        context: &TaskContext<G>,
        _: &mut Store,
        dependencies: &[Dynamic],
        _old_output: Option<&Dynamic>,
        _updated_nodes: &HashSet<NodeIndex>,
    ) -> anyhow::Result<(Tracking, Map<Self::Output>)> {
        let (tracking, inputs) = self.dependencies.resolve(dependencies);

        let items = (self.callback)(context, inputs)?;

        let mut map = std::collections::BTreeMap::new();

        for (key, item) in items {
            let hash = {
                let mut hasher = Blake3Hasher::default();
                item.hash(&mut hasher);
                hasher.into()
            };

            let provenance = Provenance(hash);

            map.insert(key.into(), (item, provenance));
        }

        Ok((tracking, Map { map, dirty: false }))
    }

    fn is_valid(
        &self,
        old_tracking: &[Option<TrackerState>],
        new_outputs: &[Dynamic],
        updated_nodes: &HashSet<NodeIndex>,
    ) -> bool {
        self.dependencies
            .is_valid(old_tracking, new_outputs, updated_nodes)
    }
}

/// Map each input to a single output, with additional (side) dependencies
/// Many<T> -> Many<R>
pub(crate) struct NodeMap<T, G, R, D, F>
where
    T: Send + Sync + 'static,
    G: Send + Sync + 'static,
    R: Send + Sync + Clone + 'static,
    D: Dependencies,
    F: for<'a> Fn(&TaskContext<'a, G>, &T, D::Output<'a>) -> anyhow::Result<R> + Send + Sync,
{
    pub name: Cow<'static, str>,
    pub dep_primary: Many<T>,
    pub dep_secondary: D,
    pub callback: F,
    pub _phantom: PhantomData<G>,
}

impl<T, G, R, D, F> TypedFine<G> for NodeMap<T, G, R, D, F>
where
    T: Send + Sync + 'static,
    G: Send + Sync + 'static,
    R: Send + Sync + Clone + 'static,
    D: Dependencies + Send + Sync,
    F: for<'a> Fn(&TaskContext<'a, G>, &T, D::Output<'a>) -> anyhow::Result<R> + Send + Sync,
{
    type Output = R;

    fn get_name(&self) -> String {
        self.name.to_string()
    }

    fn dependencies(&self) -> Vec<NodeIndex> {
        let mut deps = Vec::new();
        deps.extend(self.dep_primary.dependencies());
        deps.extend(self.dep_secondary.dependencies());
        deps
    }

    fn get_watched(&self) -> Vec<camino::Utf8PathBuf> {
        vec![]
    }

    fn execute(
        &self,
        context: &TaskContext<G>,
        _: &mut Store,
        dependencies: &[Dynamic],
        old_output: Option<&Dynamic>,
        updated_nodes: &HashSet<NodeIndex>,
    ) -> anyhow::Result<(Tracking, Map<Self::Output>)> {
        // We assume the first dependency is the primary Many<T>
        let input_map = dependencies[0].downcast_ref::<Map<T>>().unwrap();

        let mut forced_dirty = false;
        for dep_idx in self.dep_secondary.dependencies() {
            if updated_nodes.contains(&dep_idx) {
                forced_dirty = true;
                break;
            }
        }

        let old_map = if !forced_dirty {
            old_output.and_then(|d| d.downcast_ref::<Map<Self::Output>>())
        } else {
            None
        };

        let mut result_map = BTreeMap::new();
        for (key, (input, provenance)) in &input_map.map {
            // If not forced dirty, and we have old output, and provenance matches
            if let Some(old_map) = old_map
                && let Some((old_item, old_provenance)) = old_map.map.get(key)
                && old_provenance == provenance
            {
                result_map.insert(key.clone(), (old_item.clone(), *provenance));
            } else {
                let (_, deps) = self.dep_secondary.resolve(&dependencies[1..]);
                let output = (self.callback)(context, input, deps)?;
                result_map.insert(key.clone(), (output, *provenance));
            }
        }

        Ok((
            Tracking::default(),
            Map {
                map: result_map,
                dirty: forced_dirty,
            },
        ))
    }

    fn is_valid(
        &self,
        _old_tracking: &[Option<TrackerState>],
        _new_outputs: &[Dynamic],
        updated_nodes: &HashSet<NodeIndex>,
    ) -> bool {
        for dep in self.dep_primary.dependencies() {
            if updated_nodes.contains(&dep) {
                return false;
            }
        }

        for dep in self.dep_secondary.dependencies() {
            if updated_nodes.contains(&dep) {
                return false;
            }
        }

        true
    }
}

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

    use crate::Environment;
    use crate::core::{Dynamic, Hash32, ImportMap, Store, TaskContext};
    use crate::engine::{
        Many, Map, One, Provenance, TrackerState, Tracking, TypedCoarse, TypedFine,
    };

    use std::collections::HashSet;
    use std::marker::PhantomData;
    use std::sync::Arc;

    use petgraph::graph::NodeIndex;

    const ENV: Environment = Environment {
        generator: "test",
        mode: crate::core::Mode::Build,
        port: None,
        data: (),
    };

    // --- Helpers ---

    fn make_ctx() -> TaskContext<'static, ()> {
        TaskContext {
            env: &ENV,
            importmap: Box::leak(Box::new(ImportMap::new())),
            span: tracing::Span::none(),
        }
    }

    fn make_coarse_output(val: i32) -> Dynamic {
        Arc::new(val)
    }

    fn extract_state(tracking: Tracking) -> Option<TrackerState> {
        // Tracking::unwrap returns Vec<Option<TrackerState>>
        match tracking.unwrap().as_slice() {
            [Some(state)] => Some(state.clone()),
            _ => None,
        }
    }

    macro_rules! map {
        ( $( $key:expr => $val:expr, $hash:expr );* $(;)? ) => {{
            let mut map = std::collections::BTreeMap::new();

            $(
                let val: i32 = $val;
                let hash: u32 = $hash;
                let hash = Hash32::hash(hash.to_ne_bytes());

                map.insert(
                    $key.into(),
                    (val, Provenance(hash))
                );
            )*

            std::sync::Arc::new(Map { map, dirty: false })
        }};
    }

    // --- NodeGather Tests ---

    #[test]
    fn test_gather_selective_access() -> anyhow::Result<()> {
        let dep_ref = NodeIndex::new(1);
        let node = NodeGather {
            name: "reader".into(),
            dependencies: Many::<i32>::new(dep_ref),
            _phantom: PhantomData::<()>,
            callback: |_, tracker| {
                let _ = tracker.get("file_a")?;
                Ok(())
            },
        };

        let updated = HashSet::from_iter([dep_ref]);

        let input_1 = map! { "file_a" => 100, 1; "file_b" => 200, 1 };
        let input_2 = map! { "file_a" => 100, 1; "file_b" => 999, 2 };
        let input_3 = map! { "file_a" => 999, 2; "file_b" => 200, 1 };

        let (tracking, _) = node.execute(&make_ctx(), &mut Store::new(), &[input_1])?;
        let state = extract_state(tracking).unwrap();

        // Unread dependency changed
        let is_valid = node.is_valid(&[Some(state.clone())], &[input_2], &updated);
        assert!(is_valid, "Should be valid if unread file changes");

        // Read dependency changed
        let is_not_valid = !node.is_valid(&[Some(state)], &[input_3], &updated);
        assert!(is_not_valid, "Should be invalid if read file changes");

        Ok(())
    }

    #[test]
    fn test_gather_iteration() -> anyhow::Result<()> {
        let dep_ref = NodeIndex::new(1);
        let node = NodeGather {
            name: "iter".into(),
            dependencies: Many::<i32>::new(dep_ref),
            _phantom: PhantomData::<()>,
            callback: |_, tracker| {
                for _ in tracker {}
                Ok(())
            },
        };

        let updated = HashSet::from_iter([dep_ref]);

        let input_1 = map! { "a" => 1, 1 };
        let input_2 = map! { "a" => 1, 1; "b" => 2, 1 };

        let (tracking, _) = node.execute(&make_ctx(), &mut Store::new(), &[input_1])?;
        let state = extract_state(tracking).unwrap();

        // New file added
        // We previously iterated to the end so this would be included
        let is_not_valid = !node.is_valid(&[Some(state)], &[input_2], &updated);
        assert!(is_not_valid, "Should be invalid if new file added");

        Ok(())
    }

    #[test]
    fn test_gather_globs() -> anyhow::Result<()> {
        let dep_ref = NodeIndex::new(1);
        let node = NodeGather {
            name: "glob".into(),
            dependencies: Many::<i32>::new(dep_ref),
            _phantom: PhantomData::<()>,
            callback: |_, tracker| {
                for _ in tracker.glob("*.txt")? {}
                Ok(())
            },
        };

        let updated = HashSet::from_iter([dep_ref]);

        let input_1 = map! { "a.txt" => 1, 1; "b.png" => 2, 1 };
        let input_2 = map! { "a.txt" => 1, 1; "b.png" => 99, 2 };
        let input_3 = map! { "a.txt" => 1, 2; "b.png" => 2, 1 };

        let (tracking, _) = node.execute(&make_ctx(), &mut Store::new(), &[input_1])?;
        let state = extract_state(tracking).unwrap();

        // Non-matching file changes
        let is_valid = node.is_valid(&[Some(state.clone())], &[input_2], &updated);
        assert!(is_valid, "Should be valid if non-matching file changes");

        // Matching file changes
        let is_not_valid = !node.is_valid(&[Some(state)], &[input_3], &updated);
        assert!(is_not_valid, "Should be invalid if matching file changes");

        Ok(())
    }

    #[test]
    fn test_gather_coarse_dep() -> anyhow::Result<()> {
        let dep_ref = NodeIndex::new(1);
        let node = NodeGather {
            name: "coarse".into(),
            dependencies: One::<i32>::new(dep_ref),
            _phantom: PhantomData::<()>,
            callback: |_, _| Ok(()),
        };

        let updated = HashSet::from_iter([dep_ref]);

        let input = make_coarse_output(1);
        let input = std::slice::from_ref(&input);

        let (tracking, _) = node.execute(&make_ctx(), &mut Store::new(), input)?;
        let state = extract_state(tracking);

        // Should be None for One dependency
        assert!(state.is_none(), "Should be None for One<T> dependency");

        // Upstream changed
        let is_not_valid = !node.is_valid(&[state], input, &updated);
        assert!(is_not_valid, "Should be invalid if coarse node changed");

        Ok(())
    }

    // --- NodeScatter Tests ---

    #[test]
    fn test_scatter_invalidation() {
        let dep_ref = NodeIndex::new(1);
        let node = NodeScatter {
            name: "scatter".into(),
            dependencies: One::<i32>::new(dep_ref),
            _phantom: PhantomData::<()>,
            callback: |_, input| Ok(vec![("key".into(), *input)]),
        };

        let updated = HashSet::from_iter([dep_ref]);

        let input = make_coarse_output(10);

        // Always invalid if dependency updated
        let is_not_invalid = !node.is_valid(&[None], &[input], &updated);
        assert!(is_not_invalid, "Scatter should be invalid if dep changed");
    }

    #[test]
    fn test_scatter_fine_invalidation() -> anyhow::Result<()> {
        let dep_ref = NodeIndex::new(1);
        let node = NodeScatter {
            name: "scatter".into(),
            dependencies: Many::<i32>::new(dep_ref),
            _phantom: PhantomData::<()>,
            callback: |_, tracker| {
                // Access only "a"
                let val = tracker.get("a")?;
                Ok(vec![("key".into(), *val)])
            },
        };

        let updated = HashSet::from_iter([dep_ref]);

        let input_1 = map! { "a" => 1, 1; "b" => 2, 1 };
        let input_2 = map! { "a" => 1, 1; "b" => 99, 2 }; // Unread "b" changed
        let input_3 = map! { "a" => 2, 2; "b" => 2, 1 }; // Read "a" changed

        let (tracking, _) = node.execute(
            &make_ctx(),
            &mut Store::new(),
            &[input_1],
            None,
            &HashSet::new(),
        )?;
        let state = extract_state(tracking).unwrap();

        // Valid if unread changed
        let is_valid = node.is_valid(&[Some(state.clone())], &[input_2], &updated);
        assert!(is_valid, "Scatter should be valid if unread dep changed");

        // Invalid if read changed
        let is_not_valid = !node.is_valid(&[Some(state)], &[input_3], &updated);
        assert!(is_not_valid, "Scatter should be invalid if read dep changed");

        Ok(())
    }

    // --- NodeMap Tests ---

    #[test]
    fn test_map_reuse() -> anyhow::Result<()> {
        let ref_1 = NodeIndex::new(1);
        let ref_2 = NodeIndex::new(2);

        // NodeMap: Primary (Many<i32>) + Secondary (One<i32>)
        // Callback adds secondary val to primary val
        let node = NodeMap {
            name: "map".into(),
            dep_primary: Many::<i32>::new(ref_1),
            dep_secondary: One::<i32>::new(ref_2),
            _phantom: PhantomData::<()>,
            callback: |_, prim, sec| Ok(*prim + *sec),
        };

        let input_p = map! { "a" => 10, 1; "b" => 20, 1 };
        let input_s = make_coarse_output(5);
        let inputs = vec![input_p.clone(), input_s.clone()];

        let (_, out_1) = node.execute(
            &make_ctx(),
            &mut Store::new(),
            &inputs,
            None,
            &HashSet::new(),
        )?;

        assert_eq!(out_1.map.get("a").unwrap().0, 15); // 10 + 5

        // "a" unchanged, "b" changed
        let input_p = map! { "a" => 10, 1; "b" => 30, 2 };
        let inputs = vec![input_p, input_s];

        // We pass out_1 as old_output
        let old_dynamic: Dynamic = Arc::new(out_1);
        let (_, out_2) = node.execute(
            &make_ctx(),
            &mut Store::new(),
            &inputs,
            Some(&old_dynamic),
            &HashSet::new(),
        )?;

        assert!(!out_2.dirty);
        assert_eq!(out_2.map.get("a").unwrap().0, 15);
        assert_eq!(out_2.map.get("b").unwrap().0, 35); // 30 + 5

        Ok(())
    }

    #[test]
    fn test_map_secondary_forced_dirty() -> anyhow::Result<()> {
        let ref_1 = NodeIndex::new(1);
        let ref_2 = NodeIndex::new(2);

        let node = NodeMap {
            name: "map_dirty".into(),
            dep_primary: Many::<i32>::new(ref_1),
            dep_secondary: One::<i32>::new(ref_2),
            _phantom: PhantomData::<()>,
            callback: |_, prim, sec| Ok(*prim + *sec),
        };

        // Initial Run
        let input_p = map! { "a" => 10, 1 };
        let input_s = make_coarse_output(5);
        let inputs = vec![input_p.clone(), input_s];

        let (_, out_1) = node.execute(
            &make_ctx(),
            &mut Store::new(),
            &inputs,
            None,
            &HashSet::new(),
        )?;

        let old_dynamic: Dynamic = Arc::new(out_1);

        // secondary dependency changes -> 10
        let input_s = make_coarse_output(10);
        let inputs = vec![input_p, input_s];

        let updated = HashSet::from_iter([ref_2]);

        // is_valid should return false because secondary updated
        let is_not_valid = !node.is_valid(&[], &[], &updated);
        assert!(is_not_valid, "Should be invalid if secondary dep updated");

        // execute should force dirty and recompute EVERYTHING
        let (_, out_2) = node.execute(
            &make_ctx(),
            &mut Store::new(),
            &inputs,
            Some(&old_dynamic),
            &updated,
        )?;

        let is_dirty = out_2.dirty;
        assert!(is_dirty, "Map should be marked dirty due to forced update");
        assert_eq!(out_2.map.get("a").unwrap().0, 20); // 10 + 10 (recomputed)

        Ok(())
    }
}