vouched-core 0.1.0

Core error types for vouched.
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
use core::{error::Error as StdError, fmt};

#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "alloc")]
use core::ops::Deref;

#[cfg(feature = "valuable")]
use alloc::string::ToString;
#[cfg(feature = "valuable")]
use valuable::{Fields, NamedField, NamedValues, StructDef, Structable, Valuable, Value, Visit};

/// Error returned when a string newtype is shorter than its `len` lower bound.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TooShortError {
    min: usize,
    actual: usize,
}

impl TooShortError {
    /// Creates a too-short error.
    pub const fn new(min: usize, actual: usize) -> Self {
        Self { min, actual }
    }

    /// Returns the minimum accepted length, measured as untrimmed Unicode scalar values.
    pub const fn min(&self) -> usize {
        self.min
    }

    /// Returns the actual length, measured as untrimmed Unicode scalar values.
    pub const fn actual(&self) -> usize {
        self.actual
    }
}

impl fmt::Display for TooShortError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "is too short (min {} chars, got {})",
            self.min, self.actual
        )
    }
}

impl StdError for TooShortError {}

/// Error returned when a string newtype is longer than its `len` upper bound.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TooLongError {
    max: usize,
    actual: usize,
}

impl TooLongError {
    /// Creates a too-long error.
    pub const fn new(max: usize, actual: usize) -> Self {
        Self { max, actual }
    }

    /// Returns the maximum accepted length, measured as untrimmed Unicode scalar values.
    pub const fn max(&self) -> usize {
        self.max
    }

    /// Returns the actual length, measured as untrimmed Unicode scalar values.
    pub const fn actual(&self) -> usize {
        self.actual
    }
}

impl fmt::Display for TooLongError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "is too long (max {} chars, got {})",
            self.max, self.actual
        )
    }
}

impl StdError for TooLongError {}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum NumericValueRepr {
    Signed(i128),
    Unsigned(u128),
}

/// Lossless numeric value captured by generated range and cast errors.
///
/// The representation is private so future integer-like values can be added
/// without exposing the enum shape. Use the `as_*` methods to recover a
/// primitive integer only when the conversion is lossless.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct NumericValue(NumericValueRepr);

impl NumericValue {
    /// Captures a signed integer value.
    pub const fn from_i128(value: i128) -> Self {
        Self(NumericValueRepr::Signed(value))
    }

    /// Captures an unsigned integer value.
    pub const fn from_u128(value: u128) -> Self {
        Self(NumericValueRepr::Unsigned(value))
    }

    /// Returns the value as `i64` when it fits exactly.
    pub fn as_i64(self) -> Option<i64> {
        match self.0 {
            NumericValueRepr::Signed(value) => i64::try_from(value).ok(),
            NumericValueRepr::Unsigned(value) => i64::try_from(value).ok(),
        }
    }

    /// Returns the value as `u64` when it fits exactly.
    pub fn as_u64(self) -> Option<u64> {
        match self.0 {
            NumericValueRepr::Signed(value) => u64::try_from(value).ok(),
            NumericValueRepr::Unsigned(value) => u64::try_from(value).ok(),
        }
    }

    /// Returns the value as `i128` when it fits exactly.
    pub fn as_i128(self) -> Option<i128> {
        match self.0 {
            NumericValueRepr::Signed(value) => Some(value),
            NumericValueRepr::Unsigned(value) => i128::try_from(value).ok(),
        }
    }

    /// Returns the value as `u128` when it fits exactly.
    pub fn as_u128(self) -> Option<u128> {
        match self.0 {
            NumericValueRepr::Signed(value) => u128::try_from(value).ok(),
            NumericValueRepr::Unsigned(value) => Some(value),
        }
    }
}

impl fmt::Display for NumericValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.0 {
            NumericValueRepr::Signed(value) => write!(f, "{value}"),
            NumericValueRepr::Unsigned(value) => write!(f, "{value}"),
        }
    }
}

macro_rules! impl_signed_numeric_value_from {
    ($($ty:ty),* $(,)?) => {
        $(
            impl From<$ty> for NumericValue {
                fn from(value: $ty) -> Self {
                    Self::from_i128(i128::from(value))
                }
            }
        )*
    };
}

macro_rules! impl_unsigned_numeric_value_from {
    ($($ty:ty),* $(,)?) => {
        $(
            impl From<$ty> for NumericValue {
                fn from(value: $ty) -> Self {
                    Self::from_u128(u128::from(value))
                }
            }
        )*
    };
}

