rustyml 0.17.0

Machine learning models, neural networks, metrics, and data utilities in Rust
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
//! Error types for RustyML
//!
//! [`Error`] holds shared validation, model state, computation, and I/O failures.
//! [`RustymlResult<T>`] aliases `std::result::Result<T, Error>` for APIs that use this error type.
//!
//! `Error` aggregates 2 domain-specific error enums: `NnError` (from `neural_network`) and
//! `TreeError` (from `machine_learning`). It also wraps the shared
//! [`IoError`]. Callers can `match` precisely, since a shared variant never
//! carries a concern specific to 1 part of the crate
//!
//! # Categories
//!
//! - **Input validation**: [`Error::EmptyInput`],
//!   [`Error::DimensionMismatch`],
//!   [`Error::ShapeMismatch`],
//!   [`Error::NonFinite`],
//!   [`Error::InvalidParameter`],
//!   [`Error::InvalidInput`]
//! - **Model state**: [`Error::NotFitted`], and the neural-network
//!   states in `NnError`
//! - **Numerics / computation**: [`Error::NotConverged`],
//!   [`Error::Computation`]
//! - **Domain-specific**: `Error::NeuralNetwork` (wrapping `neural_network`'s `NnError`) and
//!   `Error::Tree` (wrapping `machine_learning`'s `TreeError`)
//! - **I/O and serialization**: [`Error::Io`]
//!
//! # Conventions
//!
//! - A non-finite **hyperparameter** from the user is an
//!   [`Error::InvalidParameter`] (the reason names
//!   finiteness). A non-finite value from the **data or a computation** is an
//!   [`Error::NonFinite`]
//! - [`Error::DimensionMismatch`] compares scalar counts
//!   (for example number of features). Use [`Error::ShapeMismatch`]
//!   when whole tensor shapes differ
//!
//! # Constructing errors
//!
//! Prefer the smart constructors
//! ([`Error::dimension_mismatch`],
//! [`Error::invalid_parameter`], and so on) over building
//! variants by hand. They accept flexible argument types and keep the wording consistent across the
//! crate. To attach context to a foreign error while preserving its source chain, use
//! [`Context::context`] /
//! [`Context::with_context`]
//!
//! [`Error`] is `#[non_exhaustive]`. Match with a trailing `_` arm to stay
//! forward-compatible
//!
//! [`RustymlResult<T>`]: crate::error::RustymlResult
//! [`Error`]: crate::error::Error
//! [`IoError`]: crate::error::IoError
//! [`Error::EmptyInput`]: crate::error::Error::EmptyInput
//! [`Error::DimensionMismatch`]: crate::error::Error::DimensionMismatch
//! [`Error::ShapeMismatch`]: crate::error::Error::ShapeMismatch
//! [`Error::NonFinite`]: crate::error::Error::NonFinite
//! [`Error::InvalidParameter`]: crate::error::Error::InvalidParameter
//! [`Error::InvalidInput`]: crate::error::Error::InvalidInput
//! [`Error::NotFitted`]: crate::error::Error::NotFitted
//! [`Error::NotConverged`]: crate::error::Error::NotConverged
//! [`Error::Computation`]: crate::error::Error::Computation
//! [`Error::Io`]: crate::error::Error::Io
//! [`Error::dimension_mismatch`]: crate::error::Error::dimension_mismatch
//! [`Error::invalid_parameter`]: crate::error::Error::invalid_parameter
//! [`Context::context`]: crate::error::Context::context
//! [`Context::with_context`]: crate::error::Context::with_context

