A tmpfiles.d implementation for UNIX-like operating systems.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

/// Types available for usage in tmpfiles.d configuration files.
/// See https://www.freedesktop.org/software/systemd/man/latest/tmpfiles.d.html#Type
const VALID_TYPES: [&'static str; 26] = [
    "f", "w", "d", "D", "e", "v", "q", "Q", "p", "p+", "L", "c", "b", "C", "x", "X", "r", "R", "z",
    "Z", "t", "T", "h", "H", "a", "A",
];

/// Type modifiers for usage in tmpfiles.d configuration files.
/// See https://www.freedesktop.org/software/systemd/man/latest/tmpfiles.d.html#Type%20Modifiers
const VALID_TYPE_MODIFIERS: [&'static str; 6] = ["+", "!", "-", "=", "~", "^"];

/// Specifiers for usage in tmpfiles.d configuration files.
/// See https://www.freedesktop.org/software/systemd/man/latest/tmpfiles.d.html#Specifiers
const VALID_SPECIFIERS: [&'static str; 25] = [
    "%a", "%A", "%b", "%B", "%C", "%g", "%G", "%h", "%H", "%l", "%L", "%m", "%M", "%o", "%q", "%S",
    "%t", "%T", "%u", "%U", "%v", "%V", "%w", "%W", "%%",
];

pub fn sanity_check(config_line: &Vec<&str>) -> Result<(), &'static str> {
    if VALID_TYPES.contains(
        &String::from(config_line[0])
            .chars()
            .next()
            .unwrap()
            .to_string()
            .as_str(),
    ) == false
    {
        return Err("0");
    }
    if String::from(config_line[1])
        .chars()
        .next()
        .unwrap()
        .to_string()
        != "/"
    {
        return Err("1");
    }
    if config_line[2].parse::<i32>().is_err() {
        if config_line[2] != "-" {
            return Err("2");
        }
    }
    Ok(())
}