lux_lib/config/
tree.rs

1use mlua::{FromLua, UserData};
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5/// Template configuration for a rock's tree layout
6#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, FromLua)]
7pub struct RockLayoutConfig {
8    /// The root of a packages `etc` directory.
9    /// If unset (the default), the root is the package root.
10    /// If set, it is a directory relative to the given Lua version's install tree root.
11    /// With the `--nvim` preset, this is `site/pack/lux`.
12    pub(crate) etc_root: Option<PathBuf>,
13    /// The `etc` directory for non-optional packages
14    /// Default: `etc` With the `--nvim` preset, this is `start`
15    /// Note: If `etc_root` is set, the package ID is appended.
16    pub(crate) etc: PathBuf,
17    /// The `etc` directory for optional packages
18    /// Default: `etc`
19    /// With the `--nvim` preset, this is `opt`
20    /// Note: If `etc_root` is set, the package ID is appended.
21    pub(crate) opt_etc: PathBuf,
22    /// The `conf` directory name
23    /// Default: `conf`
24    pub(crate) conf: PathBuf,
25    /// The `doc` directory name
26    /// Default: `doc`
27    pub(crate) doc: PathBuf,
28}
29
30impl RockLayoutConfig {
31    /// Creates a `RockLayoutConfig` for use with Neovim
32    /// - `etc_root`: `site/pack/lux`
33    /// - `etc`: `start`
34    /// - `opt_etc`: `opt`
35    pub fn new_nvim_layout() -> Self {
36        Self {
37            etc_root: Some("site/pack/lux".into()),
38            etc: "start".into(),
39            opt_etc: "opt".into(),
40            conf: "conf".into(),
41            doc: "doc".into(),
42        }
43    }
44
45    pub(crate) fn is_default(&self) -> bool {
46        &Self::default() == self
47    }
48}
49
50impl Default for RockLayoutConfig {
51    fn default() -> Self {
52        Self {
53            etc_root: None,
54            etc: "etc".into(),
55            opt_etc: "etc".into(),
56            conf: "conf".into(),
57            doc: "doc".into(),
58        }
59    }
60}
61
62impl UserData for RockLayoutConfig {
63    fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
64        methods.add_function("new", |_, ()| Ok(RockLayoutConfig::default()));
65        methods.add_function("new_nvim_layout", |_, ()| {
66            Ok(RockLayoutConfig::new_nvim_layout())
67        });
68    }
69}