module-util 0.3.1

Utilities for working with module.
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
//! Generic module [`Evaluator`].

use core::fmt::{self, Display};
use core::iter::FusedIterator;

use alloc::collections::linked_list::{self, LinkedList};
use alloc::vec::Vec;

use module::Merge;
use module::merge::error::Trace;

use crate::Result;

///////////////////////////////////////////////////////////////////////////////

trait LinkedListExt<T> {
    fn append_front(&mut self, other: &mut Self);
}

impl<T> LinkedListExt<T> for LinkedList<T> {
    fn append_front(&mut self, other: &mut Self) {
        other.append(self);
        self.append(other);
    }
}

///////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Clone)]
enum Instruction<T> {
    Enter(T),
    Exit,
    Eval(T),
}

impl<T> Instruction<T> {
    fn as_eval(&self) -> Option<&T> {
        if let Self::Eval(v) = self {
            Some(v)
        } else {
            None
        }
    }

    fn as_eval_mut(&mut self) -> Option<&mut T> {
        if let Self::Eval(v) = self {
            Some(v)
        } else {
            None
        }
    }

    fn into_eval(self) -> Option<T> {
        if let Self::Eval(v) = self {
            Some(v)
        } else {
            None
        }
    }
}

///////////////////////////////////////////////////////////////////////////////

/// A list of imports.
///
/// # serde
///
/// This type deserializes as a "sequence".
///
/// See: [serde data model](https://serde.rs/data-model.html).
#[derive(Clone)]
pub struct Imports<T>(LinkedList<Instruction<T>>);

impl<T> fmt::Debug for Imports<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list()
            .entries(self.0.iter().map(|x| {
                x.as_eval()
                    .expect("Imports should only have Eval instructions")
            }))
            .finish()
    }
}

impl<T> FromIterator<T> for Imports<T> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let inner = iter.into_iter().map(Instruction::Eval).collect();
        Self(inner)
    }
}

impl<T> From<T> for Imports<T> {
    fn from(value: T) -> Self {
        [value].into_iter().collect()
    }
}

impl<T> Default for Imports<T> {
    fn default() -> Self {
        Self::empty()
    }
}

impl<T> Imports<T> {
    /// Create an empty import list.
    pub const fn empty() -> Self {
        Self(LinkedList::new())
    }

    /// Get the number of imports in this list.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Check whether the import list is empty.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Add `import` to the list.
    pub fn push(&mut self, import: T) {
        self.0.push_back(Instruction::Eval(import));
    }

    /// Move over all imports from `imports` to `self`.
    ///
    /// This function is equivalent to [`push`]ing each item of `imports` to
    /// `self`.
    ///
    /// [`push`]: Imports::push
    pub fn append(&mut self, imports: &mut Self) {
        self.0.append(&mut imports.0);
    }

    /// Create an iterator over the imports of `self`.
    pub fn iter(&self) -> Iter<'_, T> {
        Iter(self.0.iter())
    }

    /// Create a mutable iterator over the imports of `self`.
    pub fn iter_mut(&mut self) -> IterMut<'_, T> {
        IterMut(self.0.iter_mut())
    }
}

impl<'a, T> IntoIterator for &'a Imports<T> {
    type Item = &'a T;
    type IntoIter = Iter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, T> IntoIterator for &'a mut Imports<T> {
    type Item = &'a mut T;
    type IntoIter = IterMut<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

impl<T> IntoIterator for Imports<T> {
    type Item = T;
    type IntoIter = IntoIter<T>;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter(self.0.into_iter())
    }
}

/// An iterator over [`Imports`].
///
/// Created by [`Imports::iter`].
#[expect(missing_debug_implementations)]
pub struct Iter<'a, T>(linked_list::Iter<'a, Instruction<T>>);

impl<'a, T> Iterator for Iter<'a, T> {
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|x| {
            x.as_eval()
                .expect("Imports should only have Eval instructions")
        })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.0.next_back().map(|x| {
            x.as_eval()
                .expect("Imports should only have Eval instructions")
        })
    }
}

impl<'a, T> ExactSizeIterator for Iter<'a, T> {
    fn len(&self) -> usize {
        self.0.len()
    }
}

impl<'a, T> FusedIterator for Iter<'a, T> {}

/// A mutable iterator over [`Imports`].
///
/// Created by [`Imports::iter_mut`].
#[expect(missing_debug_implementations)]
pub struct IterMut<'a, T>(linked_list::IterMut<'a, Instruction<T>>);

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|x| {
            x.as_eval_mut()
                .expect("Imports should only have Eval instructions")
        })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.0.next_back().map(|x| {
            x.as_eval_mut()
                .expect("Imports should only have Eval instructions")
        })
    }
}

impl<'a, T> ExactSizeIterator for IterMut<'a, T> {
    fn len(&self) -> usize {
        self.0.len()
    }
}

