type-bridge-contract 2.0.1

Versioned binding-neutral contract primitives for type-bridge
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
//! Context-free canonical migration identities, fingerprints, and schema steps.

use std::fmt;

use serde::{Serialize, Serializer};
use sha2::{Digest as _, Sha256};

use crate::capability::{CapabilityId, CapabilitySet};
use crate::codec::to_canonical_json;
use crate::diagnostic::{Diagnostic, DiagnosticCategory};
use crate::fingerprint::{
    CanonicalizationVersion, Fingerprint, FingerprintDigest, FingerprintDomain,
};
use crate::migration_assertion::{
    AssertionExpectation, MigrationAssertionPlan, MigrationAssertionPlanFingerprint,
};
use crate::schema_delta::{SchemaDelta, SchemaOperation};
use crate::schema_fingerprint::ManagedSemanticSchemaFingerprint;

/// Exact canonical migration format discriminator.
pub const MIGRATION_FORMAT_V1: &str = "typebridge.migration/v1";
// Pre-release wire ledger: migration/v1 gained assertion steps and the manifest
// lowering-profile binding while legacy schema-only canonical bytes stay unchanged.
/// Fingerprint domain for compound migration ledger keys.
pub const MIGRATION_ID_FINGERPRINT_DOMAIN: &str = "typebridge.migration.id";
/// Canonicalization contract for compound migration IDs.
pub const MIGRATION_ID_CANONICALIZATION: &str = "typebridge.migration-id/v1";
/// Fingerprint domain for trusted schema-delta bytes.
pub const SCHEMA_DELTA_FINGERPRINT_DOMAIN: &str = "typebridge.migration.schema-delta";
/// Canonicalization contract for trusted schema deltas.
pub const SCHEMA_DELTA_FINGERPRINT_CANONICALIZATION: &str = "typebridge.schema-delta/v1";
/// Fingerprint domain for ordered schema-only migration plans.
pub const MIGRATION_PLAN_FINGERPRINT_DOMAIN: &str = "typebridge.migration.plan";
/// Canonicalization contract for ordered schema-only migration plans.
pub const MIGRATION_PLAN_FINGERPRINT_CANONICALIZATION: &str = "typebridge.migration-plan/v1";
/// Capability required by a persisted verifier-derived conditional assertion.
pub const CONDITIONAL_RESOLUTION_CAPABILITY: &str = "migration.conditional-resolution";
/// Maximum ASCII byte length of one portable migration identity component.
pub const MAX_MIGRATION_COMPONENT_BYTES: usize = 255;

fn validate_component(
    value: String,
    kind: &'static str,
    allow_leading_digit: bool,
) -> Result<String, Diagnostic> {
    let mut bytes = value.bytes();
    let valid_first = bytes.next().is_some_and(|byte| {
        byte.is_ascii_lowercase() || (allow_leading_digit && byte.is_ascii_digit())
    });
    let valid_rest = bytes.all(|byte| {
        byte.is_ascii_lowercase() || byte.is_ascii_digit() || matches!(byte, b'_' | b'-')
    });
    if value.len() <= MAX_MIGRATION_COMPONENT_BYTES && valid_first && valid_rest {
        Ok(value)
    } else {
        Err(Diagnostic::stable(
            DiagnosticCategory::InvalidContract,
            "invalid_migration_identity_component",
            "migration identity component must be bounded portable lowercase ASCII",
        )
        .with_detail("component_kind", kind))
    }
}

macro_rules! migration_component {
    ($name:ident, $doc:literal, $kind:literal, $leading_digit:expr) => {
        #[doc = $doc]
        #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
        #[serde(transparent)]
        pub struct $name(String);

        impl $name {
            /// Validate and construct this portable migration identity component.
            pub fn new(value: impl Into<String>) -> Result<Self, Diagnostic> {
                Ok(Self(validate_component(
                    value.into(),
                    $kind,
                    $leading_digit,
                )?))
            }

            /// Return the canonical component spelling.
            pub fn as_str(&self) -> &str {
                &self.0
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str(self.as_str())
            }
        }
    };
}

migration_component!(
    MigrationAppLabel,
    "A portable application label forming the first component of migration identity.",
    "app_label",
    false
);
migration_component!(
    MigrationName,
    "A portable filename-stem migration name forming the second identity component.",
    "name",
    true
);
migration_component!(
    MigrationStepId,
    "A stable identity unique within one ordered migration plan.",
    "step_id",
    false
);

