macroonz-compiler 0.2.0

Deterministic Rust code generation for procedural macros: plan, render, close, explain, and bind one sealed expansion from declared input.
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
//! The bounded home's invariant nucleus: every road that reaches a private field.
//!
//! Declared inside `types.rs` as its own child, which is what makes this home's claims structural.
//! A list longer than its ceiling and a non-empty list with nothing in it are values nobody can build, rather than shapes something downstream has to check for.

use super::{
    Bounded, Capped, Capping, DuplicateKey, Empty, ForeignRosterReference, KeyedRoster,
    KeyedRosterAssignment, KeyedRosterAssignmentError, KeyedRosterError, NonEmpty, NonEmptyError,
    Overflow, UnassignedRosterMember,
};
use core::borrow::Borrow;

impl<T, const N: usize> Bounded<T, N> {
    /// An empty collection under this ceiling.
    #[must_use]
    pub const fn empty() -> Self {
        Self(Vec::new())
    }

    /// Admits one complete ordered offering under this ceiling.
    ///
    /// # Errors
    ///
    /// Returns [`Overflow`] when more than `N` items are offered.
    pub fn new(items: Vec<T>) -> Result<Self, Overflow> {
        if items.len() <= N {
            Ok(Self(items))
        } else {
            Err(Overflow {
                capacity: N,
                offered: items.len(),
            })
        }
    }

    /// Admits a fixed-arity offering whose fit is settled at compile time.
    #[must_use]
    pub fn from_array<const M: usize>(items: [T; M]) -> Self {
        const {
            assert!(
                M <= N,
                "a fixed list longer than the ceiling it is declared under"
            );
        }
        Self(Vec::from(items))
    }

    /// The held items.
    #[must_use]
    pub fn as_slice(&self) -> &[T] {
        self.0.as_slice()
    }

    /// Reads the held items in order.
    pub fn iter(&self) -> impl Iterator<Item = &T> {
        self.0.iter()
    }

    /// How many items are held.
    #[must_use]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Whether nothing is held.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub(crate) fn into_vec(self) -> Vec<T> {
        self.0
    }

    pub(crate) fn mapped<U>(&self, operation: impl FnMut(&T) -> U) -> Bounded<U, N> {
        Bounded(self.iter().map(operation).collect())
    }

    /// Appends one item where the resulting collection fits under this ceiling.
    ///
    /// # Errors
    ///
    /// Returns [`Overflow`] without changing the list where the appended item would exceed `N`.
    pub fn try_push(&mut self, item: T) -> Result<(), Overflow> {
        let offered = self.0.len().saturating_add(1);
        if offered > N {
            return Err(Overflow {
                capacity: N,
                offered,
            });
        }
        self.0.push(item);
        Ok(())
    }
}

impl<T, const N: usize> NonEmpty<T, N> {
    /// A non-empty collection holding exactly one item.
    #[must_use]
    pub const fn one(value: T) -> Self {
        const {
            assert!(
                N >= 1,
                "a non-empty list under a ceiling that admits no item"
            );
        }
        Self {
            head: value,
            tail: Vec::new(),
        }
    }

    /// Admits one complete ordered offering that is non-empty and under this ceiling.
    ///
    /// # Errors
    ///
    /// Returns [`Empty`] when nothing is offered, and [`Overflow`] when more than `N` items are.
    pub fn new(items: Vec<T>) -> Result<Self, NonEmptyError> {
        let offered = items.len();
        let mut rest = items.into_iter();
        let Some(head) = rest.next() else {
            return Err(NonEmptyError::Empty(Empty));
        };
        if offered <= N {
            Ok(Self {
                head,
                tail: rest.collect(),
            })
        } else {
            Err(NonEmptyError::Overflow(Overflow {
                capacity: N,
                offered,
            }))
        }
    }

