datacraft-core 0.1.2

Headless domain model, command bus and application state for DataCraft
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
//! Logical Access field kinds (step 3.1, item H10; ARCHITECTURE §7).
//!
//! `FieldKind` is the logical counterpart of the physical
//! [`StorageDataType`](../../datacraft_storage/enum.StorageDataType.html)
//! in storage: physical concerns live below the engine boundary, Access
//! semantics ("Short Text", "AutoNumber") live here. The Jet/ACE backends
//! map their physical types onto this model (dependency direction
//! `storage → core`).

use serde::{Deserialize, Serialize};

use super::error::SchemaError;
use super::value::FieldValue;

/// Smallest/largest OLE Automation day Access accepts (years 100–9999).
const OLE_DATE_MIN: f64 = -657_434.0;
/// Exclusive upper bound: `2958466.0` would be 10000-01-01.
const OLE_DATE_MAX_EXCLUSIVE: f64 = 2_958_466.0;

impl NumericKind {
    /// Value family name used in kind-mismatch diagnostics.
    fn family_name(self) -> &'static str {
        match self {
            Self::Byte | Self::Integer | Self::LongInteger => "integer",
            Self::Single => "single",
            Self::Double => "double",
            Self::Decimal { .. } => "decimal",
        }
    }

    /// Human-readable kind name used in range diagnostics.
    fn display_name(self) -> &'static str {
        match self {
            Self::Byte => "byte",
            Self::Integer => "integer",
            Self::LongInteger => "long integer",
            Self::Single => "single",
            Self::Double => "double",
            Self::Decimal { .. } => "decimal",
        }
    }

    /// Validates a domain value against this numeric precision family.
    pub fn validate_value(self, value: &FieldValue) -> Result<(), SchemaError> {
        let mismatch = || SchemaError::KindMismatch {
            expected: self.family_name(),
            found: value.family(),
        };
        match self {
            Self::Byte | Self::Integer | Self::LongInteger => match value {
                FieldValue::Integer(i) => {
                    let (min, max) = self.integer_range().expect("integral kinds have a range");
                    if *i >= min && *i <= max {
                        Ok(())
                    } else {
                        Err(SchemaError::ValueOutOfRange {
                            detail: format!(
                                "{} integers hold {min}..={max}, found {i}",
                                self.display_name()
                            ),
                        })
                    }
                }
                _ => Err(mismatch()),
            },
            Self::Single | Self::Double => match value {
                FieldValue::Float(f) if f.is_finite() => Ok(()),
                FieldValue::Float(_) => Err(SchemaError::ValueOutOfRange {
                    detail: "floating-point values must be finite".to_owned(),
                }),
                _ => Err(mismatch()),
            },
            Self::Decimal { .. } => match value {
                FieldValue::Decimal(_) => Ok(()),
                _ => Err(mismatch()),
            },
        }
    }
}

/// Precision family of a `Number` field, following the Access UI.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NumericKind {
    /// 1-byte unsigned integer (Access "Byte").
    Byte,
    /// 2-byte signed integer (Access "Integer").
    Integer,
    /// 4-byte signed integer (Access "Long Integer").
    LongInteger,
    /// IEEE 754 single precision (Access "Single").
    Single,
    /// IEEE 754 double precision (Access "Double").
    Double,
    /// Fixed-point decimal with declared precision and scale.
    Decimal {
        /// Total number of significant digits (1–28).
        precision: u8,
        /// Digits after the decimal point (0–precision).
        scale: u8,
    },
}

impl NumericKind {
    /// Inclusive integer range for the integral kinds; `None` otherwise.
    pub fn integer_range(self) -> Option<(i64, i64)> {
        match self {
            Self::Byte => Some((0, 255)),
            Self::Integer => Some((i64::from(i16::MIN), i64::from(i16::MAX))),
            Self::LongInteger => Some((i64::from(i32::MIN), i64::from(i32::MAX))),
            Self::Single | Self::Double | Self::Decimal { .. } => None,
        }
    }

    /// Validates the declared precision/scale of the decimal kind.
    pub fn validate(self) -> Result<(), SchemaError> {
        if let Self::Decimal { precision, scale } = self {
            if !(1..=28).contains(&precision) {
                return Err(SchemaError::InvalidKind {
                    detail: format!("decimal precision must be 1..=28, found {precision}"),
                });
            }
            if scale > precision {
                return Err(SchemaError::InvalidKind {
                    detail: format!(
                        "decimal scale must not exceed precision (precision {precision}, scale {scale})"
                    ),
                });
            }
        }
        Ok(())
    }
}

/// AutoNumber flavor (Access "Field Size" of an AutoNumber field).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AutoKind {
    /// Incrementing 4-byte integer.
    LongInteger,
    /// 16-byte replication identifier (GUID).
    ReplicationId,
}

/// Binary field flavor.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BinaryKind {
    /// OLE object (embedded package or linked data).
    OleObject,
    /// ACE attachment (multi-value complex column).
    Attachment,
}