/// A typed compound migration identity; it is never delimiter-joined.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
pub struct MigrationId {
    app_label: MigrationAppLabel,
    name: MigrationName,
}

impl MigrationId {
    /// Validate and construct a compound migration identity.
    pub fn new(app_label: impl Into<String>, name: impl Into<String>) -> Result<Self, Diagnostic> {
        Ok(Self {
            app_label: MigrationAppLabel::new(app_label)?,
            name: MigrationName::new(name)?,
        })
    }

    /// Construct from already validated components.
    #[must_use]
    pub const fn from_components(app_label: MigrationAppLabel, name: MigrationName) -> Self {
        Self { app_label, name }
    }

    /// Return the application-label component.
    pub const fn app_label(&self) -> &MigrationAppLabel {
        &self.app_label
    }

    /// Return the filename-stem name component.
    pub const fn name(&self) -> &MigrationName {
        &self.name
    }

    /// Return exact canonical compound-ID bytes.
    pub fn canonical_bytes(&self) -> Result<Vec<u8>, Diagnostic> {
        to_canonical_json(self)
    }

    /// Derive the domain-separated ledger key for this compound identity.
    pub fn ledger_key(&self) -> Result<MigrationLedgerKey, Diagnostic> {
        MigrationLedgerKey::compute(self)
    }
}

/// The closed canonical migration format admitted by this contract revision.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MigrationFormat;

impl MigrationFormat {
    /// The initial canonical migration format.
    pub const V1: Self = Self;

    /// Validate an exact migration format discriminator.
    pub fn new(value: &str) -> Result<Self, Diagnostic> {
        if value == MIGRATION_FORMAT_V1 {
            Ok(Self::V1)
        } else {
            Err(Diagnostic::stable(
                DiagnosticCategory::InvalidContract,
                "unsupported_migration_format",
                "canonical migration format is not supported",
            )
            .with_detail("actual", value.to_owned())
            .with_detail("supported", MIGRATION_FORMAT_V1))
        }
    }

    /// Return the exact wire discriminator.
    pub const fn as_str(self) -> &'static str {
        MIGRATION_FORMAT_V1
    }
}

impl Serialize for MigrationFormat {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

/// Retry policy supported by the schema-only contract slice.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RetryPolicy {
    /// Never replay without a higher-layer verified recovery decision.
    Never,
}

/// Recovery policy supported by the schema-only contract slice.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RecoveryPolicy {
    /// Stop and require an explicit verified operator decision.
    OperatorRequired,
}

/// Raw full SHA-256 of exact canonical manifest bytes, stored outside the manifest.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct MigrationManifestDigest(FingerprintDigest);

impl MigrationManifestDigest {
    /// Hash exact canonical manifest bytes without adding a fingerprint domain preimage.
    #[must_use]
    pub fn compute(canonical_manifest_bytes: &[u8]) -> Self {
        let digest = Sha256::digest(canonical_manifest_bytes);
        let hex = digest
            .iter()
            .map(|byte| format!("{byte:02x}"))
            .collect::<String>();
        Self(
            FingerprintDigest::from_hex(&hex)
                .expect("SHA-256 always produces a valid lowercase 32-byte digest"),
        )
    }

    /// Parse exactly 64 lowercase hexadecimal characters.
    pub fn from_hex(value: &str) -> Result<Self, Diagnostic> {
        FingerprintDigest::from_hex(value).map(Self)
    }

    /// Return lowercase hexadecimal text.
    #[must_use]
    pub fn to_hex(self) -> String {
        self.0.to_hex()
    }

    /// Return the raw 32 digest bytes.
    #[must_use]
    pub const fn bytes(self) -> [u8; 32] {
        self.0.bytes()
    }
}

/// Domain-separated ledger key derived from canonical compound migration-ID bytes.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct MigrationLedgerKey(Fingerprint);

impl MigrationLedgerKey {
    /// Compute one stable ledger key without flattening identity components.
    pub fn compute(id: &MigrationId) -> Result<Self, Diagnostic> {
        Ok(Self(Fingerprint::compute(
            FingerprintDomain::new(MIGRATION_ID_FINGERPRINT_DOMAIN)?,
            CanonicalizationVersion::new(MIGRATION_ID_CANONICALIZATION)?,
            None,
            &id.canonical_bytes()?,
        )))
    }

    /// Return the generic domain-separated fingerprint.
    pub const fn as_fingerprint(&self) -> &Fingerprint {
        &self.0
    }
}

/// Fingerprint of one trusted schema delta's exact canonical bytes.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct SchemaDeltaFingerprint(Fingerprint);

