Skip to main content

lux_cli/
args.rs

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