/// The unified error type for all fallible RustyML operations
///
/// See the [module documentation](crate::error) for the category breakdown and conventions
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// An input array, vector, or dataset was empty where data was required
    ///
    /// The payload describes what was empty (for example `"input data"`, `"target vector"`)
    #[error("input is empty: {0}")]
    EmptyInput(
        /// Name of the empty input.
        String,
    ),

    /// 2 scalar counts that had to agree did not. Examples: number of features at predict
    /// time versus at fit time, or the length of `x` versus `y`
    #[error("dimension mismatch: expected {expected}, found {found}")]
    DimensionMismatch {
        /// The expected count
        expected: usize,
        /// The count that was actually provided
        found: usize,
    },

    /// 2 whole tensor shapes that had to agree did not (for example a gradient's shape versus the
    /// activation it flows into)
    #[error("shape mismatch: expected {expected:?}, found {found:?}")]
    ShapeMismatch {
        /// The expected shape
        expected: Vec<usize>,
        /// The shape that was actually provided
        found: Vec<usize>,
    },

    /// A value in the data, or produced during a computation, was `NaN` or infinite
    ///
    /// The payload names where it occurred. For an invalid non-finite *hyperparameter*, use
    /// [`Error::InvalidParameter`] instead
    #[error("non-finite value (NaN or infinity) encountered in {0}")]
    NonFinite(
        /// Location of the non-finite value.
        String,
    ),

    /// A user-supplied hyperparameter was out of its valid range
    #[error("invalid parameter `{name}`: {reason}")]
    InvalidParameter {
        /// Name of the offending parameter
        name: String,
        /// Why it is invalid (range, sign, finiteness, and so on)
        reason: String,
    },

    /// Input that failed validation in a way not captured by a more specific variant. Examples:
    /// an unexpected tensor rank, malformed labels, or a relational constraint between the data
    /// and the configuration
    #[error("invalid input: {0}")]
    InvalidInput(
        /// Input validation message.
        String,
    ),

    /// A method that requires a trained model was called before the model was fitted
    ///
    /// The payload is the model's name (for example `"KMeans"`)
    #[error("model `{0}` has not been fitted; call `fit` before this operation")]
    NotFitted(
        /// Name of the unfitted model.
        &'static str,
    ),

    /// An iterative algorithm failed to reach its convergence criterion
    #[error("failed to converge: {0}")]
    NotConverged(
        /// Convergence failure message.
        String,
    ),

    /// A computation failed for a reason that is not a validation problem (numerical breakdown,
    /// a violated internal invariant, or a wrapped lower-level error)
    ///
    /// When wrapping a foreign error, prefer [`Context::context`], which preserves the original
    /// error as the [`source`](std::error::Error::source)
    #[error("computation failed: {context}")]
    Computation {
        /// Human-readable description of what failed
        context: String,
        /// The underlying error, if this wraps 1
        #[source]
        source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
    },

    /// A neural-network-specific error. See [`NnError`](crate::neural_network::NnError)
    #[cfg(feature = "neural_network")]
    #[error(transparent)]
    NeuralNetwork(
        /// Neural network error.
        #[from]
        crate::neural_network::NnError,
    ),

    /// A decision-tree-specific error. See [`TreeError`](crate::machine_learning::TreeError)
    #[cfg(feature = "machine_learning")]
    #[error(transparent)]
    Tree(
        /// Decision tree error.
        #[from]
        crate::machine_learning::TreeError,
    ),

    /// An I/O or serialization error. See [`IoError`]
    #[error(transparent)]
    Io(
        /// I/O or serialization error.
        #[from]
        IoError,
    ),
}

impl Error {
    /// Builds an [`Error::EmptyInput`] describing what was empty
    ///
    /// # Parameters
    ///
    /// - `what` - Name of the empty input
    ///
    /// # Returns
    ///
    /// - `Self` - Error with the supplied details
    #[cold]
    pub fn empty_input(what: impl Into<String>) -> Self {
        Self::EmptyInput(what.into())
    }

    /// Builds an [`Error::DimensionMismatch`] from the expected and found counts
    ///
    /// # Parameters
    ///
    /// - `expected` - Required count
    /// - `found` - Provided count
    ///
    /// # Returns
    ///
    /// - `Self` - Error with the supplied details
    #[cold]
    pub fn dimension_mismatch(expected: usize, found: usize) -> Self {
        Self::DimensionMismatch { expected, found }
    }

    /// Builds an [`Error::ShapeMismatch`] from the expected and found shapes
    ///
    /// Accepts anything convertible to `Vec<usize>`, including `&[usize]` (for example `array.shape()`)
    ///
    /// # Parameters
    ///
    /// - `expected` - Required shape
    /// - `found` - Provided shape
    ///
    /// # Returns
    ///
    /// - `Self` - Error with the supplied details
    #[cold]
    pub fn shape_mismatch(expected: impl Into<Vec<usize>>, found: impl Into<Vec<usize>>) -> Self {
        Self::ShapeMismatch {
            expected: expected.into(),
            found: found.into(),
        }
    }

    /// Builds an [`Error::NonFinite`] naming where the non-finite value occurred
    ///
    /// # Parameters
    ///
    /// - `context` - Location of the non-finite value
    ///
    /// # Returns
    ///
    /// - `Self` - Error with the supplied details
    #[cold]
    pub fn non_finite(context: impl Into<String>) -> Self {
        Self::NonFinite(context.into())
    }