impl SchemaDeltaFingerprint {
    /// Compute the fingerprint of one already validated schema delta.
    pub fn compute(delta: &SchemaDelta) -> Result<Self, Diagnostic> {
        Ok(Self(Fingerprint::compute(
            FingerprintDomain::new(SCHEMA_DELTA_FINGERPRINT_DOMAIN)?,
            CanonicalizationVersion::new(SCHEMA_DELTA_FINGERPRINT_CANONICALIZATION)?,
            None,
            &delta.canonical_bytes()?,
        )))
    }

    /// Return the generic domain-separated fingerprint.
    pub const fn as_fingerprint(&self) -> &Fingerprint {
        &self.0
    }
}

/// The only context-free migration step kind in the schema-only slice.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum MigrationStepKind {
    /// One trusted schema-delta chunk.
    SchemaDelta,
    /// One canonical verifier-derived no-rows assertion.
    Assertion,
}

/// Context-free claims derived exactly from a trusted schema delta.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SchemaDeltaStepContract {
    delta_fingerprint: SchemaDeltaFingerprint,
    id: MigrationStepId,
    recovery: RecoveryPolicy,
    required_capabilities: CapabilitySet,
    retry: RetryPolicy,
    #[serde(skip_serializing_if = "Option::is_none")]
    reverse: Option<SchemaDelta>,
    source_semantics: ManagedSemanticSchemaFingerprint,
    target_semantics: ManagedSemanticSchemaFingerprint,
}

impl SchemaDeltaStepContract {
    /// Derive all step claims and validate an optional exact operation-wise inverse.
    pub fn new(
        id: MigrationStepId,
        delta: &SchemaDelta,
        reverse: Option<SchemaDelta>,
    ) -> Result<Self, Diagnostic> {
        if let Some(candidate) = &reverse {
            let inverse_operations = delta
                .operations()
                .iter()
                .rev()
                .flat_map(SchemaOperation::inverse)
                .collect();
            let expected = SchemaDelta::new(
                delta.format(),
                delta.target().clone(),
                delta.source().clone(),
                inverse_operations,
            )?;
            if candidate != &expected {
                return Err(Diagnostic::stable(
                    DiagnosticCategory::InvalidContract,
                    "schema_delta_step_inverse_mismatch",
                    "schema step reverse delta is not the exact operation-wise inverse",
                ));
            }
        }
        Ok(Self {
            delta_fingerprint: SchemaDeltaFingerprint::compute(delta)?,
            id,
            recovery: RecoveryPolicy::OperatorRequired,
            required_capabilities: delta.required_capabilities().clone(),
            retry: RetryPolicy::Never,
            reverse,
            source_semantics: delta.source().managed_semantic_schema().clone(),
            target_semantics: delta.target().managed_semantic_schema().clone(),
        })
    }

    /// Return the step-local identity.
    pub const fn id(&self) -> &MigrationStepId {
        &self.id
    }

    /// Return the exact source managed-semantic precondition.
    pub const fn source_semantics(&self) -> &ManagedSemanticSchemaFingerprint {
        &self.source_semantics
    }

    /// Return the exact target managed-semantic postcondition.
    pub const fn target_semantics(&self) -> &ManagedSemanticSchemaFingerprint {
        &self.target_semantics
    }

    /// Return capabilities derived from the trusted delta.
    pub const fn required_capabilities(&self) -> &CapabilitySet {
        &self.required_capabilities
    }

    /// Return the exact schema-delta fingerprint.
    pub const fn delta_fingerprint(&self) -> &SchemaDeltaFingerprint {
        &self.delta_fingerprint
    }

    /// Return the fixed no-replay retry policy.
    pub const fn retry(&self) -> RetryPolicy {
        self.retry
    }

    /// Return the fixed operator-required recovery policy.
    pub const fn recovery(&self) -> RecoveryPolicy {
        self.recovery
    }

    /// Return the optional checked exact inverse.
    pub const fn reverse(&self) -> Option<&SchemaDelta> {
        self.reverse.as_ref()
    }
}

/// One trusted schema-only migration step.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SchemaDeltaStep {
    contract: SchemaDeltaStepContract,
    delta: SchemaDelta,
    kind: MigrationStepKind,
}

impl SchemaDeltaStep {
    /// Construct a schema step and derive every context-free contract field.
    pub fn new(
        id: MigrationStepId,
        delta: SchemaDelta,
        reverse: Option<SchemaDelta>,
    ) -> Result<Self, Diagnostic> {
        let contract = SchemaDeltaStepContract::new(id, &delta, reverse)?;
        Ok(Self {
            contract,
            delta,
            kind: MigrationStepKind::SchemaDelta,
        })
    }

