fapolicy_rules/set.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
11/// # Set
12/// Set is a named group of values of the same type.
13///
14/// - Fapolicyd internally distinguishes between INT and STRING set types.
15/// - You can define your own set and use it as a value for a specific rule attribute.
16/// - The definition is in `key=value` syntax and starts with a set name.
17/// - The set name has to start with `%` and the rest is alphanumeric or `_`. The value is a comma separated list.
18/// - The set type is inherited from the first item in the list.
19/// - If that can be turned into number then whole list is expected to carry numbers.
20/// - One can use these sets as a value for subject and object attributes.
21/// - It is also possible to use a plain list as an attribute value without previous definition.
22/// - The assigned set has to match the attribute type. It is not possible set groups for `TRUST` and `PATTERN` attributes.
23///
24#[derive(Clone, Debug, PartialEq)]
25pub struct Set {
26 pub name: String,
27 pub values: Vec<String>,
28}
29
30impl Set {
31 pub fn new(name: &str, list: Vec<String>) -> Self {
32 Set {
33 name: name.into(),
34 values: list,
35 }
36 }
37}
38
39impl Display for Set {
40 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41 let list: String = self.values.join(",");
42 f.write_fmt(format_args!("%{}={}", &self.name, list))
43 }
44}