    pub(crate) fn from_bounded(items: Bounded<T, N>) -> Result<Self, Empty> {
        let mut items = items.into_vec().into_iter();
        let Some(head) = items.next() else {
            return Err(Empty);
        };
        Ok(Self {
            head,
            tail: items.collect(),
        })
    }

    /// The first item, which this list always has.
    #[must_use]
    pub const fn first(&self) -> &T {
        &self.head
    }

    /// The first item and the rest, in order.
    #[must_use]
    pub fn split(&self) -> (&T, &[T]) {
        (&self.head, self.tail.as_slice())
    }

    /// Reads the held items in order.
    pub fn iter(&self) -> impl Iterator<Item = &T> {
        self.into_iter()
    }

    /// How many items are held, which is never zero.
    #[must_use]
    pub fn count(&self) -> usize {
        self.tail.len().saturating_add(1)
    }

    pub(crate) fn mapped<U>(self, mut operation: impl FnMut(T) -> U) -> NonEmpty<U, N> {
        NonEmpty {
            head: operation(self.head),
            tail: self.tail.into_iter().map(operation).collect(),
        }
    }
}

impl<'held, T, const N: usize> IntoIterator for &'held NonEmpty<T, N> {
    type Item = &'held T;
    type IntoIter = core::iter::Chain<core::iter::Once<&'held T>, core::slice::Iter<'held, T>>;

    fn into_iter(self) -> Self::IntoIter {
        core::iter::once(&self.head).chain(self.tail.iter())
    }
}

impl<T, K, const N: usize> KeyedRoster<T, K, N> {
    /// A roster containing one member under its caller-declared key.
    #[must_use]
    pub const fn one(member: T, key: K) -> Self {
        Self {
            members: NonEmpty::one(member),
            keys: NonEmpty::one(key),
        }
    }

    /// The first member, which this roster always has.
    #[must_use]
    pub const fn first(&self) -> &T {
        self.members.first()
    }

    /// The key retained for the first member.
    #[must_use]
    pub const fn first_key(&self) -> &K {
        self.keys.first()
    }

    /// How many members are held, which is never zero.
    #[must_use]
    pub fn count(&self) -> usize {
        self.members.count()
    }

    pub(crate) fn positions_where(
        &self,
        mut predicate: impl FnMut(usize, &K, &T) -> bool,
    ) -> Bounded<usize, N> {
        Bounded(
            self.indexed()
                .filter_map(|(index, key, member)| predicate(index, key, member).then_some(index))
                .collect(),
        )
    }

    /// Reads the members in declaration order.
    pub fn members(&self) -> impl Iterator<Item = &T> {
        self.members.iter()
    }

    /// Reads the retained keys in declaration order.
    pub fn keys(&self) -> impl Iterator<Item = &K> {
        self.keys.iter()
    }

    /// Reads every declaration index, retained key, and member together.
    pub fn indexed(&self) -> impl Iterator<Item = (usize, &K, &T)> {
        self.keys()
            .zip(self.members())
            .enumerate()
            .map(|(index, (key, member))| (index, key, member))
    }

    /// Reads the key and member at one checked declaration index.
    #[must_use]
    pub fn at(&self, index: usize) -> Option<(&K, &T)> {
        self.keys().zip(self.members()).nth(index)
    }

    /// Finds the declaration index of one borrowed key.
    #[must_use]
    pub fn index_of<Q>(&self, sought: &Q) -> Option<usize>
    where
        K: Borrow<Q>,
        Q: Eq + ?Sized,
    {
        self.keys().position(|key| key.borrow() == sought)
    }

    /// Finds the member held under one borrowed key.
    #[must_use]
    pub fn get<Q>(&self, sought: &Q) -> Option<&T>
    where
        K: Borrow<Q>,
        Q: Eq + ?Sized,
    {
        self.keys()
            .zip(self.members())
            .find_map(|(key, member)| (key.borrow() == sought).then_some(member))
    }