    /// Builds an [`Error::InvalidParameter`] from a parameter name and the reason it is invalid
    ///
    /// # Parameters
    ///
    /// - `name` - Parameter name
    /// - `reason` - Reason the value is invalid
    ///
    /// # Returns
    ///
    /// - `Self` - Error with the supplied details
    #[cold]
    pub fn invalid_parameter(name: impl Into<String>, reason: impl Into<String>) -> Self {
        Self::InvalidParameter {
            name: name.into(),
            reason: reason.into(),
        }
    }

    /// Builds an [`Error::InvalidInput`] (the fallback for validation failures without a more
    /// specific variant)
    ///
    /// # Parameters
    ///
    /// - `msg` - Input validation message
    ///
    /// # Returns
    ///
    /// - `Self` - Error with the supplied details
    #[cold]
    pub fn invalid_input(msg: impl Into<String>) -> Self {
        Self::InvalidInput(msg.into())
    }

    /// Builds an [`Error::NotFitted`] for the named model
    ///
    /// # Parameters
    ///
    /// - `model` - Model name
    ///
    /// # Returns
    ///
    /// - `Self` - Error with the supplied details
    #[cold]
    pub fn not_fitted(model: &'static str) -> Self {
        Self::NotFitted(model)
    }

    /// Builds an [`Error::NotConverged`] with a description of the failure
    ///
    /// # Parameters
    ///
    /// - `msg` - Convergence failure message
    ///
    /// # Returns
    ///
    /// - `Self` - Error with the supplied details
    #[cold]
    pub fn not_converged(msg: impl Into<String>) -> Self {
        Self::NotConverged(msg.into())
    }

    /// Builds an [`Error::Computation`] with no wrapped source
    ///
    /// To wrap a lower-level error, use [`Context::context`] instead
    ///
    /// # Parameters
    ///
    /// - `context` - Computation failure message
    ///
    /// # Returns
    ///
    /// - `Self` - Error with the supplied details
    #[cold]
    pub fn computation(context: impl Into<String>) -> Self {
        Self::Computation {
            context: context.into(),
            source: None,
        }
    }
}

/// Lets `?` lift a raw [`std::io::Error`] directly into [`Error`] (as [`IoError::Std`])
impl From<std::io::Error> for Error {
    #[cold]
    fn from(e: std::io::Error) -> Self {
        Self::Io(IoError::Std(e))
    }
}

/// Lets `?` lift a raw [`postcard::Error`] directly into [`Error`] (as [`IoError::Serialization`])
impl From<postcard::Error> for Error {
    #[cold]
    fn from(e: postcard::Error) -> Self {
        Self::Io(IoError::Serialization(e))
    }
}

/// I/O and serialization errors, surfaced through [`Error::Io`]
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum IoError {
    /// A standard I/O error from a filesystem operation
    #[error("I/O error: {0}")]
    Std(
        /// Underlying filesystem error.
        #[from]
        std::io::Error,
    ),

    /// A binary serialization or deserialization error (postcard format)
    #[error("serialization error: {0}")]
    Serialization(
        /// Underlying binary format error.
        #[from]
        postcard::Error,
    ),

    /// The loaded model does not match the saved model.
    /// Layer counts, layer types, sublayer lists, or weight shapes can differ.
    #[error("model structure mismatch: {0}")]
    ModelStructureMismatch(
        /// Model structure mismatch details.
        String,
    ),

    /// The file is not a RustyML model file, or its on-disk format version is not the version this
    /// build writes
    ///
    /// Weight containers carry no layout tag of their own. A stale file's shapes can match the
    /// current model's by coincidence. A square convolution kernel is 1 example. A `Flatten` ->
    /// `Dense` pair is another, since its `Dense` weight shape stays the same under either tensor
    /// layout. The format version is the only reliable guard against loading weights laid out for
    /// a previous release
    #[error("unsupported model format: {0}")]
    UnsupportedModelFormat(
        /// Unsupported format details.
        String,
    ),
}

/// An alias for `Result<T, `[`Error`]`>`
///
/// Named distinctly, rather than shadowing [`std::result::Result`], so that a
/// `-> RustymlResult<T>` signature unmistakably means *this* crate's [`Error`]. Only [`Context`]
/// and the public signatures that use it need this alias
pub type RustymlResult<T> = std::result::Result<T, Error>;

