gix_attributes/
assignment.rs

1use std::fmt::Write;
2
3use bstr::ByteSlice;
4
5use crate::{Assignment, AssignmentRef, NameRef, StateRef};
6
7impl<'a> AssignmentRef<'a> {
8    pub(crate) fn new(name: NameRef<'a>, state: StateRef<'a>) -> AssignmentRef<'a> {
9        AssignmentRef { name, state }
10    }
11
12    /// Turn this reference into its owned counterpart.
13    pub fn to_owned(self) -> Assignment {
14        self.into()
15    }
16}
17
18impl<'a> From<AssignmentRef<'a>> for Assignment {
19    fn from(a: AssignmentRef<'a>) -> Self {
20        Assignment {
21            name: a.name.to_owned(),
22            state: a.state.to_owned(),
23        }
24    }
25}
26
27impl<'a> Assignment {
28    /// Provide a ref type to this owned instance.
29    pub fn as_ref(&'a self) -> AssignmentRef<'a> {
30        AssignmentRef::new(self.name.as_ref(), self.state.as_ref())
31    }
32}
33
34impl std::fmt::Display for AssignmentRef<'_> {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        match self.state {
37            StateRef::Set => f.write_str(self.name.as_str()),
38            StateRef::Unset => {
39                f.write_char('-')?;
40                f.write_str(self.name.as_str())
41            }
42            StateRef::Value(v) => {
43                f.write_str(self.name.as_str())?;
44                f.write_char('=')?;
45                f.write_str(v.as_bstr().to_str_lossy().as_ref())
46            }
47            StateRef::Unspecified => {
48                f.write_char('!')?;
49                f.write_str(self.name.as_str())
50            }
51        }
52    }
53}