summaryrefslogtreecommitdiffstats
path: root/src/theme.rs
blob: 2ac93f89cb93776f4fc4b16d884b5c80497de725 (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
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

use ini::Ini;

pub const THEMES_DIRECTORY: &str = "/usr/share/plymouth/themes";
pub const THEME_FILE_EXTENSION: &str = "plymouth";

#[derive(Debug)]
pub struct Theme {
    path: PathBuf,
    name: String,
    description: String,
    module_name: String,
}

fn find_theme_config_file(path: &Path) -> io::Result<PathBuf> {
    fs::read_dir(&path)?
        .filter_map(|p| p.ok())
        .map(|e| e.path())
        .filter(|p| match p.extension() {
            Some(ext) => ext == THEME_FILE_EXTENSION,
            None => false,
        })
        .nth(0)
        .map_or(
            Err(io::Error::new(
                io::ErrorKind::NotFound,
                "No *.plymouth file found in directory",
            )),
            |e| Ok(e),
        )
}

impl Theme {
    pub fn from_ini<P: AsRef<Path>>(path: P, ini: Ini) -> Self {
        let theme_section = ini
            .section(Some("Plymouth Theme"))
            .expect("could not find [Plymouth Theme] section");
        let name = theme_section.get("Name").unwrap_or("").into();
        let description = theme_section.get("Description").unwrap_or("").into();
        let module_name = theme_section.get("ModuleName").unwrap_or("").into();

        Theme {
            path: PathBuf::from(path.as_ref()),
            name,
            description,
            module_name,
        }
    }

    pub fn from_path<P: AsRef<Path>>(path: P) -> io::Result<Self> {
        let config_path = find_theme_config_file(path.as_ref())?;

        let ini = Ini::load_from_file(&config_path).map_err(|e| super::ini_to_io_err(e))?;

        Ok(Self::from_ini(path, ini))
    }

    pub fn from_name(name: &str) -> io::Result<Self> {
        let path = Path::new(THEMES_DIRECTORY).join(name);
        Self::from_path(path)
    }

    pub fn list() -> io::Result<impl Iterator<Item = String>> {
        fs::read_dir(THEMES_DIRECTORY).and_then(|d| {
            Ok(d.filter_map(|p| p.ok())
                .filter_map(|e| e.file_name().into_string().ok()))
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn from_ini() {
        let mut ini = Ini::new();
        ini.with_section(Some("Plymouth Theme"))
            .set("Name", "foobar")
            .set("Description", "A foo and a bar.")
            .set("ModuleName", "script");

        let theme = Theme::from_ini("/dev/null", ini);
        assert!(theme.name == "foobar");
        assert!(theme.description == "A foo and a bar.");
        assert!(theme.module_name == "script");
    }

    #[test]
    fn from_ini_default_values() {
        let mut ini = Ini::new();
        // rust-ini does not support creating an empty section, so we add
        // a fake foo=bar entry to create the section
        ini.with_section(Some("Plymouth Theme")).set("foo", "bar");

        let theme = Theme::from_ini("/dev/null", ini);
        assert!(theme.name == "");
        assert!(theme.description == "");
        assert!(theme.module_name == "");
    }
}