    /// Finds the declaration position, retained key, and member under one borrowed key.
    #[must_use]
    pub fn indexed_get<Q>(&self, sought: &Q) -> Option<(usize, &K, &T)>
    where
        K: Borrow<Q>,
        Q: Eq + ?Sized,
    {
        self.indexed()
            .find(|(_index, key, _member)| (*key).borrow() == sought)
    }
}

impl<T, K: Eq, const N: usize> KeyedRoster<T, K, N> {
    /// Admits one complete ordered offering under caller-declared unique keys.
    ///
    /// The offering's nonempty bounded magnitude is settled before the key projection runs.
    ///
    /// # Errors
    ///
    /// Returns [`Empty`] when nothing is offered, [`Overflow`] when more than `N` items are offered, and [`DuplicateKey`] coordinates for every distinct key that occurs more than once.
    pub fn new(
        members: Vec<T>,
        key_of: impl FnMut(&T) -> K,
    ) -> Result<Self, KeyedRosterError<K, N>> {
        let members = NonEmpty::new(members).map_err(keyed_magnitude_refusal)?;
        let keys = project_keys(&members, key_of);
        match admit_keys(keys) {
            KeyAdmission::Unique(keys) => Ok(Self { members, keys }),
            KeyAdmission::Duplicated(duplicates) => {
                Err(KeyedRosterError::DuplicateKeys(duplicates))
            }
        }
    }
}

impl<K: Eq, const N: usize> NonEmpty<K, N> {
    pub(crate) fn duplicate_keys(self) -> Option<NonEmpty<DuplicateKey<K, N>, N>> {
        match admit_keys(self) {
            KeyAdmission::Unique(_) => None,
            KeyAdmission::Duplicated(duplicates) => Some(duplicates),
        }
    }
}

impl<K, const N: usize> DuplicateKey<K, N> {
    /// The caller-declared key that occurred more than once.
    #[must_use]
    pub const fn key(&self) -> &K {
        &self.key
    }

    /// The zero-based declaration position of the first occurrence.
    #[must_use]
    pub const fn first_position(&self) -> usize {
        self.first
    }

    /// The zero-based declaration positions of every later occurrence.
    #[must_use]
    pub const fn repeated_positions(&self) -> &NonEmpty<usize, N> {
        &self.repeated
    }
}

pub(crate) fn first_duplicate_position<T>(
    items: &[T],
    equivalent: impl Fn(&T, &T) -> bool,
) -> Option<usize> {
    items.iter().enumerate().find_map(|(position, item)| {
        items
            .iter()
            .take(position)
            .any(|earlier| equivalent(earlier, item))
            .then_some(position)
    })
}

fn keyed_magnitude_refusal<K, const N: usize>(error: NonEmptyError) -> KeyedRosterError<K, N> {
    match error {
        NonEmptyError::Empty(empty) => KeyedRosterError::Empty(empty),
        NonEmptyError::Overflow(overflow) => KeyedRosterError::Overflow(overflow),
    }
}

fn project_keys<T, K, const N: usize>(
    members: &NonEmpty<T, N>,
    mut key_of: impl FnMut(&T) -> K,
) -> NonEmpty<K, N> {
    let (head, tail) = members.split();
    NonEmpty {
        head: key_of(head),
        tail: tail.iter().map(key_of).collect(),
    }
}

fn admit_keys<K: Eq, const N: usize>(keys: NonEmpty<K, N>) -> KeyAdmission<K, N> {
    let NonEmpty { head, tail } = keys;
    let mut groups = KeyGroups {
        head: KeyGroup {
            key: head,
            first: 0,
            repeated: Vec::new(),
        },
        tail: Vec::new(),
    };
    for (index, key) in tail.into_iter().enumerate() {
        groups.insert(key, index.saturating_add(1));
    }
    groups.admit()
}

struct KeyGroup<K> {
    key: K,
    first: usize,
    repeated: Vec<usize>,
}

struct KeyGroups<K> {
    head: KeyGroup<K>,
    tail: Vec<KeyGroup<K>>,
}