/// Scalar family of an ACE multi-value column (step 16.1, M4).
///
/// Multi-value columns store an ordered list of scalars in a flat backing
/// table; this is the element kind the values validate against.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MultiValueKind {
    /// Text elements with an Access character budget.
    Text {
        /// Maximum number of characters (Access "Field Size").
        max_len: u16,
    },
    /// Numeric elements of the given precision family.
    Number(NumericKind),
    /// Date/time elements in OLE Automation form.
    DateTime,
    /// Currency elements as scaled integers (factor 10⁻⁴).
    Currency,
    /// Yes/No elements.
    Boolean {
        /// Whether the UI offers a third (Null) display state.
        tri_state: bool,
    },
    /// 16-byte replication identifiers.
    Guid,
}

/// Logical Access type of a field (ARCHITECTURE §7 mapping table).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", content = "data", rename_all = "snake_case")]
pub enum FieldKind {
    /// Short text with a maximum length of 1–255 characters.
    Text {
        /// Maximum number of characters (Access "Field Size").
        max_len: u16,
    },
    /// Long text stored on memo pages.
    Memo,
    /// Numeric field of the given precision family.
    Number(NumericKind),
    /// Date/time in OLE Automation form.
    DateTime,
    /// Currency as a scaled 64-bit integer (factor 10⁻⁴).
    Currency,
    /// AutoNumber — values are engine-assigned, never user-provided.
    AutoNumber(AutoKind),
    /// Yes/No. `tri_state` only governs UI display (Null display);
    /// actual nullability comes from `FieldProperties::required`.
    Boolean {
        /// Whether the UI offers a third (Null) display state.
        tri_state: bool,
    },
    /// Binary data (OLE object or attachment).
    Binary(BinaryKind),
    /// ACE multi-value column over a scalar element kind (step 16.1, M4).
    MultiValue(MultiValueKind),
    /// ACE complex column whose backing records have no multi-value or
    /// attachment shape (e.g. version history).
    Complex,
    /// 16-byte replication identifier (plain GUID field; AutoNumbers with
    /// GUIDs are `AutoNumber(ReplicationId)` instead).
    Guid,
}

impl FieldKind {
    /// Value family name used in kind-mismatch diagnostics.
    pub fn family(&self) -> &'static str {
        match self {
            Self::Text { .. } => "short text",
            Self::Memo => "memo",
            Self::Number(NumericKind::Byte | NumericKind::Integer | NumericKind::LongInteger) => {
                "integer"
            }
            Self::Number(NumericKind::Single) => "single",
            Self::Number(NumericKind::Double) => "double",
            Self::Number(NumericKind::Decimal { .. }) => "decimal",
            Self::DateTime => "date/time",
            Self::Currency => "currency",
            Self::AutoNumber(_) => "autonumber",
            Self::Boolean { .. } => "boolean",
            Self::Binary(_) => "binary",
            Self::MultiValue(_) => "multi-value",
            Self::Complex => "complex",
            Self::Guid => "guid",
        }
    }

    /// Validates the kind's own invariants (text length bounds, decimal
    /// precision/scale).
    pub fn validate(&self) -> Result<(), SchemaError> {
        match self {
            Self::Text { max_len } => {
                if !(1..=255).contains(max_len) {
                    return Err(SchemaError::InvalidKind {
                        detail: format!("text max_len must be 1..=255, found {max_len}"),
                    });
                }
                Ok(())
            }
            Self::Number(kind) => kind.validate(),
            Self::MultiValue(MultiValueKind::Text { max_len }) => {
                if !(1..=255).contains(max_len) {
                    return Err(SchemaError::InvalidKind {
                        detail: format!(
                            "multi-value text max_len must be 1..=255, found {max_len}"
                        ),
                    });
                }
                Ok(())
            }
            Self::MultiValue(MultiValueKind::Number(kind)) => kind.validate(),
            _ => Ok(()),
        }
    }

    /// Validates a domain value against this kind. `Null` passes here —
    /// required-ness is a property concern checked on
    /// [`FieldDef::validate_value`](super::def::FieldDef::validate_value).
    pub fn validate_value(&self, value: &FieldValue) -> Result<(), SchemaError> {
        if matches!(value, FieldValue::Null) {
            return Ok(());
        }
        let mismatch = || SchemaError::KindMismatch {
            expected: self.family(),
            found: value.family(),
        };
        match self {
            Self::Text { max_len } => match value {
                FieldValue::Text(s) => {
                    let found = s.chars().count();
                    if found <= usize::from(*max_len) {
                        Ok(())
                    } else {
                        Err(SchemaError::ValueTooLong {
                            max_len: usize::from(*max_len),
                            found,
                        })
                    }
                }
                _ => Err(mismatch()),
            },
            Self::Memo => match value {
                FieldValue::Text(_) => Ok(()),
                _ => Err(mismatch()),
            },
            Self::Number(kind) => kind.validate_value(value),
            Self::DateTime => match value {
                FieldValue::DateTime(days) => {
                    if !days.is_finite() {
                        Err(SchemaError::ValueOutOfRange {
                            detail: "date/time values must be finite".to_owned(),
                        })
                    } else if !(OLE_DATE_MIN..OLE_DATE_MAX_EXCLUSIVE).contains(days) {
                        Err(SchemaError::ValueOutOfRange {
                            detail: format!(
                                "OLE date {days} is outside the Access-supported range \
                                 [{OLE_DATE_MIN}, {OLE_DATE_MAX_EXCLUSIVE})"
                            ),
                        })
                    } else {
                        Ok(())
                    }
                }
                _ => Err(mismatch()),
            },
            Self::Currency => match value {
                FieldValue::Currency(_) => Ok(()),
                _ => Err(mismatch()),
            },
            Self::AutoNumber(_) => Err(SchemaError::EngineAssigned { field: None }),
            Self::Boolean { .. } => match value {
                FieldValue::Boolean(_) => Ok(()),
                _ => Err(mismatch()),
            },
            Self::Binary(BinaryKind::OleObject) => match value {
                FieldValue::Binary(_) => Ok(()),
                _ => Err(mismatch()),
            },
            Self::Binary(BinaryKind::Attachment) => match value {
                FieldValue::Complex(_) => Ok(()),
                _ => Err(mismatch()),
            },
            Self::MultiValue(kind) => match value {
                FieldValue::Complex(records) => {
                    let element = kind.element_kind();
                    for record in records {
                        let Some(scalar) = record.scalar() else {
                            return Err(SchemaError::ValueOutOfRange {
                                detail: "multi-value record carries no scalar element".to_owned(),
                            });
                        };
                        element.validate_value(scalar)?;
                    }
                    Ok(())
                }
                _ => Err(mismatch()),
            },
            Self::Complex => match value {
                FieldValue::Complex(_) => Ok(()),
                _ => Err(mismatch()),
            },
            Self::Guid => match value {
                FieldValue::Guid(_) => Ok(()),
                _ => Err(mismatch()),
            },
        }
    }
}

