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
use std::fmt::Write;

use bstr::ByteSlice;

use crate::{Assignment, AssignmentRef, NameRef, StateRef};

impl<'a> AssignmentRef<'a> {
    pub(crate) fn new(name: NameRef<'a>, state: StateRef<'a>) -> AssignmentRef<'a> {
        AssignmentRef { name, state }
    }

    /// Turn this reference into its owned counterpart.
    pub fn to_owned(self) -> Assignment {
        self.into()
    }
}

impl<'a> From<AssignmentRef<'a>> for Assignment {
    fn from(a: AssignmentRef<'a>) -> Self {
        Assignment {
            name: a.name.to_owned(),
            state: a.state.to_owned(),
        }
    }
}

impl<'a> Assignment {
    /// Provide a ref type to this owned instance.
    pub fn as_ref(&'a self) -> AssignmentRef<'a> {
        AssignmentRef::new(self.name.as_ref(), self.state.as_ref())
    }
}

impl std::fmt::Display for AssignmentRef<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.state {
            StateRef::Set => f.write_str(self.name.as_str()),
            StateRef::Unset => {
                f.write_char('-')?;
                f.write_str(self.name.as_str())
            }
            StateRef::Value(v) => {
                f.write_str(self.name.as_str())?;
                f.write_char('=')?;
                f.write_str(v.as_bstr().to_str_lossy().as_ref())
            }
            StateRef::Unspecified => {
                f.write_char('!')?;
                f.write_str(self.name.as_str())
            }
        }
    }
}