impl_signed_numeric_value_from!(i8, i16, i32, i64, i128);
impl_unsigned_numeric_value_from!(u8, u16, u32, u64, u128);

#[cfg(feature = "valuable")]
impl Valuable for NumericValue {
    fn as_value(&self) -> Value<'_> {
        match self.0 {
            NumericValueRepr::Signed(value) => Value::I128(value),
            NumericValueRepr::Unsigned(value) => Value::U128(value),
        }
    }

    fn visit(&self, visit: &mut dyn Visit) {
        visit.visit_value(self.as_value());
    }
}

/// Error returned when a numeric newtype is outside its `range` bounds.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OutOfRangeNumericError {
    actual: NumericValue,
    lower_bound: Option<NumericValue>,
    upper_bound: Option<NumericValue>,
}

impl OutOfRangeNumericError {
    /// Creates an out-of-range error with the value that failed validation.
    pub const fn new(actual: NumericValue) -> Self {
        Self {
            actual,
            lower_bound: None,
            upper_bound: None,
        }
    }

    /// Returns an out-of-range error with the lower bound that failed.
    #[must_use]
    pub const fn with_lower_bound(self, lower_bound: NumericValue) -> Self {
        Self {
            actual: self.actual,
            lower_bound: Some(lower_bound),
            upper_bound: self.upper_bound,
        }
    }

    /// Returns an out-of-range error with the upper bound that failed.
    #[must_use]
    pub const fn with_upper_bound(self, upper_bound: NumericValue) -> Self {
        Self {
            actual: self.actual,
            lower_bound: self.lower_bound,
            upper_bound: Some(upper_bound),
        }
    }

    /// Returns the actual value that failed validation.
    pub const fn actual(&self) -> NumericValue {
        self.actual
    }

    /// Returns the lower bound that failed when it could be captured losslessly.
    pub const fn lower_bound(&self) -> Option<NumericValue> {
        self.lower_bound
    }

    /// Returns the upper bound that failed when it could be captured losslessly.
    pub const fn upper_bound(&self) -> Option<NumericValue> {
        self.upper_bound
    }
}

impl fmt::Display for OutOfRangeNumericError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let actual = self.actual;
        match (self.lower_bound, self.upper_bound) {
            (Some(lower_bound), Some(upper_bound)) => {
                write!(
                    f,
                    "out of range (value {actual}, lower bound {lower_bound}, upper bound {upper_bound})"
                )
            }
            (Some(lower_bound), None) => {
                write!(
                    f,
                    "out of range (value {actual}, lower bound {lower_bound})"
                )
            }
            (None, Some(upper_bound)) => {
                write!(
                    f,
                    "out of range (value {actual}, upper bound {upper_bound})"
                )
            }
            (None, None) => write!(f, "out of range (value {actual})"),
        }
    }
}

impl StdError for OutOfRangeNumericError {}

#[cfg(feature = "valuable")]
static OUT_OF_RANGE_NUMERIC_ERROR_FIELDS: &[NamedField<'static>] = &[
    NamedField::new("actual"),
    NamedField::new("lower_bound"),
    NamedField::new("upper_bound"),
];

#[cfg(feature = "valuable")]
impl Valuable for OutOfRangeNumericError {
    fn as_value(&self) -> Value<'_> {
        Value::Structable(self)
    }

    fn visit(&self, visit: &mut dyn Visit) {
        let values = [
            self.actual.as_value(),
            self.lower_bound.as_value(),
            self.upper_bound.as_value(),
        ];
        visit.visit_named_fields(&NamedValues::new(
            OUT_OF_RANGE_NUMERIC_ERROR_FIELDS,
            &values,
        ));
    }
}

#[cfg(feature = "valuable")]
impl Structable for OutOfRangeNumericError {
    fn definition(&self) -> StructDef<'_> {
        StructDef::new_static(
            "OutOfRangeNumericError",
            Fields::Named(OUT_OF_RANGE_NUMERIC_ERROR_FIELDS),
        )
    }
}

/// Error returned when a string newtype contains a character rejected by `chars`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidCharError {
    index: usize,
    ch: char,
}

impl InvalidCharError {
    /// Creates an invalid-character error.
    pub const fn new(index: usize, ch: char) -> Self {
        Self { index, ch }
    }

    /// Returns the zero-based character index, not a byte offset.
    pub const fn index(&self) -> usize {
        self.index
    }

    /// Returns the rejected character.
    pub const fn ch(&self) -> char {
        self.ch
    }
}

