vsf 0.3.4

Versatile Storage Format
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
//! Type constraints for VSF schema validation
//!
//! TypeConstraint provides a flexible way to validate VsfType values without
//! duplicating the VsfType enum. Instead of parallel type systems, we use
//! pattern-based constraints that can match categories, specific types, or
//! apply custom validation logic.

use super::validate::{ValidationError, ValidationResult};
use crate::types::{EtType, VsfType};
use std::fmt;
use std::sync::Arc;

/// Type constraint for VSF schema fields
///
/// Constraints validate VsfType values using pattern matching rather than
/// creating a parallel type system. This ensures complete coverage of all
/// VsfType variants including future additions.
#[derive(Clone)]
pub enum TypeConstraint {
    // === CATEGORY CONSTRAINTS (most common) ===
    /// Any unsigned integer type (u0, u, u3-u7)
    AnyUnsigned,
    /// Any signed integer type (i, i3-i7)
    AnySigned,
    /// Any integer type (unsigned or signed)
    AnyInteger,
    /// Any float type (f5, f6)
    AnyFloat,
    /// Any numeric type (integer or float)
    AnyNumeric,
    /// Any complex number (j5, j6)
    AnyComplex,

    // === EAGLE TIME VARIANTS ===
    /// Any Eagle Time type (e(EtType::*))
    AnyEagleTime,
    /// Eagle Time with specific backing type constraint
    EagleTime(Box<TypeConstraint>),

    // === STRING TYPES ===
    /// Any string type (x, d, l)
    AnyString,
    /// UTF-8 text (x) - User-facing Unicode text
    Utf8Text,
    /// Dictionary key (d) - Internal naming (section names, field names, keys)
    DictKey,
    /// ASCII text (l) - User-facing ASCII-only text
    AsciiText,

    // === HASH TYPES ===
    /// Any hash type (hp, hb, hs, hm, hg)
    AnyHash,
    /// BLAKE3 provenance hash (hp)
    Blake3Provenance,
    /// BLAKE3 rolling hash (hb)
    Blake3Rolling,
    /// SHA hash (hs) - SHA-256, SHA-512, etc.
    ShaHash,
    /// Smear hash (hm) - XOR of BLAKE3 + SHA3-256 + SHA-512
    Smear,
    /// Spaghetti hash (hg) - domain-separated, maximally weird mixing
    Spaghetti,

    // === KEY TYPES ===
    /// Any cryptographic key (ke, kx, kp, kc, ka)
    AnyKey,
    /// Ed25519 public key (ke)
    Ed25519Key,
    /// X25519 key (kx)
    X25519Key,
    /// ECDSA P-256 key (kp)
    EcdsaP256Key,
    /// Curve25519 key (kc)
    Curve25519Key,
    /// AES key (ka)
    AesKey,

    // === SIGNATURE TYPES ===
    /// Any signature type (ge, gp)
    AnySignature,
    /// Ed25519 signature (ge)
    Ed25519Sig,
    /// ECDSA P-256 signature (gp)
    EcdsaP256Sig,

    // === TENSOR TYPES ===
    /// Any contiguous tensor with element constraint (t_*)
    Tensor(Box<TypeConstraint>),
    /// Any strided tensor with element constraint (q_*)
    StridedTensor(Box<TypeConstraint>),

    // === SPIRIX TYPES ===
    #[cfg(feature = "spirix")]
    /// Any Spirix scalar (s33-s77)
    AnyScalar,
    #[cfg(feature = "spirix")]
    /// Any Spirix circle (c33-c77)
    AnyCircle,

    // === COLOUR TYPES ===
    /// Any colour type (r*)
    AnyColour,

    // === WRAPPED DATA ===
    /// Wrapped data with specific encoding byte
    Wrapped(u8),
    /// Any wrapped data (v)
    AnyWrapped,

    // === COMPOSITE CONSTRAINTS ===
    /// Value must match ANY of these constraints (OR)
    OneOf(Vec<TypeConstraint>),
    /// Value must match ALL of these constraints (AND)
    AllOf(Vec<TypeConstraint>),

