comtrya_lib/contexts/
privilege.rs

1use crate::config::Config;
2use crate::contexts::{Context, ContextProvider};
3use serde::{Deserialize, Serialize};
4use std::fmt::Display;
5#[derive(Debug, Serialize, Deserialize, Clone)]
6pub enum Privilege {
7    #[serde(alias = "sudo")]
8    Sudo,
9
10    #[serde(alias = "doas")]
11    Doas,
12
13    #[serde(alias = "run0")]
14    Run0,
15}
16
17impl Default for Privilege {
18    fn default() -> Self {
19        Privilege::Sudo
20    }
21}
22
23impl Display for Privilege {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        let str = match self {
26            Privilege::Sudo => "sudo".to_string(),
27            Privilege::Doas => "doas".to_string(),
28            Privilege::Run0 => "run0".to_string(),
29        };
30        write!(f, "{}", str)
31    }
32}
33
34pub struct PrivilegeContextProvider<'a> {
35    pub config: &'a Config,
36}
37
38impl<'a> ContextProvider for PrivilegeContextProvider<'a> {
39    fn get_prefix(&self) -> String {
40        "privilege".to_string()
41    }
42
43    fn get_contexts(&self) -> anyhow::Result<Vec<Context>> {
44        let mut contexts = vec![];
45
46        contexts.push(Context::KeyValueContext(
47            "privilege".to_string(),
48            self.config.privilege.to_string().into(),
49        ));
50
51        Ok(contexts)
52    }
53}