impl fmt::Display for InvalidCharError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "contains invalid character '{}' at index {}",
            self.ch, self.index
        )
    }
}

impl StdError for InvalidCharError {}

/// Common interface for generated Vouched errors.
///
/// Error types generated by the Vouched derive macro implement this trait.
/// The `as_*` methods provide type-safe access to specific validation errors.
pub trait VouchedError: StdError + Send + Sync + 'static {
    /// Returns the underlying too-short error when this is that variant.
    fn as_too_short(&self) -> Option<&TooShortError> {
        None
    }
    /// Returns the underlying too-long error when this is that variant.
    fn as_too_long(&self) -> Option<&TooLongError> {
        None
    }
    /// Returns the underlying numeric out-of-range error when this is that variant.
    fn as_out_of_range_numeric(&self) -> Option<&OutOfRangeNumericError> {
        None
    }
    /// Returns the underlying invalid-character error when this is that variant.
    fn as_invalid_char(&self) -> Option<&InvalidCharError> {
        None
    }
}

/// Wrapper type for handling different Vouched error types uniformly.
///
/// This allows code that works with multiple Vouched wrappers to handle their
/// validation errors through a single type. It supports automatic conversion
/// through the `?` operator.
///
/// Available with the `alloc` feature, which is enabled by the default `std`
/// feature.
///
/// # Examples
///
/// ```
/// # use vouched_core::*;
/// #
/// # #[derive(Debug)]
/// # enum UserIdError { TooShort(TooShortError), TooLong(TooLongError) }
/// # impl core::fmt::Display for UserIdError {
/// #     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
/// #         match self { UserIdError::TooShort(e) => e.fmt(f), UserIdError::TooLong(e) => e.fmt(f) }
/// #     }
/// # }
/// # impl core::error::Error for UserIdError {}
/// # impl VouchedError for UserIdError {
/// #     fn as_too_short(&self) -> Option<&TooShortError> {
/// #         match self { UserIdError::TooShort(e) => Some(e), _ => None }
/// #     }
/// #     fn as_too_long(&self) -> Option<&TooLongError> {
/// #         match self { UserIdError::TooLong(e) => Some(e), _ => None }
/// #     }
/// # }
/// #
/// // Function that handles different Vouched error types uniformly.
/// fn process_errors() -> Result<(), Error> {
///     // Any error implementing VouchedError can be converted into Error.
///     let err = UserIdError::TooShort(TooShortError::new(1, 0));
///     Err(Error::from(err))
/// }
///
/// // Inspect the concrete validation error kind.
/// let result = process_errors();
/// assert!(result.is_err());
/// let err = result.unwrap_err();
/// assert!(err.as_too_short().is_some());
/// ```
#[cfg(feature = "alloc")]
#[derive(Debug)]
pub struct Error(Box<dyn VouchedError>);

#[cfg(feature = "alloc")]
impl Error {
    /// Wraps a boxed generated Vouched error.
    pub fn new(inner: Box<dyn VouchedError>) -> Self {
        Self(inner)
    }

    /// Returns the wrapped generated Vouched error.
    pub fn into_inner(self) -> Box<dyn VouchedError> {
        self.0
    }
}

#[cfg(feature = "alloc")]
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

#[cfg(feature = "alloc")]
impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        Some(&*self.0)
    }
}

#[cfg(feature = "alloc")]
impl<E> From<E> for Error
where
    E: VouchedError,
{
    fn from(e: E) -> Self {
        Self(Box::new(e))
    }
}

#[cfg(feature = "alloc")]
impl Deref for Error {
    type Target = dyn VouchedError;

    fn deref(&self) -> &Self::Target {
        &*self.0
    }
}

#[cfg(feature = "alloc")]
impl AsRef<dyn VouchedError> for Error {
    fn as_ref(&self) -> &(dyn VouchedError + 'static) {
        &*self.0
    }
}

#[cfg(feature = "valuable")]
static ERROR_FIELDS: &[NamedField<'static>] = &[NamedField::new("message")];

#[cfg(feature = "valuable")]
impl Valuable for Error {
    fn as_value(&self) -> Value<'_> {
        Value::Structable(self)
    }

    fn visit(&self, visit: &mut dyn Visit) {
        let message = self.to_string();
        let values = [message.as_value()];
        visit.visit_named_fields(&NamedValues::new(ERROR_FIELDS, &values));
    }
}

#[cfg(feature = "valuable")]
impl Structable for Error {
    fn definition(&self) -> StructDef<'_> {
        StructDef::new_static("Error", Fields::Named(ERROR_FIELDS))
    }
}