nextest_runner/config/
track_default.rs

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
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use serde::{Deserialize, Deserializer};

/// Tracks if a particular default is implicit.
///
/// The `default` impl can be passed in to `serde` directly, if desired.
///
/// This could be its own crate maybe?
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub(crate) struct TrackDefault<T> {
    /// The deserialized or default value.
    pub value: T,
    /// Whether the value was deserialized.
    pub is_deserialized: bool,
}

impl<T> TrackDefault<T> {
    pub fn with_default_value(value: T) -> Self {
        Self {
            value,
            is_deserialized: false,
        }
    }

    pub fn with_deserialized_value(value: T) -> Self {
        Self {
            value,
            is_deserialized: true,
        }
    }
}

impl<'de, T> Deserialize<'de> for TrackDefault<T>
where
    T: Deserialize<'de>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = Self::with_deserialized_value(T::deserialize(deserializer)?);
        Ok(value)
    }
}