    // === CUSTOM VALIDATION ===
    /// Custom predicate function for complex validation
    Custom {
        name: &'static str,
        description: &'static str,
        validator: Arc<dyn Fn(&VsfType) -> bool + Send + Sync>,
    },

    // === PERMISSIVE ===
    /// Accept any VsfType
    Any,
}

impl TypeConstraint {
    /// Validate a VsfType against this constraint
    pub fn validate(&self, value: &VsfType) -> ValidationResult<()> {
        if self.matches(value) {
            Ok(())
        } else {
            Err(ValidationError::TypeConstraintViolation {
                constraint: self.description(),
                got: vsf_type_name(value),
            })
        }
    }

    /// Check if a VsfType matches this constraint
    pub fn matches(&self, value: &VsfType) -> bool {
        match self {
            TypeConstraint::AnyUnsigned => matches!(
                value,
                VsfType::u0(_)
                    | VsfType::u(_, _)
                    | VsfType::u3(_)
                    | VsfType::u4(_)
                    | VsfType::u5(_)
                    | VsfType::u6(_)
                    | VsfType::u7(_)
            ),

            TypeConstraint::AnySigned => matches!(
                value,
                VsfType::i(_)
                    | VsfType::i3(_)
                    | VsfType::i4(_)
                    | VsfType::i5(_)
                    | VsfType::i6(_)
                    | VsfType::i7(_)
            ),

            TypeConstraint::AnyInteger => {
                TypeConstraint::AnyUnsigned.matches(value)
                    || TypeConstraint::AnySigned.matches(value)
            }

            TypeConstraint::AnyFloat => matches!(value, VsfType::f5(_) | VsfType::f6(_)),

            TypeConstraint::AnyNumeric => {
                TypeConstraint::AnyInteger.matches(value) || TypeConstraint::AnyFloat.matches(value)
            }

            TypeConstraint::AnyComplex => matches!(value, VsfType::j5(_) | VsfType::j6(_)),

            TypeConstraint::AnyEagleTime => matches!(value, VsfType::e(_)),

            TypeConstraint::EagleTime(inner) => {
                if let VsfType::e(et) = value {
                    match et {
                        EtType::u(_) => inner.matches(&VsfType::u6(0)),
                        EtType::i(_) => inner.matches(&VsfType::i6(0)),
                        EtType::f5(_) => inner.matches(&VsfType::f5(0.0)),
                        EtType::f6(_) => inner.matches(&VsfType::f6(0.0)),
                    }
                } else {
                    false
                }
            }

            TypeConstraint::AnyString => {
                matches!(value, VsfType::x(_) | VsfType::d(_) | VsfType::l(_))
            }

            TypeConstraint::Utf8Text => matches!(value, VsfType::x(_)),
            TypeConstraint::DictKey => matches!(value, VsfType::d(_)),
            TypeConstraint::AsciiText => matches!(value, VsfType::l(_)),

            TypeConstraint::AnyHash => {
                matches!(
                    value,
                    VsfType::hp(_)
                        | VsfType::hb(_)
                        | VsfType::hs(_)
                        | VsfType::hm(_)
                        | VsfType::hg(_)
                        | VsfType::hP(_)
                )
            }

            TypeConstraint::Blake3Provenance => matches!(value, VsfType::hp(_)),
            TypeConstraint::Blake3Rolling => matches!(value, VsfType::hb(_)),
            TypeConstraint::ShaHash => matches!(value, VsfType::hs(_)),
            TypeConstraint::Smear => matches!(value, VsfType::hm(_)),
            TypeConstraint::Spaghetti => matches!(value, VsfType::hg(_)),

            TypeConstraint::AnyKey => matches!(
                value,
                VsfType::ke(_) | VsfType::kx(_) | VsfType::kp(_) | VsfType::kc(_) | VsfType::ka(_)
            ),

            TypeConstraint::Ed25519Key => matches!(value, VsfType::ke(_)),
            TypeConstraint::X25519Key => matches!(value, VsfType::kx(_)),
            TypeConstraint::EcdsaP256Key => matches!(value, VsfType::kp(_)),
            TypeConstraint::Curve25519Key => matches!(value, VsfType::kc(_)),
            TypeConstraint::AesKey => matches!(value, VsfType::ka(_)),

            TypeConstraint::AnySignature => matches!(value, VsfType::ge(_) | VsfType::gp(_)),

            TypeConstraint::Ed25519Sig => matches!(value, VsfType::ge(_)),
            TypeConstraint::EcdsaP256Sig => matches!(value, VsfType::gp(_)),

            TypeConstraint::Tensor(_elem_constraint) => {
                // Check if it's any t_* variant
                // TODO: Also validate element type with elem_constraint
                matches!(
                    value,
                    VsfType::t_u0(_)
                        | VsfType::t_u3(_)
                        | VsfType::t_u4(_)
                        | VsfType::t_u5(_)
                        | VsfType::t_u6(_)
                        | VsfType::t_u7(_)
                        | VsfType::t_i3(_)
                        | VsfType::t_i4(_)
                        | VsfType::t_i5(_)
                        | VsfType::t_i6(_)
                        | VsfType::t_i7(_)
                        | VsfType::t_f5(_)
                        | VsfType::t_f6(_)
                        | VsfType::t_j5(_)
                        | VsfType::t_j6(_)
                )
            }

            TypeConstraint::StridedTensor(_elem_constraint) => {
                // Check if it's any q_* variant
                matches!(
                    value,
                    VsfType::q_u0(_)
                        | VsfType::q_u3(_)
                        | VsfType::q_u4(_)
                        | VsfType::q_u5(_)
                        | VsfType::q_u6(_)
                        | VsfType::q_u7(_)
                        | VsfType::q_i3(_)
                        | VsfType::q_i4(_)
                        | VsfType::q_i5(_)
                        | VsfType::q_i6(_)
                        | VsfType::q_i7(_)
                        | VsfType::q_f5(_)
                        | VsfType::q_f6(_)
                        | VsfType::q_j5(_)
                        | VsfType::q_j6(_)
                )
            }

            #[cfg(feature = "spirix")]
            TypeConstraint::AnyScalar => {
                matches!(
                    value,
                    VsfType::s33(_)
                        | VsfType::s34(_)
                        | VsfType::s35(_)
                        | VsfType::s36(_)
                        | VsfType::s37(_)
                        | VsfType::s43(_)
                        | VsfType::s44(_)
                        | VsfType::s45(_)
                        | VsfType::s46(_)
                        | VsfType::s47(_)
                        | VsfType::s53(_)
                        | VsfType::s54(_)
                        | VsfType::s55(_)
                        | VsfType::s56(_)
                        | VsfType::s57(_)
                        | VsfType::s63(_)
                        | VsfType::s64(_)
                        | VsfType::s65(_)
                        | VsfType::s66(_)
                        | VsfType::s67(_)
                        | VsfType::s73(_)
                        | VsfType::s74(_)
                        | VsfType::s75(_)
                        | VsfType::s76(_)
                        | VsfType::s77(_)
                )
            }

            #[cfg(feature = "spirix")]
            TypeConstraint::AnyCircle => {
                matches!(
                    value,
                    VsfType::c33(_)
                        | VsfType::c34(_)
                        | VsfType::c35(_)
                        | VsfType::c36(_)
                        | VsfType::c37(_)
                        | VsfType::c43(_)
                        | VsfType::c44(_)
                        | VsfType::c45(_)
                        | VsfType::c46(_)
                        | VsfType::c47(_)
                        | VsfType::c53(_)
                        | VsfType::c54(_)
                        | VsfType::c55(_)
                        | VsfType::c56(_)
                        | VsfType::c57(_)
                        | VsfType::c63(_)
                        | VsfType::c64(_)
                        | VsfType::c65(_)
                        | VsfType::c66(_)
                        | VsfType::c67(_)
                        | VsfType::c73(_)
                        | VsfType::c74(_)
                        | VsfType::c75(_)
                        | VsfType::c76(_)
                        | VsfType::c77(_)
                )
            }

            TypeConstraint::AnyColour => vsf_type_name(value).starts_with('r'),

            TypeConstraint::Wrapped(encoding) => {
                if let VsfType::v(enc, _) = value {
                    enc == encoding
                } else {
                    false
                }
            }

            TypeConstraint::AnyWrapped => matches!(value, VsfType::v(_, _)),

            TypeConstraint::OneOf(constraints) => constraints.iter().any(|c| c.matches(value)),

            TypeConstraint::AllOf(constraints) => constraints.iter().all(|c| c.matches(value)),

            TypeConstraint::Custom { validator, .. } => validator(value),

            TypeConstraint::Any => true,
        }
    }