enum KeyAdmission<K, const N: usize> {
    Unique(NonEmpty<K, N>),
    Duplicated(NonEmpty<DuplicateKey<K, N>, N>),
}

impl<K: Eq> KeyGroups<K> {
    fn insert(&mut self, key: K, index: usize) {
        if self.head.key == key {
            self.head.repeated.push(index);
            return;
        }
        if let Some(group) = self.tail.iter_mut().find(|group| group.key == key) {
            group.repeated.push(index);
            return;
        }
        self.tail.push(KeyGroup {
            key,
            first: index,
            repeated: Vec::new(),
        });
    }

    fn admit<const N: usize>(self) -> KeyAdmission<K, N> {
        let mut tail = self.tail.into_iter();
        let head = match admitted_group(self.head) {
            Ok(key) => key,
            Err(duplicate) => {
                return KeyAdmission::Duplicated(NonEmpty {
                    head: duplicate,
                    tail: tail
                        .filter_map(|group| admitted_group(group).err())
                        .collect(),
                });
            }
        };
        let mut unique = Vec::new();
        while let Some(group) = tail.next() {
            match admitted_group(group) {
                Ok(key) => unique.push(key),
                Err(duplicate) => {
                    return KeyAdmission::Duplicated(NonEmpty {
                        head: duplicate,
                        tail: tail
                            .filter_map(|remaining| admitted_group(remaining).err())
                            .collect(),
                    });
                }
            }
        }
        KeyAdmission::Unique(NonEmpty { head, tail: unique })
    }
}

fn admitted_group<K, const N: usize>(group: KeyGroup<K>) -> Result<K, DuplicateKey<K, N>> {
    let KeyGroup {
        key,
        first,
        repeated,
    } = group;
    let mut repeated = repeated.into_iter();
    if let Some(repeated_head) = repeated.next() {
        return Err(DuplicateKey {
            key,
            first,
            repeated: NonEmpty {
                head: repeated_head,
                tail: repeated.collect(),
            },
        });
    }
    Ok(key)
}

impl<D, K, P, S, const N: usize> KeyedRosterAssignment<D, K, P, S, N> {
    /// The complete caller-keyed denominator retained by this assignment.
    #[must_use]
    pub const fn denominator(&self) -> &KeyedRoster<D, K, N> {
        &self.denominator
    }

    /// The payload roster aligned with the denominator and keyed by caller-declared seats.
    #[must_use]
    pub const fn payloads(&self) -> &KeyedRoster<P, S, N> {
        &self.payloads
    }

    /// How many denominator members and aligned payloads are held.
    #[must_use]
    pub fn count(&self) -> usize {
        self.denominator.count()
    }

    /// The first denominator member and its assigned payload.
    #[must_use]
    pub const fn first(&self) -> (&K, &D, &S, &P) {
        (
            self.denominator.first_key(),
            self.denominator.first(),
            self.payloads.first_key(),
            self.payloads.first(),
        )
    }

    /// Reads every denominator member and aligned payload in denominator order.
    pub fn indexed(&self) -> impl Iterator<Item = (usize, &K, &D, &S, &P)> {
        self.denominator
            .indexed()
            .zip(self.payloads.keys().zip(self.payloads.members()))
            .map(|((index, key, member), (seat, payload))| (index, key, member, seat, payload))
    }

    /// Reads one denominator member and aligned payload at a checked index.
    #[must_use]
    pub fn at(&self, index: usize) -> Option<(&K, &D, &S, &P)> {
        self.denominator
            .at(index)
            .zip(self.payloads.at(index))
            .map(|((key, member), (seat, payload))| (key, member, seat, payload))
    }

    /// Finds one denominator member and aligned payload through a borrowed key.
    #[must_use]
    pub fn get<Q>(&self, sought: &Q) -> Option<(&D, &S, &P)>
    where
        K: Borrow<Q>,
        Q: Eq + ?Sized,
    {
        let index = self.denominator.index_of(sought)?;
        self.at(index)
            .map(|(_key, member, seat, payload)| (member, seat, payload))
    }
}

