summaryrefslogtreecommitdiffstats
path: root/src/flags/permission.rs
blob: 2cced0bdec388070fdd67cbf97d60fa34843a932 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! This module defines the [PermissionFlag]. To set it up from [Cli], a [Config] and its
//! [Default] value, use its [configure_from](Configurable::configure_from) method.

use super::Configurable;

use crate::app::Cli;
use crate::config_file::Config;

use serde::Deserialize;

/// The flag showing which file permissions units to use.
#[derive(Clone, Debug, Copy, PartialEq, Eq, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
pub enum PermissionFlag {
    /// The variant to show file permissions in rwx format
    #[default]
    Rwx,
    /// The variant to show file permissions in octal format
    Octal,
    /// Disable the display of owner and permissions, may be used to speed up in Windows
    Disable,
}

impl PermissionFlag {
    fn from_arg_str(value: &str) -> Self {
        match value {
            "rwx" => Self::Rwx,
            "octal" => Self::Octal,
            "disable" => Self::Disable,
            // Invalid value should be handled by `clap` when building an `Cli`
            other => unreachable!("Invalid value '{other}' for 'permission'"),
        }
    }
}

impl Configurable<Self> for PermissionFlag {
    /// Get a potential `PermissionFlag` variant from [Cli].
    ///
    /// If any of the "rwx" or "octal" arguments is passed, the corresponding
    /// `PermissionFlag` variant is returned in a [Some]. If neither of them is passed,
    /// this returns [None].
    /// Sets permissions to rwx if classic flag is enabled.
    fn from_cli(cli: &Cli) -> Option<Self> {
        if cli.classic {
            Some(Self::Rwx)
        } else {
            cli.permission.as_deref().map(Self::from_arg_str)
        }
    }

    /// Get a potential `PermissionFlag` variant from a [Config].
    ///
    /// If the `Config::permissions` has value and is one of "rwx" or "octal",
    /// this returns the corresponding `PermissionFlag` variant in a [Some].
    /// Otherwise this returns [None].
    /// Sets permissions to rwx if classic flag is enabled.
    fn from_config(config: &Config) -> Option<Self> {
        if config.classic == Some(true) {
            Some(Self::Rwx)
        } else {
            config.permission
        }
    }
}

#[cfg(test)]
mod test {
    use clap::Parser;

    use super::PermissionFlag;

    use crate::app::Cli;
    use crate::config_file::Config;
    use crate::flags::Configurable;

    #[test]
    fn test_default() {
        assert_eq!(PermissionFlag::Rwx, PermissionFlag::default());
    }

    #[test]
    fn test_from_cli_none() {
        let argv = ["lsd"];
        let cli = Cli::try_parse_from(argv).unwrap();
        assert_eq!(None, PermissionFlag::from_cli(&cli));
    }

    #[test]
    fn test_from_cli_default() {
        let argv = ["lsd", "--permission", "rwx"];
        let cli = Cli::try_parse_from(argv).unwrap();
        assert_eq!(Some(PermissionFlag::Rwx), PermissionFlag::from_cli(&cli));
    }

    #[test]
    fn test_from_cli_short() {
        let argv = ["lsd", "--permission", "octal"];
        let cli = Cli::try_parse_from(argv).unwrap();
        assert_eq!(Some(PermissionFlag::Octal), PermissionFlag::from_cli(&cli));
    }

    #[test]
    fn test_from_cli_permissions_disable() {
        let argv = ["lsd", "--permission", "disable"];
        let cli = Cli::try_parse_from(argv).unwrap();
        assert_eq!(
            Some(PermissionFlag::Disable),
            PermissionFlag::from_cli(&cli)
        );
    }

    #[test]
    #[should_panic]
    fn test_from_cli_unknown() {
        let argv = ["lsd", "--permission", "unknown"];
        let _ = Cli::try_parse_from(argv).unwrap();
    }
    #[test]
    fn test_from_cli_permissions_multi() {
        let argv = ["lsd", "--permission", "octal", "--permission", "rwx"];
        let cli = Cli::try_parse_from(argv).unwrap();
        assert_eq!(Some(PermissionFlag::Rwx), PermissionFlag::from_cli(&cli));
    }

    #[test]
    fn test_from_cli_permissions_classic() {
        let argv = ["lsd", "--permission", "rwx", "--classic"];
        let cli = Cli::try_parse_from(argv).unwrap();
        assert_eq!(Some(PermissionFlag::Rwx), PermissionFlag::from_cli(&cli));
    }

    #[test]
    fn test_from_config_none() {
        assert_eq!(None, PermissionFlag::from_config(&Config::with_none()));
    }

    #[test]
    fn test_from_config_rwx() {
        let mut c = Config::with_none();
        c.permission = Some(PermissionFlag::Rwx);
        assert_eq!(Some(PermissionFlag::Rwx), PermissionFlag::from_config(&c));
    }

    #[test]
    fn test_from_config_octal() {
        let mut c = Config::with_none();
        c.permission = Some(PermissionFlag::Octal);
        assert_eq!(Some(PermissionFlag::Octal), PermissionFlag::from_config(&c));
    }

    #[test]
    fn test_from_config_classic_mode() {
        let mut c = Config::with_none();
        c.classic = Some(true);
        assert_eq!(Some(PermissionFlag::Rwx), PermissionFlag::from_config(&c));
    }
}