/// Extension methods for attaching context to a foreign error while preserving its source chain
///
/// Implemented for every `Result<T, E>` whose error type is a `Send + Sync + 'static`
/// [`std::error::Error`], so it composes with `?`:
///
/// ```ignore
/// use crate::error::Context;
/// let reshaped = array.into_shape(shape).context("reshape for layer normalization")?;
/// ```
pub trait Context<T> {
    /// Wraps the error as the [`source`](std::error::Error::source) of an [`Error::Computation`]
    /// carrying the given context
    ///
    /// The `context` argument is evaluated **eagerly**, before the call, on both the `Ok` and
    /// `Err` paths. Use this when the context is a string literal or a value you already hold.
    /// If producing the message does any work (for example `format!`), use
    /// [`with_context`](Context::with_context) instead, so that work happens only on failure
    ///
    /// # Parameters
    ///
    /// - `context` - Message attached to a source error
    ///
    /// # Returns
    ///
    /// - `RustymlResult<T>` - Original value or contextual error
    ///
    /// # Errors
    ///
    /// - [`Error::Computation`] - The source result contains an error
    fn context(self, context: impl Into<String>) -> RustymlResult<T>;

    /// Like [`context`](Context::context), but a closure produces the context **lazily**, running
    /// only on the `Err` path
    ///
    /// Prefer this whenever building the message allocates or computes, typically anything using
    /// `format!`, so the common `Ok` path pays nothing
    ///
    /// # Parameters
    ///
    /// - `f` - Closure that supplies a message on failure
    ///
    /// # Returns
    ///
    /// - `RustymlResult<T>` - Original value or contextual error
    ///
    /// # Errors
    ///
    /// - [`Error::Computation`] - The source result contains an error
    fn with_context<F, S>(self, f: F) -> RustymlResult<T>
    where
        F: FnOnce() -> S,
        S: Into<String>;
}

impl<T, E> Context<T> for std::result::Result<T, E>
where
    E: std::error::Error + Send + Sync + 'static,
{
    #[cold]
    fn context(self, context: impl Into<String>) -> RustymlResult<T> {
        self.map_err(|e| Error::Computation {
            context: context.into(),
            source: Some(Box::new(e)),
        })
    }

    #[cold]
    fn with_context<F, S>(self, f: F) -> RustymlResult<T>
    where
        F: FnOnce() -> S,
        S: Into<String>,
    {
        self.map_err(|e| Error::Computation {
            context: f().into(),
            source: Some(Box::new(e)),
        })
    }
}

/// Unit tests for error construction, `Display` formatting, and the `Context` extension trait
#[cfg(test)]
mod tests {
    use super::*;
    use std::cell::Cell;
    use std::error::Error as StdError;

    /// `.context(msg)` on an `Err` wraps it as `Error::Computation`, carrying the message. The
    /// original error survives as the `source` (downcastable to the concrete type), so the chain
    /// is not lost
    #[test]
    fn context_wraps_err_as_computation_preserving_source() {
        let parsed: Result<i32, _> = "not a number".parse::<i32>();
        match parsed.context("parsing the threshold") {
            Err(Error::Computation { context, source }) => {
                assert_eq!(context, "parsing the threshold");
                let src = source.expect("the original error must be preserved as the source");
                assert!(
                    src.downcast_ref::<std::num::ParseIntError>().is_some(),
                    "source must downcast back to the original ParseIntError"
                );
            }
            other => panic!("expected Error::Computation, got {other:?}"),
        }
    }

    /// The wrapped error is reachable through the standard `std::error::Error::source()` chain
    #[test]
    fn context_exposes_wrapped_error_via_std_source() {
        let err = "x".parse::<i32>().context("ctx").unwrap_err();
        let src = StdError::source(&err).expect("source() must return Some");
        assert!(src.downcast_ref::<std::num::ParseIntError>().is_some());
    }

    /// `.context(_)` is a transparent pass-through on the `Ok` path
    #[test]
    fn context_is_passthrough_on_ok() {
        let ok: Result<i32, std::num::ParseIntError> = Ok(42);
        assert_eq!(ok.context("unused").unwrap(), 42);
    }

