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
//! OpticKind — the taxonomy of optic field annotations.
//!
//! Six kinds form a lattice under composition (`then_*`):
//!
//! ```text
//! Iso
//! / \
//! Lens Prism
//! \ /
//! Traversal
//! |
//! Fold Setter
//! ```
//!
//! The `compose` method implements the cross-tier composition table.
/// The kind of optic a field is annotated with.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OpticKind {
/// Round-trip lossless. `T: Into<U> + From<U>`.
Iso,
/// Total, bidirectional access. Always present.
Lens,
/// Partial access. May be absent (`Option<T>`, `Result<T, E>`).
Prism,
/// Multiple targets. `Vec<T>`, slices.
Traversal,
/// Read-only collapse. Cannot set back.
Fold,
/// Write-only. Cannot read.
Setter,
}
/// Metadata for a single optic-annotated field.
#[derive(Debug, Clone)]
pub struct FieldOptic {
/// The field name as written in the struct.
pub name: &'static str,
/// Which optic kind the field was annotated with.
pub kind: OpticKind,
}
impl OpticKind {
/// The `then_*` composition table. Returns the result of composing
/// `self` with `other` in sequence.
///
/// The rule: composition moves DOWN the lattice. Iso is identity.
/// Fold and Setter absorb everything.
pub fn compose(self, other: OpticKind) -> OpticKind {
use OpticKind::*;
match (self, other) {
// Iso is the identity element
(Iso, x) | (x, Iso) => x,
// Lens . Lens = Lens (total . total = total)
(Lens, Lens) => Lens,
// Lens . Prism or Prism . Lens = Prism (partial wins)
(Lens, Prism) | (Prism, Lens) => Prism,
// Lens . Traversal or Traversal . Lens = Traversal
(Lens, Traversal) | (Traversal, Lens) => Traversal,
// Prism . Prism = Prism
(Prism, Prism) => Prism,
// Prism . Traversal or Traversal . Prism = Traversal
(Prism, Traversal) | (Traversal, Prism) => Traversal,
// Traversal . Traversal = Traversal
(Traversal, Traversal) => Traversal,
// Fold absorbs everything
(Fold, _) | (_, Fold) => Fold,
// Setter absorbs everything
(Setter, _) | (_, Setter) => Setter,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn iso_is_identity() {
use OpticKind::*;
for kind in [Iso, Lens, Prism, Traversal, Fold, Setter] {
assert_eq!(Iso.compose(kind), kind);
assert_eq!(kind.compose(Iso), kind);
}
}
#[test]
fn lens_lens_is_lens() {
assert_eq!(OpticKind::Lens.compose(OpticKind::Lens), OpticKind::Lens);
}
#[test]
fn lens_prism_is_prism() {
assert_eq!(OpticKind::Lens.compose(OpticKind::Prism), OpticKind::Prism);
assert_eq!(OpticKind::Prism.compose(OpticKind::Lens), OpticKind::Prism);
}
#[test]
fn prism_traversal_is_traversal() {
assert_eq!(
OpticKind::Prism.compose(OpticKind::Traversal),
OpticKind::Traversal
);
assert_eq!(
OpticKind::Traversal.compose(OpticKind::Prism),
OpticKind::Traversal
);
}
#[test]
fn fold_absorbs_all() {
use OpticKind::*;
for kind in [Iso, Lens, Prism, Traversal, Fold, Setter] {
assert_eq!(Fold.compose(kind), Fold);
assert_eq!(kind.compose(Fold), Fold);
}
}
#[test]
fn setter_absorbs_all() {
use OpticKind::*;
for kind in [Iso, Lens, Prism, Traversal, Setter] {
assert_eq!(Setter.compose(kind), Setter);
assert_eq!(kind.compose(Setter), Setter);
}
}
#[test]
fn composition_table_full() {
use OpticKind::*;
assert_eq!(Lens.compose(Lens), Lens);
assert_eq!(Lens.compose(Prism), Prism);
assert_eq!(Prism.compose(Traversal), Traversal);
assert_eq!(Iso.compose(Lens), Lens);
assert_eq!(Fold.compose(Lens), Fold);
}
#[test]
fn field_optic_metadata() {
let field = FieldOptic {
name: "status",
kind: OpticKind::Lens,
};
assert_eq!(field.name, "status");
assert_eq!(field.kind, OpticKind::Lens);
}
}