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