    /// `with_context`'s closure is **lazy**: it runs only on the `Err` path, never on `Ok`
    #[test]
    fn with_context_closure_runs_only_on_err() {
        // Ok path: the closure must not run
        let ran_on_ok = Cell::new(false);
        let ok: Result<i32, std::num::ParseIntError> = Ok(7);
        let passed = ok.with_context(|| {
            ran_on_ok.set(true);
            "should never be built"
        });
        assert_eq!(passed.unwrap(), 7);
        assert!(
            !ran_on_ok.get(),
            "with_context closure must not run on the Ok path"
        );

        // Err path: the closure runs and supplies the context
        let ran_on_err = Cell::new(false);
        let wrapped = "nope".parse::<i32>().with_context(|| {
            ran_on_err.set(true);
            format!("lazy context {}", 1)
        });
        assert!(
            ran_on_err.get(),
            "with_context closure must run on the Err path"
        );
        match wrapped {
            Err(Error::Computation { context, source }) => {
                assert_eq!(context, "lazy context 1");
                assert!(source.is_some(), "source must be preserved");
            }
            other => panic!("expected Error::Computation, got {other:?}"),
        }
    }
    // Each expected string below is reproduced by hand from its `#[error("...")]` format string.
    // Debug-formatted `Vec<usize>` (the `{:?}` in ShapeMismatch) renders as `[a, b]`.

    /// `#[error("input is empty: {0}")]` with payload `"target vector"`
    #[test]
    fn display_empty_input() {
        let e = Error::empty_input("target vector");
        assert_eq!(e.to_string(), "input is empty: target vector");
    }

    /// `#[error("dimension mismatch: expected {expected}, found {found}")]`
    #[test]
    fn display_dimension_mismatch() {
        let e = Error::dimension_mismatch(3, 5);
        assert_eq!(e.to_string(), "dimension mismatch: expected 3, found 5");
    }

    /// `#[error("shape mismatch: expected {expected:?}, found {found:?}")]`. The `{:?}`
    /// renders each `Vec<usize>` as `[.., ..]`
    #[test]
    fn display_shape_mismatch() {
        let e = Error::shape_mismatch(vec![2usize, 3], vec![2usize, 4]);
        assert_eq!(
            e.to_string(),
            "shape mismatch: expected [2, 3], found [2, 4]"
        );
    }

    /// `#[error("invalid parameter `{name}`: {reason}")]`. Note the literal backticks
    /// around the parameter name
    #[test]
    fn display_invalid_parameter() {
        let e = Error::invalid_parameter("C", "must be > 0");
        assert_eq!(e.to_string(), "invalid parameter `C`: must be > 0");
    }

    /// `#[error("model `{0}` has not been fitted; call `fit` before this operation")]`
    #[test]
    fn display_not_fitted() {
        let e = Error::not_fitted("KMeans");
        assert_eq!(
            e.to_string(),
            "model `KMeans` has not been fitted; call `fit` before this operation"
        );
    }

    // Below: smart constructors not covered above (NonFinite, InvalidInput, the source-less
    // Computation constructor), plus the RustymlResult alias.

    /// `Error::non_finite` builds `NonFinite` carrying the supplied context string
    #[test]
    fn non_finite_constructor_carries_context() {
        match Error::non_finite("weights") {
            Error::NonFinite(ref ctx) => assert!(
                ctx.contains("weights"),
                "context should mention 'weights', got: {ctx}"
            ),
            other => panic!("expected NonFinite, got {other:?}"),
        }
    }

    /// `Error::invalid_input` builds `InvalidInput` carrying the supplied message
    #[test]
    fn invalid_input_constructor_carries_message() {
        match Error::invalid_input("unexpected rank") {
            Error::InvalidInput(ref msg) => assert!(
                msg.contains("unexpected rank"),
                "message should mention the supplied text, got: {msg}"
            ),
            other => panic!("expected InvalidInput, got {other:?}"),
        }
    }

    /// `Error::computation` builds `Computation` with the context set and `source` `None`.
    /// Wrap a lower-level error as the source through `Context::context` instead
    #[test]
    fn computation_constructor_has_no_source() {
        match Error::computation("overflow") {
            Error::Computation {
                ref context,
                ref source,
            } => {
                assert!(
                    context.contains("overflow"),
                    "context should contain 'overflow'"
                );
                assert!(
                    source.is_none(),
                    "source should be None for Error::computation"
                );
            }
            other => panic!("expected Computation, got {other:?}"),
        }
    }

    /// `RustymlResult<T>` is a transparent alias for `Result<T, Error>` and is usable as 1
    #[test]
    fn rustyml_result_is_result_alias() {
        let ok: RustymlResult<i32> = Ok(42);
        assert!(matches!(ok, Ok(42)));
        let err: RustymlResult<i32> = Err(Error::empty_input("test"));
        assert!(matches!(err, Err(Error::EmptyInput(_))));
    }
}