fancy_tree/config/main/
mod.rs1use super::ConfigFile;
3use crate::color::ColorChoice;
4use crate::lua::interop;
5use crate::tree::Entry;
6use mlua::{FromLua, Lua};
7use std::path::Path;
8
9#[derive(Debug)]
11pub struct Main {
12 color: Option<ColorChoice>,
14 skip: Option<mlua::Function>,
16}
17
18impl Main {
19 #[inline]
21 pub fn color_choice(&self) -> Option<ColorChoice> {
22 self.color
23 }
24 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}