summaryrefslogtreecommitdiffstats
path: root/src/options/option_value.rs
blob: d569f6767fc58703fa7f96956c45eb2e6529f35b (plain)
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
use crate::config::delta_unreachable;

/// A value associated with a Delta command-line option name.
pub enum OptionValue {
    Boolean(bool),
    Float(f64),
    OptionString(Option<String>),
    String(String),
    Int(usize),
}

/// An OptionValue, tagged according to its provenance/semantics.
pub enum ProvenancedOptionValue {
    GitConfigValue(OptionValue),
    DefaultValue(OptionValue),
}

impl From<bool> for OptionValue {
    fn from(value: bool) -> Self {
        OptionValue::Boolean(value)
    }
}

impl From<OptionValue> for bool {
    fn from(value: OptionValue) -> Self {
        match value {
            OptionValue::Boolean(value) => value,
            _ => delta_unreachable("Error converting OptionValue to bool."),
        }
    }
}

impl From<f64> for OptionValue {
    fn from(value: f64) -> Self {
        OptionValue::Float(value)
    }
}

impl From<OptionValue> for f64 {
    fn from(value: OptionValue) -> Self {
        match value {
            OptionValue::Float(value) => value,
            _ => delta_unreachable("Error converting OptionValue to f64."),
        }
    }
}

impl From<Option<String>> for OptionValue {
    fn from(value: Option<String>) -> Self {
        OptionValue::OptionString(value)
    }
}

impl From<OptionValue> for Option<String> {
    fn from(value: OptionValue) -> Self {
        match value {
            OptionValue::OptionString(value) => value,
            // HACK: See the comment in options::set::compute_line_numbers_mode(). That function
            // deliberately reads what is normally a boolean value ('line-numbers') as a string.
            // However options::get::get_option_value() can fall through to obtaining the value
            // from builtin_features, in which case an OptionValue::Boolean will be encountered.
            // See the comment in options::set::compute_line_numbers_mode() and docstring of
            // options::get::get_option_value().
            OptionValue::Boolean(_) => None,
            _ => delta_unreachable("Error converting OptionValue to Option<String>."),
        }
    }
}

impl From<String> for OptionValue {
    fn from(value: String) -> Self {
        OptionValue::String(value)
    }
}

impl From<&str> for OptionValue {
    fn from(value: &str) -> Self {
        value.to_string().into()
    }
}

impl From<OptionValue> for String {
    fn from(value: OptionValue) -> Self {
        match value {
            OptionValue::String(value) => value,
            _ => delta_unreachable("Error converting OptionValue to String."),
        }
    }
}

impl From<usize> for OptionValue {
    fn from(value: usize) -> Self {
        OptionValue::Int(value)
    }
}

impl From<OptionValue> for usize {
    fn from(value: OptionValue) -> Self {
        match value {
            OptionValue::Int(value) => value,
            _ => delta_unreachable("Error converting OptionValue to usize."),
        }
    }
}