fancy_tree/lua/
interop.rs1use crate::tree::Entry;
3use crate::tree::entry::Attributes;
4use mlua::{IntoLua, Lua};
5use std::path::Path;
6
7pub struct FileAttributes<'a, P: AsRef<Path>>(&'a Entry<P>);
9
10impl<'a, P> FileAttributes<'a, P>
11where
12 P: AsRef<Path>,
13{
14 #[inline]
16 fn is_hidden(&self) -> bool {
17 self.0.is_hidden()
18 }
19
20 #[inline]
22 fn is_executable(&self) -> bool {
23 self.0.is_executable()
24 }
25
26 fn file_type(&self) -> &str {
28 const DIRECTORY: &str = "directory";
29 const FILE: &str = "file";
30 const SYMLINK: &str = "symlink";
31
32 match self.0.attributes() {
33 Attributes::Directory(_) => DIRECTORY,
34 Attributes::File(_) => FILE,
35 Attributes::Symlink(_) => SYMLINK,
36 }
37 }
38
39 fn language(&self) -> Option<&'static str> {
41 self.0
42 .attributes()
43 .file()
44 .and_then(|file| file.language())
45 .map(|language| language.name())
46 }
47}
48
49impl<'a, P> IntoLua for FileAttributes<'a, P>
50where
51 P: AsRef<Path>,
52{
53 fn into_lua(self, lua: &Lua) -> mlua::Result<mlua::Value> {
54 let table = lua.create_table()?;
55 table.set("is_hidden", self.is_hidden())?;
56 table.set("is_executable", self.is_executable())?;
57 table.set("file_type", self.file_type())?;
58 table.set("language", self.language())?;
59 let table = mlua::Value::Table(table);
60 Ok(table)
61 }
62}
63
64impl<'a, P> From<&'a Entry<P>> for FileAttributes<'a, P>
65where
66 P: AsRef<Path>,
67{
68 #[inline]
69 fn from(value: &'a Entry<P>) -> Self {
70 Self(value)
71 }
72}