use clap::ValueEnum;
use lux_lib::package;
use lux_lib::{
config, lua,
tree::{NvimLayout, RojoLayout},
};
use miette::{miette, Result};
use std::{path::PathBuf, str::FromStr};
#[derive(Debug, Clone)]
pub enum PackageOrRockspec {
Package(package::Req),
RockSpec(PathBuf),
}
#[derive(Debug, Clone, PartialEq, ValueEnum)]
pub enum OutputFormat {
Json,
Text,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum Preset {
Nvim,
Rojo,
}
impl Preset {
pub fn lua_version(self) -> Option<lua::Version> {
match self {
Self::Nvim => lua::installation::version_for_neovim(),
Self::Rojo => Some(lua::Version::Luau),
}
}
pub fn apply(self, config: config::Builder) -> config::Builder {
match self {
Self::Nvim => config.entrypoint_layout(NvimLayout),
Self::Rojo => config.entrypoint_layout(RojoLayout),
}
}
}
impl FromStr for PackageOrRockspec {
type Err = miette::Report;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let path = PathBuf::from(s);
if path.is_file() {
Ok(Self::RockSpec(path))
} else {
let pkg = package::Req::from_str(s).map_err(|err| {
miette!(
help = format!("if '{s}' is a path to a file, ensure it exists"),
"No file '{s}' found and cannot parse package query: {err}",
)
})?;
Ok(Self::Package(pkg))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Cli;
use clap::Parser;
use lux_lib::lua;
#[test]
fn parses_rojo_preset() {
let cli = Cli::try_parse_from(["lx", "--preset", "rojo", "list"]).unwrap();
assert_eq!(cli.preset, Some(Preset::Rojo));
assert_eq!(Preset::Rojo.lua_version(), Some(lua::Version::Luau));
}
#[test]
fn parses_preset_and_deprecated_nvim_flag() {
let with_preset = Cli::try_parse_from(["lx", "--preset", "nvim", "list"]).unwrap();
assert_eq!(with_preset.preset, Some(Preset::Nvim));
assert!(!with_preset.nvim);
let with_nvim = Cli::try_parse_from(["lx", "--nvim", "list"]).unwrap();
assert!(with_nvim.nvim);
assert_eq!(with_nvim.preset, None);
assert!(Cli::try_parse_from(["lx", "--preset", "nvim", "--nvim", "list"]).is_err());
}
}