    /// Return the fixed schema-delta step kind.
    pub const fn kind(&self) -> MigrationStepKind {
        self.kind
    }

    /// Return the derived step contract.
    pub const fn contract(&self) -> &SchemaDeltaStepContract {
        &self.contract
    }

    /// Return the trusted schema delta.
    pub const fn delta(&self) -> &SchemaDelta {
        &self.delta
    }

    /// Return exact canonical step bytes.
    pub fn canonical_bytes(&self) -> Result<Vec<u8>, Diagnostic> {
        to_canonical_json(self)
    }
}

/// Context-free claims derived exactly from one canonical assertion plan.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct AssertionStepContract {
    id: MigrationStepId,
    plan_fingerprint: MigrationAssertionPlanFingerprint,
    recovery: RecoveryPolicy,
    required_capabilities: CapabilitySet,
    retry: RetryPolicy,
    source_semantics: ManagedSemanticSchemaFingerprint,
    target_semantics: ManagedSemanticSchemaFingerprint,
}

impl AssertionStepContract {
    fn derive(
        id: MigrationStepId,
        plan: &MigrationAssertionPlan,
        expected: AssertionExpectation,
    ) -> Result<Self, Diagnostic> {
        if expected != AssertionExpectation::NoRows
            || plan.expectation() != AssertionExpectation::NoRows
        {
            return Err(Diagnostic::stable(
                DiagnosticCategory::InvalidContract,
                "migration_assertion_step_expectation_mismatch",
                "persisted migration assertions support only the no-rows expectation",
            ));
        }
        let mut required_capabilities = plan.required_capabilities().clone();
        required_capabilities.insert(
            CapabilityId::new(CONDITIONAL_RESOLUTION_CAPABILITY)
                .expect("the fixed conditional-resolution capability is canonical"),
        );
        Ok(Self {
            id,
            plan_fingerprint: plan.fingerprint()?,
            recovery: RecoveryPolicy::OperatorRequired,
            required_capabilities,
            retry: RetryPolicy::Never,
            source_semantics: plan.managed_semantics().clone(),
            target_semantics: plan.managed_semantics().clone(),
        })
    }

    /// Return the step-local identity.
    pub const fn id(&self) -> &MigrationStepId {
        &self.id
    }

    /// Return the canonical assertion-plan fingerprint.
    pub const fn plan_fingerprint(&self) -> &MigrationAssertionPlanFingerprint {
        &self.plan_fingerprint
    }

    /// Return capabilities derived from the assertion syntax and resolution contract.
    pub const fn required_capabilities(&self) -> &CapabilitySet {
        &self.required_capabilities
    }

    /// Return the fixed no-replay retry policy.
    pub const fn retry(&self) -> RetryPolicy {
        self.retry
    }

    /// Return the fixed operator-required recovery policy.
    pub const fn recovery(&self) -> RecoveryPolicy {
        self.recovery
    }

    /// Return the exact source managed-semantic precondition.
    pub const fn source_semantics(&self) -> &ManagedSemanticSchemaFingerprint {
        &self.source_semantics
    }

    /// Return the identical target managed-semantic postcondition.
    pub const fn target_semantics(&self) -> &ManagedSemanticSchemaFingerprint {
        &self.target_semantics
    }

    /// Assertions never have an independently executable reverse step.
    pub const fn reverse(&self) -> Option<()> {
        None
    }
}

/// Exact ordered migration step algebra. Trusted steps deliberately do not deserialize.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MigrationStep {
    /// One state-changing schema delta.
    SchemaDelta(Box<SchemaDeltaStep>),
    /// One state-preserving verifier-derived assertion.
    Assertion {
        /// Constructor-derived assertion contract.
        contract: Box<AssertionStepContract>,
        /// Canonical typed assertion plan.
        plan: Box<MigrationAssertionPlan>,
        /// Closed expected outcome.
        expected: AssertionExpectation,
    },
}

impl MigrationStep {
    /// Construct a trusted assertion step and derive all contract claims.
    pub fn assertion(
        id: MigrationStepId,
        plan: MigrationAssertionPlan,
        expected: AssertionExpectation,
    ) -> Result<Self, Diagnostic> {
        let contract = AssertionStepContract::derive(id, &plan, expected)?;
        Ok(Self::Assertion {
            contract: Box::new(contract),
            plan: Box::new(plan),
            expected,
        })
    }

