Skip to content

Commit 7df8cf3

Browse files
committed
feat: make it easier to access the value of StateRef.
1 parent 9ad9c5b commit 7df8cf3

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

gix-attributes/src/state.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct Value(KString);
1313
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1414
pub struct ValueRef<'a>(#[cfg_attr(feature = "serde", serde(borrow))] KStringRef<'a>);
1515

16-
/// Conversions
16+
/// Lifecycle
1717
impl<'a> ValueRef<'a> {
1818
/// Keep `input` as our value.
1919
pub fn from_bytes(input: &'a [u8]) -> Self {
@@ -25,7 +25,10 @@ impl<'a> ValueRef<'a> {
2525
},
2626
))
2727
}
28+
}
2829

30+
/// Access and conversions
31+
impl ValueRef<'_> {
2932
/// Access this value as byte string.
3033
pub fn as_bstr(&self) -> &BStr {
3134
self.0.as_bytes().as_bstr()
@@ -79,6 +82,14 @@ impl StateRef<'_> {
7982
pub fn is_unset(&self) -> bool {
8083
matches!(self, StateRef::Unset)
8184
}
85+
86+
/// Attempt to obtain the string value of this state, or return `None` if there is no such value.
87+
pub fn as_bstr(&self) -> Option<&BStr> {
88+
match self {
89+
StateRef::Value(v) => Some(v.as_bstr()),
90+
_ => None,
91+
}
92+
}
8293
}
8394

8495
/// Initialization

0 commit comments

Comments
 (0)