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
//! One shape for a newtype over a field of on-disk flag bits, in the two forms a word takes.
//!
//! Several structures in these formats carry a word whose bits are named, independent flags:
//! the three ext feature words, btrfs's three, an inode's `i_flags`, a FAT directory entry's
//! attribute byte. Each is a newtype over an integer, and each needs the same operations —
//! read the raw word, wrap one, test membership, test emptiness, add flags, remove flags.
//!
//! Written by hand, those come out slightly different every time — one type keeps the whole
//! set, the next drops emptiness, the one after that exposes its inner word where the rest
//! keep it private — and none of the differences means anything to a caller. So [`flag_set!`]
//! writes the operations, and every type has all of them and hides its word.
//!
//! # A word whose bits a caller names
//!
//! Some of those words are also a *vocabulary*: a person writes `extent` or `no-holes` on a
//! command line, a report prints the same word back, and the word has to mean one bit in both
//! directions. `named_flags!` is [`flag_set!`] plus that table — the flags and their names
//! declared once, and `names`, `from_name`, `unknown_bits` and a legible
//! [`Debug`](core::fmt::Debug) generated from it. It is written where a family whose format
//! has such a word is compiled, which is why the name here is not a link.
//!
//! A word nobody names takes [`flag_set!`] alone. An inode's flags and a directory entry's
//! attributes are read and written by this crate and by no user, so a table of names for them
//! would be a vocabulary with no speaker.
//!
//! # What is not a flag set
//!
//! [`AcceptedLoss`](crate::AcceptedLoss) is a set over bits and is deliberately not one of
//! these. Its element is a typed [`Property`](crate::Property) rather than another instance of
//! itself, so its operations take a `Property` — `and(Property)`, `contains(Property)` — and
//! `BitOr` between two sets is not the operation a caller wants. The numbering behind it is an
//! implementation detail nothing outside may depend on, which is the opposite of a flag word,
//! whose whole point is that the bits are the format's.
/// Generate the set operations for a newtype over a field of on-disk flag bits.
///
/// The type is declared by the caller, as a newtype over `$repr` with a **private** field:
/// the raw word is reached through [`bits`] and wrapped through [`from_bits`], so a caller
/// that means to read the on-disk value says so.
///
/// [`bits`]: #
/// [`from_bits`]: #
/// Generate a flag word whose bits carry names, from one table read in both directions.
///
/// The type is declared here as a newtype over `$repr` with a private field, carrying every
/// operation [`flag_set!`] writes, and each flag is declared beside the name it is known by
/// outside this crate:
///
/// ```ignore
/// named_flags! {
/// /// What the word is, and which decision it carries.
/// #[derive(Default)]
/// Incompat: u32 {
/// /// What setting this flag means for the on-disk form.
/// EXTENTS("extent") = 0x0040,
/// }
/// }
/// ```
///
/// One table carries both spellings, so neither can grow without the other. The Rust symbol
/// renders [`Debug`](core::fmt::Debug), which keeps a diagnostic legible without a lookup
/// table; the quoted name drives `names` and `from_name`, and is the vocabulary a person
/// types and the format's own tooling prints.
///
/// Attributes written above the type reach the declaration, so a word needing derives beyond
/// the four every one of them carries — a `Default`, a `Hash`, a `Serialize` behind a
/// feature — asks for them there.
///
/// # Which of the two vocabularies a word takes
///
/// A format usually spells its own flags twice: an all-capitals constant in the header that
/// defines it, and a lowercase option word its tooling accepts on a command line. The name in
/// this table is the one **this crate has to accept as input**, because a word a tool prints
/// and then refuses is the failure the single table exists to prevent — so where the two
/// spellings differ, the option word wins and a report prints it.
///
/// # This or [`named_choice!`](crate::naming::named_choice)
///
/// Both are one vocabulary read in both directions, and what separates them is the shape of
/// the value. A [`named_choice!`](crate::naming::named_choice) names the variants of a closed
/// set, exactly one of which holds at a time; this names the bits of a word, any number of
/// which are set at once. So a choice answers with one name and a word answers with a list,
/// and a word additionally has to answer for the bits it does *not* name — which a closed set
/// has no equivalent of, and which is why `unknown_bits` lives here.
// Compiled where a family whose format carries a *named* flag word is. Two of the four have
// one — a feature word a caller reads and writes — and the other two carry flag words nobody
// names, which is what `flag_set!` alone is for.
) => ;
}
pub use flag_set;
pub use named_flags;