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
//! The identity home's invariant nucleus: every road that reaches a private field.
//!
//! This file is declared inside `types.rs` as its own child, so it sees the fields the declarations keep private and nothing else in the crate does.
//! That is what makes the walls structural rather than reviewed: a road around one of them would have to be written here, and none is.

use super::{
    Anchoring, GENERATOR, GeneratorIdentity, HumanProjection, Identity, OwnerIdentity, Profile,
    Provenance, Role, ShapeVersion, Subject, Transcript, Version,
};
use crate::bounded::{Bounded, Overflow};
use core::fmt;
use core::hash::{Hash, Hasher};
use core::marker::PhantomData;

impl<S: Subject> Identity<S> {
    /// Derive one identity from its complete transcript.
    /// Deterministic and total: every transcript names an identity.
    #[must_use]
    pub fn derived(transcript: Transcript<'_>) -> Self {
        let context = transcript.profile().context_for::<S>(transcript.role());
        Self(
            blake3::derive_key(&context, &transcript.encoded::<S>()),
            PhantomData,
        )
    }

    /// Derive one identity and the record of how it was derived.
    ///
    /// The record is for a value that is going to keep it — one whose identity a reader may be handed on its own and asked to account for.
    /// A caller with nowhere to put one takes [`Identity::derived`], and the record is simply not made rather than made and carried by everything.
    #[must_use]
    pub fn derived_with_provenance(transcript: Transcript<'_>) -> (Self, Provenance) {
        (Self::derived(transcript), transcript.provenance::<S>())
    }

    /// The identity's thirty-two bytes, borrowed for comparison and for rendering.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }
}

impl<S: Subject> PartialEq for Identity<S> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl<S: Subject> Eq for Identity<S> {}

impl<S: Subject> Hash for Identity<S> {
    fn hash<H: Hasher>(&self, into: &mut H) {
        self.0.hash(into);
    }
}

impl<S: Subject> fmt::Debug for Identity<S> {
    fn fmt(&self, into: &mut fmt::Formatter<'_>) -> fmt::Result {
        into.debug_tuple(S::NAME).field(&self.0).finish()
    }
}

impl Version {
    /// The position the grammar's owner assigned.
    #[must_use]
    pub const fn declared(position: u32) -> Self {
        Self(position)
    }

    /// The assigned position.
    #[must_use]
    pub const fn position(self) -> u32 {
        self.0
    }
}

impl Profile {
    /// One grammar, under the stem of whoever owns it, at one version.
    #[must_use]
    pub const fn declared(stem: &'static str, name: &'static str, version: Version) -> Self {
        Self {
            stem,
            name,
            version,
        }
    }

    /// The stem of whoever owns this grammar.
    #[must_use]
    pub const fn stem(self) -> &'static str {
        self.stem
    }

    /// The grammar's declared name.
    #[must_use]
    pub const fn name(self) -> &'static str {
        self.name
    }

    /// The declared version — this grammar's own position, and no other grammar's.
    #[must_use]
    pub const fn version(self) -> Version {
        self.version
    }

    /// The derive-key context one SUBJECT derives under, at one role.
    ///
    /// # Authority
    ///
    /// **The subject is the TYPE's and never an argument.** A road that took the subject as text would let a caller derive under any key space it could spell — including one another subject already occupies — and the typed identity above it would be a promise the encoder never had to keep.
    #[must_use]
    pub fn context_for<S: Subject>(self, role: Role) -> String {
        self.context_over(S::STEM, S::NAME, role)
    }

    /// The same context, over a subject a derivation record already carries.
    ///
    /// Crate-internal, with one caller: [`Provenance::context`], which renders what a derivation recorded rather than performing one.
    pub(crate) fn context_over(self, subject_stem: &str, subject: &str, role: Role) -> String {
        let Self {
            stem,
            name,
            version,
        } = self;
        let position = version.position();
        let seat = role.name();
        format!("{stem}/{name}/v{position}/{subject_stem}/{subject}/{seat}")
    }
}

