Skip to main content

excel_cli/cli/
dispatch.rs

1use serde_json::Value;
2
3use crate::cli::args::{Cli, Commands, OutputFormat};
4use crate::cli::error::{AppError, EXIT_SUCCESS};
5
6pub fn dispatch(cli: Cli) -> Result<(Value, OutputFormat, i32), AppError> {
7    match cli.command {
8        Commands::Inspect { subcommand } => {
9            let format = match &subcommand {
10                crate::cli::args::InspectCommands::Workbook { format, .. } => format.clone(),
11                crate::cli::args::InspectCommands::Sheet { format, .. } => format.clone(),
12                crate::cli::args::InspectCommands::Sample { format, .. } => format.clone(),
13                crate::cli::args::InspectCommands::Columns { format, .. } => format.clone(),
14                crate::cli::args::InspectCommands::Tables { format, .. } => format.clone(),
15            };
16            let value = crate::cli::inspect::handle(subcommand)?;
17            Ok((value, format, EXIT_SUCCESS))
18        }
19        Commands::Read { subcommand } => {
20            let format = match &subcommand {
21                crate::cli::args::ReadCommands::Cell { format, .. } => format.clone(),
22                crate::cli::args::ReadCommands::Range { format, .. } => format.clone(),
23                crate::cli::args::ReadCommands::Rows { format, .. } => format.clone(),
24                crate::cli::args::ReadCommands::Records { format, .. } => format.clone(),
25            };
26            let value = crate::cli::read::handle(subcommand)?;
27            Ok((value, format, EXIT_SUCCESS))
28        }
29        Commands::Check {
30            file,
31            sheet,
32            rules,
33            severity_threshold,
34        } => {
35            let (value, exit_code) =
36                crate::cli::check::handle(file, sheet, rules, severity_threshold)?;
37            Ok((value, OutputFormat::Json, exit_code))
38        }
39        Commands::Ui { file } => {
40            let workbook = crate::excel::open_workbook(&file, false)
41                .map_err(crate::cli::error::anyhow_to_app_error)?;
42            let app_state = crate::app::AppState::new(workbook, file)
43                .map_err(crate::cli::error::anyhow_to_app_error)?;
44            crate::ui::run_app(app_state).map_err(crate::cli::error::anyhow_to_app_error)?;
45            Ok((
46                crate::cli::envelope::success_envelope(
47                    "ui",
48                    "",
49                    "",
50                    crate::cli::envelope::target_workbook(),
51                    serde_json::json!({}),
52                    serde_json::json!({"status": "interactive"}),
53                    vec![],
54                ),
55                OutputFormat::Json,
56                EXIT_SUCCESS,
57            ))
58        }
59    }
60}