impl<'a, T> FusedIterator for IterMut<'a, T> {}

/// Owning iterator over [`Imports`].
///
/// Created by [`Imports::into_iter`].
#[expect(missing_debug_implementations)]
pub struct IntoIter<T>(linked_list::IntoIter<Instruction<T>>);

impl<T> Iterator for IntoIter<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|x| {
            x.into_eval()
                .expect("Imports should only have Eval instructions")
        })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

impl<T> DoubleEndedIterator for IntoIter<T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.0.next_back().map(|x| {
            x.into_eval()
                .expect("Imports should only have Eval instructions")
        })
    }
}

impl<T> ExactSizeIterator for IntoIter<T> {
    fn len(&self) -> usize {
        self.0.len()
    }
}

impl<T> FusedIterator for IntoIter<T> {}

#[cfg(feature = "serde")]
mod serde_impl {
    use super::*;

    use core::marker::PhantomData;

    use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};

    impl<'de, T> Deserialize<'de> for Imports<T>
    where
        T: Deserialize<'de>,
    {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            struct ImportsVisitor<T>(PhantomData<T>);

            impl<'de, T> Visitor<'de> for ImportsVisitor<T>
            where
                T: Deserialize<'de>,
            {
                type Value = Imports<T>;

                fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                    formatter.write_str("a sequence")
                }

                fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
                where
                    A: SeqAccess<'de>,
                {
                    let mut inner = LinkedList::new();

                    while let Some(e) = seq.next_element()? {
                        inner.push_back(Instruction::Eval(e));
                    }

                    Ok(Imports(inner))
                }
            }

            let visitor = ImportsVisitor(PhantomData);
            deserializer.deserialize_seq(visitor)
        }
    }
}

///////////////////////////////////////////////////////////////////////////////

/// A generic evaluator that evaluates modules of type `Value`.
///
/// This evaluator can be used to decouple the way modules are read from the way
/// they are evaluated. It follows design paradigms of the ["sans-io" pattern].
///
/// This evaluator does not do anything on its own, it needs external code to
/// "drive" it. Something like an "event loop".
///
/// ```rust,no_run
/// # use module_util::evaluator::Evaluator;
/// let mut evaluator = Evaluator::new();
///
/// evaluator.import("module 1");
/// while let Some(this) = evaluator.next() {
///     println!("evaluating {this}...");
///
///     // Read the value of the module here from the filesystem, network,
///     // environment, etc.
///     let value: i32 = unimplemented!();
///
///     // Resolve what other modules this module wants to import. This is also
///     // specific to how you read a module. For example, it could be the
///     // top-level "imports" array of the JSON file.
///     let imports = unimplemented!();
///
///     evaluator.eval(this, imports, value).unwrap();
/// }
///
/// let value = evaluator.finish().unwrap();
/// println!("final value: {value}");
/// ```
///
/// This pattern allows you to implement whatever kind of module logic you want
/// inside the loop without modifying the evaluator.
///
/// ["sans-io" pattern]: https://sans-io.readthedocs.io/how-to-sans-io.html#how-to-write-i-o-free-protocol-implementations
#[derive(Debug, Clone)]
pub struct Evaluator<Ref, Value> {
    program: LinkedList<Instruction<Ref>>,
    trace: Vec<Ref>,
    value: Option<Value>,
}

impl<Ref, Value> Default for Evaluator<Ref, Value> {
    fn default() -> Self {
        Self::new()
    }
}

impl<Ref, Value> Evaluator<Ref, Value> {
    /// Create a new evaluator.
    pub const fn new() -> Self {
        Self {
            program: LinkedList::new(),
            trace: Vec::new(),
            value: None,
        }
    }

    /// Create a new evaluator with an initial value.
    ///
    /// Calling [`finish`] on the returned [`Evaluator`] will never return
    /// [`None`].
    ///
    /// [`finish`]: Evaluator::finish
    pub const fn with(value: Value) -> Self {
        Self {
            program: LinkedList::new(),
            trace: Vec::new(),
            value: Some(value),
        }
    }

    /// Specify additional `imports` to be evaluated.
    ///
    /// This function is usually only used to provide the first module.
    pub fn import<I>(&mut self, imports: I)
    where
        I: Into<Imports<Ref>>,
    {
        self._import(imports.into())
    }

    fn _import(&mut self, imports: Imports<Ref>) {
        let Imports(mut imports) = imports;
        self.program.append(&mut imports);
    }

    /// Get the next module in the evaluation order.
    ///
    /// Modules are evaluated in DFS order.
    #[expect(clippy::should_implement_trait)]
    // This should not be implemented in [`Iterator::next`] because it does not
    // really make sense for this type to be used as an [`Iterator`].
    pub fn next(&mut self) -> Option<Ref> {
        loop {
            match self.program.pop_front()? {
                Instruction::Enter(x) => {
                    self.trace.push(x);
                }
                Instruction::Exit => {
                    self.trace.pop();
                }
                Instruction::Eval(x) => break Some(x),
            }
        }
    }