    /// Return the closed step discriminator.
    pub const fn kind(&self) -> MigrationStepKind {
        match self {
            Self::SchemaDelta(_) => MigrationStepKind::SchemaDelta,
            Self::Assertion { .. } => MigrationStepKind::Assertion,
        }
    }

    /// Return the unique step-local identity.
    pub const fn id(&self) -> &MigrationStepId {
        match self {
            Self::SchemaDelta(step) => step.contract().id(),
            Self::Assertion { contract, .. } => contract.id(),
        }
    }

    /// Return constructor-derived required capabilities.
    pub const fn required_capabilities(&self) -> &CapabilitySet {
        match self {
            Self::SchemaDelta(step) => step.contract().required_capabilities(),
            Self::Assertion { contract, .. } => contract.required_capabilities(),
        }
    }

    /// Return a schema-delta step when this is state-changing.
    pub fn as_schema_delta(&self) -> Option<&SchemaDeltaStep> {
        match self {
            Self::SchemaDelta(step) => Some(step),
            Self::Assertion { .. } => None,
        }
    }

    /// Return assertion contract, plan, and expectation when state-preserving.
    pub fn as_assertion(
        &self,
    ) -> Option<(
        &AssertionStepContract,
        &MigrationAssertionPlan,
        AssertionExpectation,
    )> {
        match self {
            Self::SchemaDelta(_) => None,
            Self::Assertion {
                contract,
                plan,
                expected,
            } => Some((contract, plan, *expected)),
        }
    }

    /// Rebuild all derived claims and reject an assembled mismatched step.
    pub fn validate(&self) -> Result<(), Diagnostic> {
        let rebuilt = match self {
            Self::SchemaDelta(step) => Self::SchemaDelta(Box::new(SchemaDeltaStep::new(
                step.contract().id().clone(),
                step.delta().clone(),
                step.contract().reverse().cloned(),
            )?)),
            Self::Assertion {
                contract,
                plan,
                expected,
            } => Self::assertion(contract.id().clone(), plan.as_ref().clone(), *expected)?,
        };
        if &rebuilt != self {
            return Err(Diagnostic::stable(
                DiagnosticCategory::Integrity,
                "migration_step_contract_mismatch",
                "migration step claims differ from constructor-derived claims",
            ));
        }
        Ok(())
    }

    /// Return exact canonical heterogeneous step bytes.
    pub fn canonical_bytes(&self) -> Result<Vec<u8>, Diagnostic> {
        to_canonical_json(self)
    }
}

impl From<SchemaDeltaStep> for MigrationStep {
    fn from(step: SchemaDeltaStep) -> Self {
        Self::SchemaDelta(Box::new(step))
    }
}

#[derive(Serialize)]
struct AssertionStepView<'a> {
    contract: &'a AssertionStepContract,
    expected: AssertionExpectation,
    kind: MigrationStepKind,
    plan: &'a MigrationAssertionPlan,
}

impl Serialize for MigrationStep {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::SchemaDelta(step) => step.serialize(serializer),
            Self::Assertion {
                contract,
                plan,
                expected,
            } => AssertionStepView {
                contract,
                expected: *expected,
                kind: MigrationStepKind::Assertion,
                plan,
            }
            .serialize(serializer),
        }
    }
}

#[derive(Serialize)]
struct MigrationPlanView<'a> {
    steps: &'a [MigrationStep],
}

/// Fingerprint of the exact ordered schema-only step algebra.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct MigrationPlanFingerprint(Fingerprint);

impl MigrationPlanFingerprint {
    /// Return canonical bytes for the exact ordered schema-only plan.
    pub fn canonical_plan_bytes(steps: &[MigrationStep]) -> Result<Vec<u8>, Diagnostic> {
        to_canonical_json(&MigrationPlanView { steps })
    }

    /// Compute an ordered plan fingerprint; changing step order changes the digest.
    pub fn compute(steps: &[MigrationStep]) -> Result<Self, Diagnostic> {
        Ok(Self(Fingerprint::compute(
            FingerprintDomain::new(MIGRATION_PLAN_FINGERPRINT_DOMAIN)?,
            CanonicalizationVersion::new(MIGRATION_PLAN_FINGERPRINT_CANONICALIZATION)?,
            None,
            &Self::canonical_plan_bytes(steps)?,
        )))
    }

    /// Return the generic domain-separated fingerprint.
    pub const fn as_fingerprint(&self) -> &Fingerprint {
        &self.0
    }
}