impl<D, K: Eq, P, S: Eq, const N: usize> KeyedRosterAssignment<D, K, P, S, N> {
    /// Completes one payload assignment over an existing caller-keyed denominator.
    ///
    /// Payload magnitude is settled before either key projection runs.
    /// Reference membership and uniqueness are settled before payload-seat keys are projected.
    ///
    /// # Errors
    ///
    /// Returns [`KeyedRosterAssignmentError`] with the first structural refusal class reached by the declared construction order.
    pub fn complete(
        denominator: KeyedRoster<D, K, N>,
        payloads: Vec<P>,
        reference_of: impl FnMut(&P) -> K,
        seat_of: impl FnMut(&P) -> S,
    ) -> Result<Self, KeyedRosterAssignmentError<K, S, N>> {
        let payloads = NonEmpty::new(payloads).map_err(assignment_magnitude_refusal::<K, S, N>)?;
        let references = project_keys(&payloads, reference_of);
        let (references, positions) = match admit_references(&denominator, references) {
            ReferenceAdmission::Lawful { keys, positions } => (keys, positions),
            ReferenceAdmission::Foreign(foreign) => {
                return Err(KeyedRosterAssignmentError::ForeignReferences(foreign));
            }
        };
        if let KeyAdmission::Duplicated(duplicates) = admit_keys(references) {
            return Err(KeyedRosterAssignmentError::DuplicateReferences(duplicates));
        }
        let seats = project_keys(&payloads, seat_of);
        let seats = match admit_keys(seats) {
            KeyAdmission::Unique(seats) => seats,
            KeyAdmission::Duplicated(duplicates) => {
                return Err(KeyedRosterAssignmentError::ReusedPayloadSeats(duplicates));
            }
        };
        let denominator = match settle_completeness(denominator, &positions) {
            AssignmentCompleteness::Complete(denominator) => denominator,
            AssignmentCompleteness::Missing(missing) => {
                return Err(KeyedRosterAssignmentError::MissingMembers(missing));
            }
        };
        let payloads = align_payloads(payloads, seats, positions);
        Ok(Self {
            denominator,
            payloads,
        })
    }
}

impl<K> ForeignRosterReference<K> {
    pub(crate) const fn at(key: K, offered_position: usize) -> Self {
        Self {
            key,
            offered_position,
        }
    }

    /// The foreign roster key named by the offered item.
    #[must_use]
    pub const fn key(&self) -> &K {
        &self.key
    }

    /// The zero-based offered-item position carrying the foreign reference.
    #[must_use]
    pub const fn offered_position(&self) -> usize {
        self.offered_position
    }
}

impl<K> UnassignedRosterMember<K> {
    /// The denominator key for which no payload was offered.
    #[must_use]
    pub const fn key(&self) -> &K {
        &self.key
    }

    /// The zero-based denominator position for which no payload was offered.
    #[must_use]
    pub const fn denominator_position(&self) -> usize {
        self.denominator_position
    }
}

enum ReferenceAdmission<K, const N: usize> {
    Lawful {
        keys: NonEmpty<K, N>,
        positions: NonEmpty<usize, N>,
    },
    Foreign(NonEmpty<ForeignRosterReference<K>, N>),
}

fn admit_references<D, K: Eq, const N: usize>(
    denominator: &KeyedRoster<D, K, N>,
    references: NonEmpty<K, N>,
) -> ReferenceAdmission<K, N> {
    let NonEmpty { head, tail } = references;
    let Some(head_position) = denominator.index_of(&head) else {
        let foreign_tail = tail
            .into_iter()
            .enumerate()
            .filter_map(|(offset, key)| {
                denominator
                    .index_of(&key)
                    .is_none()
                    .then_some(ForeignRosterReference {
                        key,
                        offered_position: offset.saturating_add(1),
                    })
            })
            .collect();
        return ReferenceAdmission::Foreign(NonEmpty {
            head: ForeignRosterReference {
                key: head,
                offered_position: 0,
            },
            tail: foreign_tail,
        });
    };
    admit_references_after_lawful_head(denominator, head, head_position, tail)
}

