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
use std::collections::HashMap;

use super::{Error, Name};
use crate::sections::{Line, PasswordRef, Section};

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Group {
    pub name: String,
    pub users: Vec<String>,
}

/// Owned variant of [PasswordRef]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum Password {
    Secure(String),
    Insecure(String),
}

impl From<&PasswordRef<'_>> for Password {
    fn from(value: &PasswordRef) -> Self {
        match value {
            PasswordRef::Secure(p) => Self::Secure(p.to_string()),
            PasswordRef::Insecure(p) => Self::Insecure(p.to_string()),
        }
    }
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct User {
    pub name: String,
    pub password: Password,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Userlist {
    pub name: String,
    pub groups: Vec<Group>,
    pub users: Vec<User>,
}

type Pair = (Name, Userlist);
impl<'a> TryFrom<&'a Section<'a>> for Pair {
    type Error = Error<'a>;

    fn try_from(entry: &'a Section<'a>) -> Result<Pair, Self::Error> {
        let Section::Userlist{name, lines, ..} = entry else {
            unreachable!()
        };

        let mut users = Vec::new();
        let mut groups = Vec::new();
        let mut other = Vec::new();

        for line in lines
            .iter()
            .filter(|l| !matches!(l, Line::Blank | Line::Comment(_)))
        {
            match line {
                Line::User { name, password, .. } => users.push(User {
                    name: name.to_string(),
                    password: password.into(),
                }),
                Line::Group { name, users, .. } => groups.push(Group {
                    name: name.to_string(),
                    users: users.iter().map(ToString::to_string).collect(),
                }),
                _other => other.push(_other),
            }
        }

        if !other.is_empty() {
            return Err(Error::WrongUserlistLines(other));
        }

        Ok((
            name.to_string(),
            Userlist {
                name: name.to_string(),
                users,
                groups,
            },
        ))
    }
}

impl<'a> Userlist {
    pub fn parse_multiple(entries: &'a [Section<'a>]) -> Result<HashMap<Name, Self>, Error<'a>> {
        entries
            .iter()
            .filter(|e| matches!(e, Section::Userlist { .. }))
            .map(Pair::try_from)
            .collect()
    }
}