Skip to main content

lux_cli/
args.rs

1use clap::ValueEnum;
2use lux_lib::package;
3use lux_lib::{
4    config::ConfigBuilder,
5    lua_installation::nvim_lua_version,
6    lua_version::LuaVersion,
7    tree::{NvimLayout, RojoLayout},
8};
9use miette::{miette, Result};
10use std::{path::PathBuf, str::FromStr};
11
12#[derive(Debug, Clone)]
13pub enum PackageOrRockspec {
14    Package(package::Req),
15    RockSpec(PathBuf),
16}
17
18#[derive(Debug, Clone, PartialEq, ValueEnum)]
19pub enum OutputFormat {
20    Json,
21    Text,
22}
23
24/// Configures Lux for a specific environment.
25#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
26pub enum Preset {
27    /// Configure Lux for [Neovim](https://neovim.io/) plugins.
28    Nvim,
29    /// Configure Lux for [Rojo](https://rojo.space/).
30    Rojo,
31}
32
33impl Preset {
34    pub fn lua_version(self) -> Option<LuaVersion> {
35        match self {
36            Self::Nvim => nvim_lua_version(),
37            Self::Rojo => Some(LuaVersion::Luau),
38        }
39    }
40
41    pub fn apply(self, config: ConfigBuilder) -> ConfigBuilder {
42        match self {
43            Self::Nvim => config.entrypoint_layout(NvimLayout),
44            Self::Rojo => config.entrypoint_layout(RojoLayout),
45        }
46    }
47}
48
49impl FromStr for PackageOrRockspec {
50    type Err = miette::Report;
51
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        let path = PathBuf::from(s);
54        if path.is_file() {
55            Ok(Self::RockSpec(path))
56        } else {
57            let pkg = package::Req::from_str(s).map_err(|err| {
58                miette!(
59                    help = format!("if '{s}' is a path to a file, ensure it exists"),
60                    "No file '{s}' found and cannot parse package query: {err}",
61                )
62            })?;
63            Ok(Self::Package(pkg))
64        }
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71    use crate::Cli;
72    use clap::Parser;
73    use lux_lib::lua_version::LuaVersion;
74
75    #[test]
76    fn parses_rojo_preset() {
77        let cli = Cli::try_parse_from(["lx", "--preset", "rojo", "list"]).unwrap();
78        assert_eq!(cli.preset, Some(Preset::Rojo));
79        assert_eq!(Preset::Rojo.lua_version(), Some(LuaVersion::Luau));
80    }
81
82    #[test]
83    fn parses_preset_and_deprecated_nvim_flag() {
84        let with_preset = Cli::try_parse_from(["lx", "--preset", "nvim", "list"]).unwrap();
85        assert_eq!(with_preset.preset, Some(Preset::Nvim));
86        assert!(!with_preset.nvim);
87
88        let with_nvim = Cli::try_parse_from(["lx", "--nvim", "list"]).unwrap();
89        assert!(with_nvim.nvim);
90        assert_eq!(with_nvim.preset, None);
91
92        assert!(Cli::try_parse_from(["lx", "--preset", "nvim", "--nvim", "list"]).is_err());
93    }
94}