    /// Get a human-readable description of this constraint
    pub fn description(&self) -> String {
        match self {
            TypeConstraint::AnyUnsigned => "any unsigned integer".into(),
            TypeConstraint::AnySigned => "any signed integer".into(),
            TypeConstraint::AnyInteger => "any integer".into(),
            TypeConstraint::AnyFloat => "any float (f5/f6)".into(),
            TypeConstraint::AnyNumeric => "any numeric type".into(),
            TypeConstraint::AnyComplex => "any complex number".into(),
            TypeConstraint::AnyEagleTime => "Eagle Time (any backing)".into(),
            TypeConstraint::EagleTime(inner) => {
                format!("Eagle Time with {} backing", inner.description())
            }
            TypeConstraint::AnyString => "any string (x/d/l)".into(),
            TypeConstraint::Utf8Text => "UTF-8 text (x)".into(),
            TypeConstraint::DictKey => "dictionary key (d)".into(),
            TypeConstraint::AsciiText => "ASCII text (l)".into(),
            TypeConstraint::AnyHash => "any hash type".into(),
            TypeConstraint::Blake3Provenance => "BLAKE3 provenance hash (hp)".into(),
            TypeConstraint::Blake3Rolling => "BLAKE3 rolling hash (hb)".into(),
            TypeConstraint::ShaHash => "SHA hash (hs)".into(),
            TypeConstraint::Smear => "smear hash (hm)".into(),
            TypeConstraint::Spaghetti => "spaghetti hash (hg)".into(),
            TypeConstraint::AnyKey => "any cryptographic key".into(),
            TypeConstraint::Ed25519Key => "Ed25519 public key (ke)".into(),
            TypeConstraint::X25519Key => "X25519 key (kx)".into(),
            TypeConstraint::EcdsaP256Key => "ECDSA P-256 key (kp)".into(),
            TypeConstraint::Curve25519Key => "Curve25519 key (kc)".into(),
            TypeConstraint::AesKey => "AES key (ka)".into(),
            TypeConstraint::AnySignature => "any signature".into(),
            TypeConstraint::Ed25519Sig => "Ed25519 signature (ge)".into(),
            TypeConstraint::EcdsaP256Sig => "ECDSA P-256 signature (gp)".into(),
            TypeConstraint::Tensor(inner) => format!("tensor of {}", inner.description()),
            TypeConstraint::StridedTensor(inner) => {
                format!("strided tensor of {}", inner.description())
            }
            #[cfg(feature = "spirix")]
            TypeConstraint::AnyScalar => "any Spirix scalar".into(),
            #[cfg(feature = "spirix")]
            TypeConstraint::AnyCircle => "any Spirix circle".into(),
            TypeConstraint::AnyColour => "any colour type".into(),
            TypeConstraint::Wrapped(enc) => format!("wrapped data (encoding {})", enc),
            TypeConstraint::AnyWrapped => "any wrapped data".into(),
            TypeConstraint::OneOf(constraints) => {
                let descs: Vec<String> = constraints.iter().map(|c| c.description()).collect();
                format!("one of [{}]", descs.join(", "))
            }
            TypeConstraint::AllOf(constraints) => {
                let descs: Vec<String> = constraints.iter().map(|c| c.description()).collect();
                format!("all of [{}]", descs.join(", "))
            }
            TypeConstraint::Custom { description, .. } => (*description).into(),
            TypeConstraint::Any => "any type".into(),
        }
    }
}