impl MultiValueKind {
    /// The scalar field kind of one element (validation and criteria
    /// coercion share it).
    pub fn element_kind(self) -> FieldKind {
        match self {
            Self::Text { max_len } => FieldKind::Text { max_len },
            Self::Number(numeric) => FieldKind::Number(numeric),
            Self::DateTime => FieldKind::DateTime,
            Self::Currency => FieldKind::Currency,
            Self::Boolean { tri_state } => FieldKind::Boolean { tri_state },
            Self::Guid => FieldKind::Guid,
        }
    }
}

#[cfg(test)]
#[path = "field_tests.rs"]
mod tests;

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

    /// Every numeric flavor renders its family name; the integral flavors
    /// carry ranges while the floating/decimal ones do not.
    #[test]
    fn numeric_flavors_render_and_distinguish_ranges() {
        for (kind, name) in [
            (NumericKind::Byte, "byte"),
            (NumericKind::Integer, "integer"),
            (NumericKind::LongInteger, "long integer"),
            (NumericKind::Single, "single"),
            (NumericKind::Double, "double"),
            (
                NumericKind::Decimal {
                    precision: 10,
                    scale: 2,
                },
                "decimal",
            ),
        ] {
            assert!(!kind.family_name().is_empty(), "{name}");
        }
        assert!(NumericKind::Byte.integer_range().is_some());
        assert!(NumericKind::Integer.integer_range().is_some());
        assert!(NumericKind::LongInteger.integer_range().is_some());
        assert!(NumericKind::Single.integer_range().is_none());
        assert!(NumericKind::Double.integer_range().is_none());
        assert!(
            NumericKind::Decimal {
                precision: 10,
                scale: 2
            }
            .integer_range()
            .is_none()
        );
    }

    /// Kinds without a dedicated display arm fall back to their family name;
    /// AutoNumber and Complex have explicit names, and a wrong value type is
    /// a precise mismatch.
    #[test]
    fn display_fallbacks_and_kind_mismatches_are_precise() {
        assert_eq!(
            FieldKind::AutoNumber(AutoKind::LongInteger).family(),
            "autonumber"
        );
        assert_eq!(FieldKind::Complex.family(), "complex");
        let error = FieldKind::Memo
            .validate_value(&FieldValue::Integer(1))
            .expect_err("wrong value type");
        assert!(error.to_string().contains("memo") || error.to_string().contains("integer"));
    }

    /// Range diagnostics name the violated kind, and complex fields
    /// accept nothing but complex values.
    #[test]
    fn range_diagnostics_name_the_kind_and_complex_refuses_scalars() {
        let error = FieldKind::Number(NumericKind::LongInteger)
            .validate_value(&FieldValue::Integer(1 << 40))
            .expect_err("out of range");
        assert!(error.to_string().contains("long integer"), "{error}");

        let error = FieldKind::Number(NumericKind::Byte)
            .validate_value(&FieldValue::Integer(300))
            .expect_err("out of range");
        assert!(error.to_string().contains("byte"), "{error}");

        let error = FieldKind::Complex
            .validate_value(&FieldValue::Integer(1))
            .expect_err("scalars do not satisfy complex fields");
        assert!(error.to_string().contains("complex"), "{error}");
    }
}