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
|
use crate::raw;
use std::ptr;
use std::str;
/// All possible states of an attribute.
///
/// This enum is used to interpret the value returned by
/// [`Repository::get_attr`](crate::Repository::get_attr) and
/// [`Repository::get_attr_bytes`](crate::Repository::get_attr_bytes).
#[derive(Debug, Clone, Copy, Eq)]
pub enum AttrValue<'string> {
/// The attribute is set to true.
True,
/// The attribute is unset (set to false).
False,
/// The attribute is set to a [valid UTF-8 string](prim@str).
String(&'string str),
/// The attribute is set to a string that might not be [valid UTF-8](prim@str).
Bytes(&'string [u8]),
/// The attribute is not specified.
Unspecified,
}
macro_rules! from_value {
($value:expr => $string:expr) => {
match unsafe { raw::git_attr_value($value.map_or(ptr::null(), |v| v.as_ptr().cast())) } {
raw::GIT_ATTR_VALUE_TRUE => Self::True,
raw::GIT_ATTR_VALUE_FALSE => Self::False,
raw::GIT_ATTR_VALUE_STRING => $string,
raw::GIT_ATTR_VALUE_UNSPECIFIED => Self::Unspecified,
_ => unreachable!(),
}
};
}
impl<'string> AttrValue<'string> {
/// Returns the state of an attribute by inspecting its [value](crate::Repository::get_attr)
/// by a [string](prim@str).
///
/// This function always returns [`AttrValue::String`] and never returns [`AttrValue::Bytes`]
/// when the attribute is set to a string.
pub fn from_string(value: Option<&'string str>) -> Self {
from_value!(value => Self::String(value.unwrap()))
}
/// Returns the state of an attribute by inspecting its [value](crate::Repository::get_attr_bytes)
/// by a [byte](u8) [slice].
///
/// This function will perform UTF-8 validation when the attribute is set to a string, returns
/// [`AttrValue::String`] if it's valid UTF-8 and [`AttrValue::Bytes`] otherwise.
pub fn from_bytes(value: Option<&'string [u8]>) -> Self {
let mut value = Self::always_bytes(value);
if let Self::Bytes(bytes) = value {
if let Ok(string) = str::from_utf8(bytes) {
value = Self::String(string);
}
}
value
}
/// Returns the state of an attribute just like [`AttrValue::from_bytes`], but skips UTF-8
/// validation and always returns [`AttrValue::Bytes`] when it's set to a string.
pub fn always_bytes(value: Option<&'string [u8]>) -> Self {
from_value!(value => Self::Bytes(value.unwrap()))
}
}
/// Compare two [`AttrValue`]s.
///
/// Note that this implementation does not differentiate between [`AttrValue::String`] and
/// [`AttrValue::Bytes`].
impl PartialEq for AttrValue<'_> {
fn eq(&self, other: &AttrValue<'_>) -> bool {
match (self, other) {
(Self::True, AttrValue::True)
| (Self::False, AttrValue::False)
| (Self::Unspecified, AttrValue::Unspecified) => true,
(AttrValue::String(string), AttrValue::Bytes(bytes))
| (AttrValue::Bytes(bytes), AttrValue::String(string)) => string.as_bytes() == *bytes,
(AttrValue::String(left), AttrValue::String(right)) => left == right,
(AttrValue::Bytes(left), AttrValue::Bytes(right)) => left == right,
_ => false,
}
}
}
#[cfg(test)]
mod tests {
use super::AttrValue;
macro_rules! test_attr_value {
($function:ident, $variant:ident) => {
const ATTR_TRUE: &str = "[internal]__TRUE__";
const ATTR_FALSE: &str = "[internal]__FALSE__";
const ATTR_UNSET: &str = "[internal]__UNSET__";
let as_bytes = AsRef::<[u8]>::as_ref;
// Use `matches!` here since the `PartialEq` implementation does not differentiate
// between `String` and `Bytes`.
assert!(matches!(
AttrValue::$function(Some(ATTR_TRUE.as_ref())),
AttrValue::$variant(s) if as_bytes(s) == ATTR_TRUE.as_bytes()
));
assert!(matches!(
AttrValue::$function(Some(ATTR_FALSE.as_ref())),
AttrValue::$variant(s) if as_bytes(s) == ATTR_FALSE.as_bytes()
));
assert!(matches!(
AttrValue::$function(Some(ATTR_UNSET.as_ref())),
AttrValue::$variant(s) if as_bytes(s) == ATTR_UNSET.as_bytes()
));
assert!(matches!(
AttrValue::$function(Some("foo".as_ref())),
AttrValue::$variant(s) if as_bytes(s) == b"foo"
));
assert!(matches!(
AttrValue::$function(Some("bar".as_ref())),
AttrValue::$variant(s) if as_bytes(s) == b"bar"
));
assert_eq!(AttrValue::$function(None), AttrValue::Unspecified);
};
}
#[test]
fn attr_value_from_string() {
test_attr_value!(from_string, String);
}
#[test]
fn attr_value_from_bytes() {
test_attr_value!(from_bytes, String);
assert!(matches!(
AttrValue::from_bytes(Some(&[0xff])),
AttrValue::Bytes(&[0xff])
));
assert!(matches!(
AttrValue::from_bytes(Some(b"\xffoobar")),
AttrValue::Bytes(b"\xffoobar")
));
}
#[test]
fn attr_value_always_bytes() {
test_attr_value!(always_bytes, Bytes);
assert!(matches!(
AttrValue::always_bytes(Some(&[0xff; 2])),
AttrValue::Bytes(&[0xff, 0xff])
));
assert!(matches!(
AttrValue::always_bytes(Some(b"\xffoo")),
AttrValue::Bytes(b"\xffoo")
));
}
#[test]
fn attr_value_partial_eq() {
assert_eq!(AttrValue::True, AttrValue::True);
assert_eq!(AttrValue::False, AttrValue::False);
assert_eq!(AttrValue::String("foo"), AttrValue::String("foo"));
assert_eq!(AttrValue::Bytes(b"foo"), AttrValue::Bytes(b"foo"));
assert_eq!(AttrValue::String("bar"), AttrValue::Bytes(b"bar"));
assert_eq!(AttrValue::Bytes(b"bar"), AttrValue::String("bar"));
assert_eq!(AttrValue::Unspecified, AttrValue::Unspecified);
assert_ne!(AttrValue::True, AttrValue::False);
assert_ne!(AttrValue::False, AttrValue::Unspecified);
assert_ne!(AttrValue::Unspecified, AttrValue::True);
assert_ne!(AttrValue::True, AttrValue::String("true"));
assert_ne!(AttrValue::Unspecified, AttrValue::Bytes(b"unspecified"));
assert_ne!(AttrValue::Bytes(b"false"), AttrValue::False);
assert_ne!(AttrValue::String("unspecified"), AttrValue::Unspecified);
assert_ne!(AttrValue::String("foo"), AttrValue::String("bar"));
assert_ne!(AttrValue::Bytes(b"foo"), AttrValue::Bytes(b"bar"));
assert_ne!(AttrValue::String("foo"), AttrValue::Bytes(b"bar"));
assert_ne!(AttrValue::Bytes(b"foo"), AttrValue::String("bar"));
}
}
|