fn admit_references_after_lawful_head<D, K: Eq, const N: usize>(
    denominator: &KeyedRoster<D, K, N>,
    head: K,
    head_position: usize,
    tail: Vec<K>,
) -> ReferenceAdmission<K, N> {
    let mut lawful_keys = Vec::new();
    let mut lawful_positions = Vec::new();
    let mut foreign_head = None;
    let mut foreign_tail = Vec::new();
    for (offset, key) in tail.into_iter().enumerate() {
        let offered_position = offset.saturating_add(1);
        if let Some(position) = denominator.index_of(&key) {
            lawful_keys.push(key);
            lawful_positions.push(position);
        } else {
            let foreign = ForeignRosterReference {
                key,
                offered_position,
            };
            if foreign_head.is_none() {
                foreign_head = Some(foreign);
            } else {
                foreign_tail.push(foreign);
            }
        }
    }
    if let Some(foreign) = foreign_head {
        ReferenceAdmission::Foreign(NonEmpty {
            head: foreign,
            tail: foreign_tail,
        })
    } else {
        ReferenceAdmission::Lawful {
            keys: NonEmpty {
                head,
                tail: lawful_keys,
            },
            positions: NonEmpty {
                head: head_position,
                tail: lawful_positions,
            },
        }
    }
}

enum AssignmentCompleteness<D, K, const N: usize> {
    Complete(KeyedRoster<D, K, N>),
    Missing(NonEmpty<UnassignedRosterMember<K>, N>),
}

enum KeyCompleteness<K, const N: usize> {
    Complete(NonEmpty<K, N>),
    Missing(NonEmpty<UnassignedRosterMember<K>, N>),
}

enum AssignmentStanding {
    Assigned,
    Missing,
}

fn settle_completeness<D, K, const N: usize>(
    denominator: KeyedRoster<D, K, N>,
    positions: &NonEmpty<usize, N>,
) -> AssignmentCompleteness<D, K, N> {
    let KeyedRoster { members, keys } = denominator;
    let NonEmpty { head, tail } = keys;
    let first_assigned = positions.iter().any(|position| *position == 0);
    let mut completeness = if first_assigned {
        KeyCompleteness::Complete(NonEmpty {
            head,
            tail: Vec::new(),
        })
    } else {
        KeyCompleteness::Missing(NonEmpty {
            head: UnassignedRosterMember {
                key: head,
                denominator_position: 0,
            },
            tail: Vec::new(),
        })
    };
    for (offset, key) in tail.into_iter().enumerate() {
        let denominator_position = offset.saturating_add(1);
        let standing = if positions
            .iter()
            .any(|position| *position == denominator_position)
        {
            AssignmentStanding::Assigned
        } else {
            AssignmentStanding::Missing
        };
        completeness = completeness.push(key, denominator_position, standing);
    }
    match completeness {
        KeyCompleteness::Complete(complete_keys) => AssignmentCompleteness::Complete(KeyedRoster {
            members,
            keys: complete_keys,
        }),
        KeyCompleteness::Missing(missing) => AssignmentCompleteness::Missing(missing),
    }
}

impl<K, const N: usize> KeyCompleteness<K, N> {
    fn push(self, key: K, denominator_position: usize, standing: AssignmentStanding) -> Self {
        match (self, standing) {
            (Self::Complete(mut keys), AssignmentStanding::Assigned) => {
                keys.tail.push(key);
                Self::Complete(keys)
            }
            (Self::Complete(_keys), AssignmentStanding::Missing) => Self::Missing(NonEmpty {
                head: UnassignedRosterMember {
                    key,
                    denominator_position,
                },
                tail: Vec::new(),
            }),
            (Self::Missing(missing), AssignmentStanding::Assigned) => Self::Missing(missing),
            (Self::Missing(mut missing), AssignmentStanding::Missing) => {
                missing.tail.push(UnassignedRosterMember {
                    key,
                    denominator_position,
                });
                Self::Missing(missing)
            }
        }
    }
}