impl ShapeVersion {
    /// The shape position the generator's owner assigned.
    #[must_use]
    pub const fn declared(position: u32) -> Self {
        Self(position)
    }

    /// The assigned position.
    #[must_use]
    pub const fn position(self) -> u32 {
        self.0
    }
}

impl GeneratorIdentity {
    /// The generator under its declared name, rendered shape, and recorded package version.
    #[must_use]
    pub const fn declared(name: &'static str, shape: ShapeVersion, package: &'static str) -> Self {
        Self {
            name,
            shape,
            package,
        }
    }

    /// The generator's stable name. One of the two facts a staleness comparison reads.
    #[must_use]
    pub const fn name(self) -> &'static str {
        self.name
    }

    /// The rendered shape's version. The other fact a staleness comparison reads.
    #[must_use]
    pub const fn shape(self) -> ShapeVersion {
        self.shape
    }

    /// The package version, recorded for a reader and compared by nothing.
    #[must_use]
    pub const fn package_version(self) -> &'static str {
        self.package
    }

    /// Whether two generator identities name the same generator rendering the same shape.
    ///
    /// The comparison a staleness reading wants, and the reason it is a named road rather than `==`: equality compares the package version too, and the package version moves for reasons no output noticed.
    #[must_use]
    pub fn same_shape(self, other: Self) -> bool {
        self.name == other.name && self.shape == other.shape
    }
}

impl<'material> Transcript<'material> {
    /// Write a transcript under a grammar the caller names.
    ///
    /// The road for a preimage whose grammar is the caller's own; the four roads below name a role and take this compiler's grammar for it.
    #[must_use]
    pub const fn under_profile(
        profile: Profile,
        role: Role,
        anchoring: Anchoring,
        material: &'material [u8],
        position: u32,
    ) -> Self {
        Self {
            profile,
            generator: GENERATOR,
            role,
            anchoring,
            material,
            position,
        }
    }

    /// Write a transcript under an anchoring the caller already decided.
    ///
    /// The road for a mint site whose anchor depends on a typed posture rather than on which family of identity it holds — a plan hangs off whatever caused it, and what caused it is a sum type.
    #[must_use]
    pub const fn under(
        role: Role,
        anchoring: Anchoring,
        material: &'material [u8],
        position: u32,
    ) -> Self {
        Self::under_profile(role.profile(), role, anchoring, material, position)
    }

    /// Derive under no anchor at all — the root of one derivation chain.
    #[must_use]
    pub const fn rooted(role: Role, material: &'material [u8], position: u32) -> Self {
        Self::under(role, Anchoring::Rooted, material, position)
    }

    /// Derive under an identity a CONSUMER minted.
    #[must_use]
    pub const fn under_owner(
        role: Role,
        anchor: &OwnerIdentity,
        material: &'material [u8],
        position: u32,
    ) -> Self {
        Self::under(
            role,
            Anchoring::UnderOwner(anchor.bytes),
            material,
            position,
        )
    }

    /// Derive under another identity this compiler derived.
    #[must_use]
    pub fn under_projection<S: Subject>(
        role: Role,
        anchor: &Identity<S>,
        material: &'material [u8],
        position: u32,
    ) -> Self {
        Self::under(
            role,
            Anchoring::UnderProjection(*anchor.as_bytes()),
            material,
            position,
        )
    }

    /// The grammar this transcript is written under.
    #[must_use]
    pub const fn profile(&self) -> Profile {
        self.profile
    }

    /// The generator this transcript records.
    ///
    /// Recorded and never written: the encoding carries no member for it, and this road exists so the derivation record can.
    #[must_use]
    pub const fn generator(&self) -> GeneratorIdentity {
        self.generator
    }

    /// The seat this transcript stands in.
    #[must_use]
    pub const fn role(&self) -> Role {
        self.role
    }

    /// What this transcript hangs off.
    #[must_use]
    pub const fn anchoring(&self) -> Anchoring {
        self.anchoring
    }

