use clap::{Parser, Subcommand, ValueEnum};
use std::path::PathBuf;
use tower_lsp::{LspService, Server};
use tracing::Level;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
pub mod check;
pub mod convert;
pub mod decode;
pub mod diagnose;
pub mod encode;
pub mod error;
pub mod format;
pub mod graph;
pub mod io_utils;
pub mod symbols;
use error::{CliError, ExitCode};
pub async fn run() {
let cli = Cli::parse();
let log_level = match cli.verbose {
0 => Level::WARN,
1 => Level::INFO,
2 => Level::DEBUG,
_ => Level::TRACE,
};
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.with_target(false)
.with_thread_ids(false)
.with_thread_names(false)
.compact(),
)
.with(
tracing_subscriber::EnvFilter::builder()
.with_default_directive(log_level.into())
.from_env_lossy(),
)
.init();
match cli.command {
Some(Command::Lsp) | None => {
tracing::info!("Starting TOON Language Server");
let stdin = tokio::io::stdin();
let stdout = tokio::io::stdout();
let (service, socket) = LspService::new(crate::lsp::ToonLanguageServer::new);
Server::new(stdin, stdout, socket).serve(service).await;
}
Some(Command::Encode(args)) => {
handle_result(encode::execute(&args), encode::error_exit_code);
}
Some(Command::Decode(args)) => {
handle_result(decode::execute(&args), decode::error_exit_code);
}
Some(Command::Check(args)) => {
handle_result(check::execute(&args), CliError::exit_code);
}
Some(Command::Format(args)) => {
handle_result(format::execute(&args), format::error_exit_code);
}
Some(Command::Symbols(args)) => {
handle_result(symbols::execute(&args), symbols::error_exit_code);
}
Some(Command::Diagnose(args)) => {
handle_result(diagnose::execute(&args), diagnose::error_exit_code);
}
Some(Command::Graph(args)) => {
handle_result(graph::execute(&args), CliError::exit_code);
}
}
}
fn handle_result<F>(result: Result<(), CliError>, exit_code_fn: F)
where
F: FnOnce(&CliError) -> ExitCode,
{
if let Err(e) = result {
eprintln!("Error: {e}");
std::process::exit(i32::from(exit_code_fn(&e)));
}
}
#[derive(Debug, Parser)]
#[command(name = "toon-lsp")]
#[command(version, about, long_about = None)]
pub struct Cli {
#[arg(short, long, action = clap::ArgAction::Count)]
pub verbose: u8,
#[command(subcommand)]
pub command: Option<Command>,
}
#[derive(Debug, Subcommand)]
pub enum Command {
Encode(EncodeArgs),
Decode(DecodeArgs),
Check(CheckArgs),
Format(FormatArgs),
Symbols(SymbolsArgs),
Diagnose(DiagnoseArgs),
Graph(GraphArgs),
Lsp,
}
#[derive(Debug, Parser)]
pub struct EncodeArgs {
#[arg(value_name = "FILE")]
pub input: Option<PathBuf>,
#[arg(short, long, value_name = "FILE")]
pub output: Option<PathBuf>,
#[arg(short = 'f', long, value_enum, default_value = "json")]
pub input_format: InputFormat,
#[arg(short, long, default_value = "2")]
pub indent: usize,
}
#[derive(Debug, Parser)]
pub struct DecodeArgs {
#[arg(value_name = "FILE")]
pub input: Option<PathBuf>,
#[arg(short, long, value_name = "FILE")]
pub output: Option<PathBuf>,
#[arg(short = 'f', long, value_enum, default_value = "json")]
pub output_format: OutputFormat,
#[arg(short, long)]
pub pretty: bool,
}
#[derive(Debug, Parser)]
pub struct CheckArgs {
#[arg(value_name = "FILE")]
pub input: Vec<PathBuf>,
#[arg(short = 'f', long, value_enum, default_value = "text")]
pub format: DiagnosticFormat,
#[arg(short, long, value_enum, default_value = "error")]
pub severity: Severity,
}
#[derive(Debug, Parser)]
pub struct FormatArgs {
#[arg(value_name = "FILE")]
pub input: Option<PathBuf>,
#[arg(short, long, value_name = "FILE")]
pub output: Option<PathBuf>,
#[arg(short, long, default_value = "2")]
pub indent: usize,
#[arg(long)]
pub check: bool,
}
#[derive(Debug, Parser)]
pub struct SymbolsArgs {
#[arg(value_name = "FILE")]
pub input: Option<PathBuf>,
#[arg(short = 'f', long, value_enum, default_value = "tree")]
pub format: SymbolsFormat,
#[arg(short, long)]
pub types: bool,
#[arg(short, long)]
pub positions: bool,
}
#[derive(Debug, Parser)]
pub struct DiagnoseArgs {
#[arg(value_name = "FILE")]
pub input: Option<PathBuf>,
#[arg(short = 'f', long, value_enum, default_value = "json")]
pub format: DiagnoseFormat,
#[arg(short, long)]
pub context: bool,
#[arg(short, long, value_enum, default_value = "error")]
pub severity: Severity,
}
#[derive(Debug, Parser)]
pub struct GraphArgs {
#[arg(value_name = "FILE")]
pub input: Option<PathBuf>,
#[arg(short, long, value_name = "FILE")]
pub output: Option<PathBuf>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
#[non_exhaustive]
pub enum DiagnosticFormat {
Text,
Json,
Github,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
#[non_exhaustive]
pub enum SymbolsFormat {
Tree,
Json,
Flat,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
#[non_exhaustive]
pub enum DiagnoseFormat {
Json,
Sarif,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
#[non_exhaustive]
pub enum Severity {
Hint,
Info,
Warning,
Error,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
#[non_exhaustive]
pub enum InputFormat {
Json,
Yaml,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
#[non_exhaustive]
pub enum OutputFormat {
Json,
Yaml,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cli_defaults() {
let cli = Cli::parse_from(["toon-lsp"]);
assert_eq!(cli.verbose, 0);
assert!(cli.command.is_none());
}
#[test]
fn test_cli_verbose_flags() {
let cli = Cli::parse_from(["toon-lsp", "-v"]);
assert_eq!(cli.verbose, 1);
let cli = Cli::parse_from(["toon-lsp", "-vv"]);
assert_eq!(cli.verbose, 2);
let cli = Cli::parse_from(["toon-lsp", "-vvv"]);
assert_eq!(cli.verbose, 3);
}
#[test]
fn test_encode_defaults() {
let cli = Cli::parse_from(["toon-lsp", "encode"]);
if let Some(Command::Encode(args)) = cli.command {
assert_eq!(args.indent, 2);
assert_eq!(args.input_format, InputFormat::Json);
} else {
panic!("Expected Encode command");
}
}
#[test]
fn test_decode_defaults() {
let cli = Cli::parse_from(["toon-lsp", "decode"]);
if let Some(Command::Decode(args)) = cli.command {
assert_eq!(args.output_format, OutputFormat::Json);
assert!(!args.pretty);
} else {
panic!("Expected Decode command");
}
}
#[test]
fn test_check_defaults() {
let cli = Cli::parse_from(["toon-lsp", "check"]);
if let Some(Command::Check(args)) = cli.command {
assert_eq!(args.format, DiagnosticFormat::Text);
assert_eq!(args.severity, Severity::Error);
} else {
panic!("Expected Check command");
}
}
#[test]
fn test_format_defaults() {
let cli = Cli::parse_from(["toon-lsp", "format"]);
if let Some(Command::Format(args)) = cli.command {
assert_eq!(args.indent, 2);
assert!(!args.check);
} else {
panic!("Expected Format command");
}
}
#[test]
fn test_symbols_defaults() {
let cli = Cli::parse_from(["toon-lsp", "symbols"]);
if let Some(Command::Symbols(args)) = cli.command {
assert_eq!(args.format, SymbolsFormat::Tree);
assert!(!args.types);
assert!(!args.positions);
} else {
panic!("Expected Symbols command");
}
}
#[test]
fn test_diagnose_defaults() {
let cli = Cli::parse_from(["toon-lsp", "diagnose"]);
if let Some(Command::Diagnose(args)) = cli.command {
assert_eq!(args.format, DiagnoseFormat::Json);
assert!(!args.context);
assert_eq!(args.severity, Severity::Error);
} else {
panic!("Expected Diagnose command");
}
}
#[test]
fn test_graph_defaults() {
let cli = Cli::parse_from(["toon-lsp", "graph"]);
if let Some(Command::Graph(args)) = cli.command {
assert!(args.input.is_none());
assert!(args.output.is_none());
} else {
panic!("Expected Graph command");
}
}
#[test]
fn test_lsp_command() {
let cli = Cli::parse_from(["toon-lsp", "lsp"]);
assert!(matches!(cli.command, Some(Command::Lsp)));
}
#[test]
fn test_severity_ordering() {
assert!(Severity::Error > Severity::Warning);
assert!(Severity::Warning > Severity::Info);
assert!(Severity::Info > Severity::Hint);
}
}