impl fmt::Debug for TypeConstraint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "TypeConstraint({})", self.description())
    }
}

impl fmt::Display for TypeConstraint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.description())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_any_unsigned_matches() {
        let constraint = TypeConstraint::AnyUnsigned;
        assert!(constraint.matches(&VsfType::u3(42)));
        assert!(constraint.matches(&VsfType::u4(1000)));
        assert!(constraint.matches(&VsfType::u5(100000)));
        assert!(constraint.matches(&VsfType::u6(1000000000)));
        assert!(!constraint.matches(&VsfType::i3(-42)));
        assert!(!constraint.matches(&VsfType::f5(3.14)));
    }

    #[test]
    fn test_eagle_time_matches() {
        let constraint = TypeConstraint::AnyEagleTime;
        assert!(constraint.matches(&VsfType::e(EtType::f6(123.456))));
        assert!(constraint.matches(&VsfType::e(EtType::f5(123.456))));
        assert!(constraint.matches(&VsfType::e(EtType::u(123456))));
        assert!(constraint.matches(&VsfType::e(EtType::i(123456))));
        assert!(!constraint.matches(&VsfType::f6(123.456)));
    }

    #[test]
    fn test_eagle_time_with_float_backing() {
        let constraint = TypeConstraint::EagleTime(Box::new(TypeConstraint::AnyFloat));
        assert!(constraint.matches(&VsfType::e(EtType::f6(123.456))));
        assert!(constraint.matches(&VsfType::e(EtType::f5(123.456))));
        assert!(!constraint.matches(&VsfType::e(EtType::u(123456))));
        assert!(!constraint.matches(&VsfType::e(EtType::i(123456))));
    }

    #[test]
    fn test_hash_types() {
        assert!(TypeConstraint::Blake3Provenance.matches(&VsfType::hp(vec![0u8; 32])));
        assert!(TypeConstraint::Blake3Rolling.matches(&VsfType::hb(vec![0u8; 32])));
        assert!(TypeConstraint::AnyHash.matches(&VsfType::hp(vec![0u8; 32])));
        assert!(TypeConstraint::AnyHash.matches(&VsfType::hb(vec![0u8; 32])));
    }

    #[test]
    fn test_key_types() {
        assert!(TypeConstraint::Ed25519Key.matches(&VsfType::ke(vec![0u8; 32])));
        assert!(TypeConstraint::X25519Key.matches(&VsfType::kx(vec![0u8; 32])));
        assert!(TypeConstraint::AnyKey.matches(&VsfType::ke(vec![0u8; 32])));
        assert!(TypeConstraint::AnyKey.matches(&VsfType::kx(vec![0u8; 32])));
    }

    #[test]
    fn test_one_of_constraint() {
        let constraint =
            TypeConstraint::OneOf(vec![TypeConstraint::AnyUnsigned, TypeConstraint::AnyString]);

        assert!(constraint.matches(&VsfType::u5(42)));
        assert!(constraint.matches(&VsfType::x("hello".to_string())));
        assert!(!constraint.matches(&VsfType::f6(3.14)));
    }

    #[test]
    fn test_validation_error() {
        let constraint = TypeConstraint::AnyUnsigned;
        let result = constraint.validate(&VsfType::f6(3.14));

        assert!(result.is_err());
        if let Err(ValidationError::TypeConstraintViolation { constraint: c, got }) = result {
            assert_eq!(c, "any unsigned integer");
            assert_eq!(got, "f6");
        } else {
            panic!("Expected TypeConstraintViolation error");
        }
    }
}

/// Helper function to get human-readable type name for VsfType
/// Used in error messages and debugging
pub fn vsf_type_name(vsf: &VsfType) -> String {
    // Use Debug formatting and extract variant name
    let debug_str = format!("{:?}", vsf);
    // Extract variant name (everything before '(' or end of string)
    debug_str
        .split('(')
        .next()
        .unwrap_or(&debug_str)
        .to_string()
}