    /// Finish the evaluation and get the result.
    ///
    /// Returns [`Some`] with the final value or [`None`] if no module has been
    /// evaluated successfully and the [`Evaluator`] was created without an
    /// initial value.
    ///
    /// See: [`Evaluator::with`].
    pub fn finish(self) -> Option<Value> {
        self.value
    }
}

impl<Ref, Value> Evaluator<Ref, Value>
where
    Ref: Display,
{
    /// Build the module trace for `this` module.
    ///
    /// This function can be used to attach a module trace to [`module::Error`]
    /// on any user-thrown errors during the evaluation loop.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use module::merge::Context;
    /// # use module_util::evaluator::Evaluator;
    /// # fn a() -> module_util::Result<i32> {
    /// use module::Error;
    ///
    /// let mut evaluator = Evaluator::new();
    ///
    /// evaluator.import("module 1");
    /// while let Some(this) = evaluator.next() {
    ///     // ...
    ///
    ///     if this == "module 2" {
    ///         return Err(Error::custom("module 2 is not allowed to be evaluated"))
    ///             .with_trace(|| evaluator.trace(this));
    ///     }
    ///
    ///     // ...
    ///
    /// #    let imports = unimplemented!();
    /// #    let value: i32 = unimplemented!();
    ///     evaluator.eval(this, imports, value)?;
    /// }
    ///
    /// let value = evaluator.finish().unwrap();
    /// Ok(value)
    /// # }
    /// ```
    pub fn trace(&self, this: Ref) -> Trace {
        let mut trace: Trace = self.trace.iter().collect();
        trace.push_back(this);
        trace
    }
}

impl<Ref, Value> Evaluator<Ref, Value>
where
    Ref: Display,
    Value: Merge,
{
    /// Evaluate the module `this`.
    ///
    /// - `imports`: any modules `this` imports
    /// - `value`: the value of the `this` module
    ///
    /// Returns the result of the [`Merge`] operation on `value`. The returned
    /// error has the appropriate trace attached.
    ///
    /// See: [`Evaluator::trace`].
    pub fn eval(&mut self, this: Ref, imports: Imports<Ref>, value: Value) -> Result<()> {
        self.value = match self.value.take() {
            None => Some(value),
            Some(old) => match old.merge(value) {
                Ok(x) => Some(x),
                Err(mut e) => {
                    e.trace = self.trace(this);
                    return Err(e);
                }
            },
        };

        let Imports(mut imports) = imports;
        if !imports.is_empty() {
            imports.push_front(Instruction::Enter(this));
            imports.push_back(Instruction::Exit);
            self.program.append_front(&mut imports);
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use module::types::NoMerge;
    use module::{Context, Error};

    use super::*;

    fn eval<T>(initial: &'static str, modules: &[(&str, &[&'static str], T)]) -> Result<T>
    where
        T: Clone + Merge,
    {
        let mut x = Evaluator::new();

        x.import(initial);
        while let Some(this) = x.next() {
            let Some((_, imports, value)) = modules.iter().find(|(name, _, _)| *name == this)
            else {
                return Err(Error::custom("no such module")).with_trace(|| x.trace(this));
            };

            let imports = imports.iter().copied().collect();
            let value = value.clone();

            x.eval(this, imports, value)?;
        }

        let value = x.finish().unwrap();
        Ok(value)
    }

    #[test]
    fn test_module_trace() {
        let err = eval(
            "module 1",
            &[
                ("module 1", &["module 2"], Some(NoMerge(()))),
                ("module 2", &["module 3", "module 4"], None),
                ("module 3", &[], None),
                ("module 4", &["module 5"], None),
                ("module 5", &["module 6"], None),
                ("module 6", &[], Some(NoMerge(()))),
            ],
        )
        .unwrap_err();

        let trace: Vec<_> = err.trace.modules().collect();

        assert_eq!(
            trace,
            &["module 1", "module 2", "module 4", "module 5", "module 6"]
        );
    }

    #[test]
    fn test_eval_order() {
        // If the order is correct (DFS), then this should reach the
        // "non-existent" module first and error there instead of erroring on
        // "module 6" because of the collision.
        let err = eval(
            "module 1",
            &[
                ("module 1", &["module 2", "module 3"], Some(NoMerge(()))),
                ("module 2", &["module 4"], None),
                ("module 4", &["module 5"], None),
                ("module 5", &["non-existent"], None),
                ("module 3", &["module 6"], None),
                ("module 6", &[], Some(NoMerge(()))),
            ],
        )
        .unwrap_err();

        assert!(!err.kind.is_collision());
    }
}