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
 95
 96
 97
 98
 99
100
101
//! Authorization(Permission)
//!
//! Permission should be given for some basic macro types

#[derive(Debug)]
/// Struct that stores auth states
pub(crate) struct AuthFlags{
    auths: Vec<AuthState>,
}

impl AuthFlags {
    pub fn new() -> Self {
        let mut auths = Vec::new();
        for _ in 0..AuthType::LEN as usize {
            auths.push(AuthState::Restricted);
        }

        Self {
            auths,
        }
    }

    pub fn set_state(&mut self, auth_type: &AuthType, auth_state: AuthState) {
        self.auths[*auth_type as usize] = auth_state;
    }

    pub fn get_state(&self, auth_type: &AuthType) -> &AuthState {
        &self.auths[*auth_type as usize]
    }

    pub fn get_status_string(&self) -> Option<String> {
        let mut format = String::new();
        for index in 0..AuthType::LEN as usize {
            let auth_type = AuthType::from_usize(index).unwrap();
            match self.get_state(&auth_type) {
                &AuthState::Warn | &AuthState::Open  => {
                    // Add newline before
                    format.push_str("
");
                    format.push_str(&format!("Auth : {:?} is open.", auth_type));
                }
                &AuthState::Restricted => ()
            }
        }
        if format.len() != 0 {
            Some(format)
        } else {
            None
        }
    }
}

#[derive(Debug, Clone, Copy)]
/// Authorization type
///
/// ```text
/// Each variants means
/// - ENV  : environment variable permission
/// - FIN  : File in(read) permission
/// - FOUT : File out(write) permission
/// - CMD  : System command permission
/// - LEN  : This is a functional variant not a real value, namely a length
/// ```
pub enum AuthType {
    ENV = 0,
    FIN = 1,
    FOUT = 2,
    CMD = 3,
    LEN = 4,
}

impl AuthType {
    /// Convert str slice into AuthType
    pub fn from(string: &str) -> Option<Self> {
        match string.to_lowercase().as_str() {
            "env" => Some(Self::ENV),
            "fin" =>  Some(Self::FIN),
            "fout" =>  Some(Self::FOUT),
            "cmd" => Some(Self::CMD),
            _ => None
        }
    }

    pub fn from_usize(number : usize) -> Option<Self> {
        match number {
            0 => Some(Self::ENV),
            1 =>  Some(Self::FIN),
            2 =>  Some(Self::FOUT),
            3 => Some(Self::CMD),
            _ => None
        }
    }
}

#[derive(Clone, Copy, Debug)]
/// Current state authorization
pub(crate) enum AuthState {
    Restricted,
    Warn,
    Open,
}