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
use typed_builder::TypedBuilder;

use crate::{EvaluationReason, FlagMetadata};

/// A structure which contains a subset of the fields defined in the evaluation details,
/// representing the result of the provider's flag resolution process.
#[derive(Clone, TypedBuilder, Debug)]
pub struct ResolutionDetails<T> {
    /// In cases of normal execution, the provider MUST populate the resolution details structure's
    /// value field with the resolved flag value.
    pub value: T,

    /// In cases of normal execution, the provider SHOULD populate the resolution details
    /// structure's variant field with a string identifier corresponding to the returned flag
    /// value.
    #[builder(default, setter(into, strip_option))]
    pub variant: Option<String>,

    /// The provider SHOULD populate the resolution details structure's reason field with "STATIC",
    /// "DEFAULT", "TARGETING_MATCH", "SPLIT", "CACHED", "DISABLED", "UNKNOWN", "ERROR" or some
    /// other string indicating the semantic reason for the returned flag value.
    #[builder(default, setter(strip_option))]
    pub reason: Option<EvaluationReason>,

    /// The provider SHOULD populate the resolution details structure's flag metadata field.
    #[builder(default, setter(strip_option))]
    pub flag_metadata: Option<FlagMetadata>,
}

impl<T: Default> Default for ResolutionDetails<T> {
    fn default() -> Self {
        Self {
            value: T::default(),
            variant: None,
            reason: None,
            flag_metadata: None,
        }
    }
}

impl<T> ResolutionDetails<T> {
    /// Create an instance given value.
    pub fn new(value: impl Into<T>) -> Self {
        Self {
            value: value.into(),
            variant: None,
            reason: None,
            flag_metadata: None,
        }
    }
}