fancy_tree/config/main/
mod.rs

1//! Module for the main config.
2use super::ConfigFile;
3use crate::color::ColorChoice;
4use crate::lua::interop;
5use crate::tree::Entry;
6use mlua::{FromLua, Lua};
7use std::path::Path;
8
9/// The main configuration type.
10#[derive(Debug)]
11pub struct Main {
12    // Determines when/how the application should show colors.
13    color: Option<ColorChoice>,
14    // Function to determine if a file should be skipped.
15    skip: Option<mlua::Function>,
16}
17
18impl Main {
19    /// Gets the configured color choice.
20    #[inline]
21    pub fn color_choice(&self) -> Option<ColorChoice> {
22        self.color
23    }
24    /// Should a file be skipped according to the configuration?
25    pub fn should_skip<P>(
26        &self,
27        entry: &Entry<P>,
28        default_choice: bool,
29    ) -> Option<mlua::Result<bool>>
30    where
31        P: AsRef<Path>,
32    {
33        let path = entry.path();
34        let attributes = interop::FileAttributes::from(entry);
35
36        self.skip
37            .as_ref()
38            .map(|f| f.call::<bool>((path, attributes, default_choice)))
39    }
40}
41
42impl ConfigFile for Main {
43    const FILENAME: &'static str = "config.lua";
44    const DEFAULT_MODULE: &'static str = include_str!("./config.lua");
45}
46
47impl FromLua for Main {
48    fn from_lua(value: mlua::Value, _lua: &Lua) -> mlua::Result<Self> {
49        let type_name = value.type_name();
50
51        let conversion_error = || mlua::Error::FromLuaConversionError {
52            from: type_name,
53            to: String::from("config::Main"),
54            message: None,
55        };
56
57        let table = value.as_table().ok_or_else(conversion_error)?;
58        let color: Option<ColorChoice> = table.get("color")?;
59        let skip: Option<mlua::Function> = table.get("skip")?;
60        let main = Main { color, skip };
61        Ok(main)
62    }
63}