git_config_value/
boolean.rs

1use std::{borrow::Cow, convert::TryFrom, ffi::OsString, fmt::Display};
2
3use bstr::{BStr, BString, ByteSlice};
4
5use crate::{Boolean, Error};
6
7fn bool_err(input: impl Into<BString>) -> Error {
8    Error::new(
9        "Booleans need to be 'no', 'off', 'false', '' or 'yes', 'on', 'true' or any number",
10        input,
11    )
12}
13
14impl TryFrom<OsString> for Boolean {
15    type Error = Error;
16
17    fn try_from(value: OsString) -> Result<Self, Self::Error> {
18        let value = git_path::os_str_into_bstr(&value)
19            .map_err(|_| Error::new("Illformed UTF-8", std::path::Path::new(&value).display().to_string()))?;
20        Self::try_from(value)
21    }
22}
23
24/// # Warning
25///
26/// The direct usage of `try_from("string")` is discouraged as it will produce the wrong result for values
27/// obtained from `core.bool-implicit-true`, which have no separator and are implicitly true.
28/// This method chooses to work correctly for `core.bool-empty=`, which is an empty string and resolves
29/// to being `false`.
30///
31/// Instead of this, obtain booleans with `config.boolean(…)`, which handles the case were no separator is
32/// present correctly.
33impl TryFrom<&BStr> for Boolean {
34    type Error = Error;
35
36    fn try_from(value: &BStr) -> Result<Self, Self::Error> {
37        if parse_true(value) {
38            Ok(Boolean(true))
39        } else if parse_false(value) {
40            Ok(Boolean(false))
41        } else {
42            use std::str::FromStr;
43            if let Some(integer) = value.to_str().ok().and_then(|s| i64::from_str(s).ok()) {
44                Ok(Boolean(integer != 0))
45            } else {
46                Err(bool_err(value))
47            }
48        }
49    }
50}
51
52impl Boolean {
53    /// Return true if the boolean is a true value.
54    ///
55    /// Note that the inner value is accessible directly as well.
56    pub fn is_true(self) -> bool {
57        self.0
58    }
59}
60
61impl TryFrom<Cow<'_, BStr>> for Boolean {
62    type Error = Error;
63    fn try_from(c: Cow<'_, BStr>) -> Result<Self, Self::Error> {
64        Self::try_from(c.as_ref())
65    }
66}
67
68impl Display for Boolean {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        self.0.fmt(f)
71    }
72}
73
74impl From<Boolean> for bool {
75    fn from(b: Boolean) -> Self {
76        b.0
77    }
78}
79
80#[cfg(feature = "serde")]
81impl serde::Serialize for Boolean {
82    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
83    where
84        S: serde::Serializer,
85    {
86        serializer.serialize_bool(self.0)
87    }
88}
89
90fn parse_true(value: &BStr) -> bool {
91    value.eq_ignore_ascii_case(b"yes") || value.eq_ignore_ascii_case(b"on") || value.eq_ignore_ascii_case(b"true")
92}
93
94fn parse_false(value: &BStr) -> bool {
95    value.eq_ignore_ascii_case(b"no")
96        || value.eq_ignore_ascii_case(b"off")
97        || value.eq_ignore_ascii_case(b"false")
98        || value.is_empty()
99}