    /// The varying material, at full length.
    #[must_use]
    pub const fn material(&self) -> &'material [u8] {
        self.material
    }

    /// The position inside the anchor's declared sequence.
    #[must_use]
    pub const fn position(&self) -> u32 {
        self.position
    }

    /// The derivation record this transcript leaves for one identity subject.
    #[must_use]
    pub fn provenance<S: Subject>(&self) -> Provenance {
        Provenance {
            subject_stem: S::STEM,
            subject: S::NAME,
            role: self.role,
            profile: self.profile,
            generator: self.generator,
            anchoring: self.anchoring,
            material_length: u64::try_from(self.material.len()).unwrap_or(u64::MAX),
            position: self.position,
        }
    }
}

impl Provenance {
    /// The stem of whoever owns the subject this derivation named.
    #[must_use]
    pub const fn subject_stem(&self) -> &'static str {
        self.subject_stem
    }

    /// The identity subject this derivation named.
    #[must_use]
    pub const fn subject(&self) -> &'static str {
        self.subject
    }

    /// The seat it stood in.
    #[must_use]
    pub const fn role(&self) -> Role {
        self.role
    }

    /// The grammar and version it was derived under.
    #[must_use]
    pub const fn profile(&self) -> Profile {
        self.profile
    }

    /// The generator that derived it.
    #[must_use]
    pub const fn generator(&self) -> GeneratorIdentity {
        self.generator
    }

    /// What it hung off, anchor included.
    #[must_use]
    pub const fn anchoring(&self) -> Anchoring {
        self.anchoring
    }

    /// How many bytes of material went into the transcript.
    #[must_use]
    pub const fn material_length(&self) -> u64 {
        self.material_length
    }

    /// The position inside the anchor's declared sequence.
    #[must_use]
    pub const fn position(&self) -> u32 {
        self.position
    }

    /// The derive-key context this derivation ran under.
    #[must_use]
    pub fn context(&self) -> String {
        self.profile
            .context_over(self.subject_stem, self.subject, self.role)
    }

    /// Whether this derivation was recorded under the generator and rendered shape this crate declares today.
    ///
    /// # Nonclaims
    ///
    /// A `false` says a different generator shape produced the record.
    /// It says nothing about whether the material moved, whether the identity would re-derive the same, or whether anything needs redoing.
    #[must_use]
    pub fn under_current_shape(&self) -> bool {
        self.generator.same_shape(GENERATOR)
    }
}

impl HumanProjection {
    /// Render one bounded human projection.
    ///
    /// # Errors
    ///
    /// Returns [`Overflow`] when the rendering runs past [`HUMAN_TEXT_LIMIT`](super::HUMAN_TEXT_LIMIT).
    /// A projection that does not fit refuses rather than truncating: a silently cut explanation is a false one.
    pub fn projected(text: &str) -> Result<Self, Overflow> {
        Bounded::new(text.as_bytes().to_vec()).map(Self)
    }

    /// The seam behind [`human_projection!`](crate::identity::human_projection), which is the only road to it.
    ///
    /// The rendering arrives as a fixed-width array, and the width is the array's own TYPE, so this road carries no runtime count, returns no refusal, and has no branch where a rendering that did not fit becomes an empty one.
    #[must_use]
    pub(crate) fn proven<const N: usize>(rendered: [u8; N]) -> Self {
        Self(Bounded::from_array(rendered))
    }

    /// The empty rendering.
    /// Total: a caller with nothing to say for a person still owes a value rather than a hole.
    #[must_use]
    pub fn empty() -> Self {
        Self(Bounded::empty())
    }

    /// The rendering's byte length.
    #[must_use]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Whether the rendering carries no bytes.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// The rendering, for a caller to SHOW a person.
    ///
    /// The one lawful use of the bytes and a one-way road out: a frontend that must put a sentence in front of somebody calls this, and nothing inside the compiler calls it at all.
    #[must_use]
    pub fn shown(&self) -> String {
        String::from_utf8_lossy(self.0.as_slice()).into_owned()
    }
}