fapolicy_rules/
file_type.rs

1/*
2 * Copyright Concurrent Technologies Corporation 2021
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 */
8
9use std::fmt::{Display, Formatter};
10
11use crate::set::Set;
12
13#[derive(Clone, Debug, PartialEq)]
14pub enum Rvalue {
15    Any,
16    Literal(String),
17    SetRef(Set),
18}
19
20impl Rvalue {
21    pub fn new_mime_type(name: &str) -> Rvalue {
22        Rvalue::Literal(name.into())
23    }
24}
25
26impl Display for Rvalue {
27    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28        match self {
29            Rvalue::Any => f.write_str("any"),
30            Rvalue::Literal(l) => f.write_fmt(format_args!("{}", l)),
31            Rvalue::SetRef(m) => f.write_fmt(format_args!("{}", m.name)),
32        }
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn display() {
42        let ft1 = Rvalue::new_mime_type("text/x-lua");
43        assert_eq!(format!("{}", ft1), format!("{}", &ft1));
44    }
45
46    #[test]
47    fn macro_mime_list() {
48        let l = "application/x-bytecode.ocaml,application/x-bytecode.python,application/java-archive,text/x-java";
49        let t = Set::new("lang", l.split(',').map(|s| s.into()).collect());
50        assert_eq!(format!("%lang={}", l), format!("{}", t));
51    }
52}