struct PendingAssignment<P, S> {
    denominator_position: usize,
    payload: P,
    seat: S,
}

fn align_payloads<P, S, const N: usize>(
    payloads: NonEmpty<P, N>,
    seats: NonEmpty<S, N>,
    positions: NonEmpty<usize, N>,
) -> KeyedRoster<P, S, N> {
    let NonEmpty {
        head: payload_head,
        tail: payload_tail,
    } = payloads;
    let NonEmpty {
        head: seat_head,
        tail: seat_tail,
    } = seats;
    let NonEmpty {
        head: position_head,
        tail: position_tail,
    } = positions;
    let mut head = PendingAssignment {
        denominator_position: position_head,
        payload: payload_head,
        seat: seat_head,
    };
    let mut tail = position_tail
        .into_iter()
        .zip(payload_tail)
        .zip(seat_tail)
        .map(
            |((denominator_position, payload), seat)| PendingAssignment {
                denominator_position,
                payload,
                seat,
            },
        )
        .collect::<Vec<_>>();
    for assignment in &mut tail {
        if assignment.denominator_position < head.denominator_position {
            core::mem::swap(&mut head, assignment);
        }
    }
    tail.sort_by_key(|assignment| assignment.denominator_position);
    let mut ordered_payload_tail = Vec::with_capacity(tail.len());
    let mut ordered_seat_tail = Vec::with_capacity(tail.len());
    for assignment in tail {
        ordered_payload_tail.push(assignment.payload);
        ordered_seat_tail.push(assignment.seat);
    }
    KeyedRoster {
        members: NonEmpty {
            head: head.payload,
            tail: ordered_payload_tail,
        },
        keys: NonEmpty {
            head: head.seat,
            tail: ordered_seat_tail,
        },
    }
}

fn assignment_magnitude_refusal<K, S, const N: usize>(
    error: NonEmptyError,
) -> KeyedRosterAssignmentError<K, S, N> {
    match error {
        NonEmptyError::Empty(empty) => KeyedRosterAssignmentError::Empty(empty),
        NonEmptyError::Overflow(overflow) => KeyedRosterAssignmentError::Overflow(overflow),
    }
}

impl<T, const N: usize> Capped<T, N> {
    /// A capped collection that kept its complete lawful offering.
    #[must_use]
    pub const fn all(items: NonEmpty<T, N>) -> Self {
        Self {
            items,
            capping: Capping::Complete,
        }
    }

    /// Keeps the first item and the ordered prefix of the rest that fits, then records the exact omitted count.
    #[must_use]
    pub fn first_n(first: T, rest: impl Iterator<Item = T>) -> Self {
        const {
            assert!(N >= 1, "a capped list under a ceiling that admits no item");
        }
        let mut tail = Vec::new();
        let mut omitted = 0_usize;
        for item in rest {
            if tail.len() < N.saturating_sub(1) {
                tail.push(item);
            } else {
                omitted = omitted.saturating_add(1);
            }
        }
        Self {
            items: NonEmpty { head: first, tail },
            capping: capping_over(omitted),
        }
    }

    /// The items the list kept.
    #[must_use]
    pub const fn items(&self) -> &NonEmpty<T, N> {
        &self.items
    }

    /// Whether the list kept everything offered to it.
    #[must_use]
    pub const fn capping(&self) -> Capping {
        self.capping
    }
}

/// Reads the capping off the exact count of what was dropped.
const fn capping_over(omitted: usize) -> Capping {
    if omitted == 0 {
        Capping::Complete
    } else {
        Capping::Truncated { omitted }
    }
}