From 46c91efbf468dbff0bedfc4787f1745be2fd5769 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 13 Nov 2025 08:59:36 -0800 Subject: [PATCH] rust: Move CLI implementation into `cli` module. --- rust/pspp/src/cli.rs | 80 +++++++++++++++++++++++++++++ rust/pspp/src/{ => cli}/convert.rs | 2 +- rust/pspp/src/{ => cli}/decrypt.rs | 0 rust/pspp/src/{ => cli}/identify.rs | 0 rust/pspp/src/{ => cli}/show.rs | 2 +- rust/pspp/src/{ => cli}/show_pc.rs | 0 rust/pspp/src/{ => cli}/show_por.rs | 0 rust/pspp/src/{ => cli}/show_spv.rs | 0 rust/pspp/src/main.rs | 64 ++--------------------- 9 files changed, 86 insertions(+), 62 deletions(-) create mode 100644 rust/pspp/src/cli.rs rename rust/pspp/src/{ => cli}/convert.rs (99%) rename rust/pspp/src/{ => cli}/decrypt.rs (100%) rename rust/pspp/src/{ => cli}/identify.rs (100%) rename rust/pspp/src/{ => cli}/show.rs (99%) rename rust/pspp/src/{ => cli}/show_pc.rs (100%) rename rust/pspp/src/{ => cli}/show_por.rs (100%) rename rust/pspp/src/{ => cli}/show_spv.rs (100%) diff --git a/rust/pspp/src/cli.rs b/rust/pspp/src/cli.rs new file mode 100644 index 0000000000..3a5868608f --- /dev/null +++ b/rust/pspp/src/cli.rs @@ -0,0 +1,80 @@ +// PSPP - a program for statistical analysis. +// Copyright (C) 2025 Free Software Foundation, Inc. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use anyhow::Result; +use clap::{Parser, Subcommand}; +use encoding_rs::Encoding; +use thiserror::Error as ThisError; + +use convert::Convert; +use decrypt::Decrypt; +use identify::Identify; +use show::Show; +use show_pc::ShowPc; +use show_por::ShowPor; +use show_spv::ShowSpv; + +mod convert; +mod decrypt; +mod identify; +mod show; +mod show_pc; +mod show_por; +mod show_spv; + +/// PSPP, a program for statistical analysis of sampled data. +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +pub struct Cli { + #[command(subcommand)] + pub command: Command, +} + +#[derive(Subcommand, Clone, Debug)] +pub enum Command { + Convert(Convert), + Decrypt(Decrypt), + Identify(Identify), + Show(Show), + ShowPor(ShowPor), + ShowPc(ShowPc), + ShowSpv(ShowSpv), +} + +impl Command { + pub fn run(self) -> Result<()> { + match self { + Command::Convert(convert) => convert.run(), + Command::Decrypt(decrypt) => decrypt.run(), + Command::Identify(identify) => identify.run(), + Command::Show(show) => show.run(), + Command::ShowPor(show_por) => show_por.run(), + Command::ShowPc(show_pc) => show_pc.run(), + Command::ShowSpv(show_spv) => show_spv.run(), + } + } +} + +#[derive(ThisError, Debug)] +#[error("{0}: unknown encoding")] +struct UnknownEncodingError(String); + +fn parse_encoding(arg: &str) -> Result<&'static Encoding, UnknownEncodingError> { + match Encoding::for_label_no_replacement(arg.as_bytes()) { + Some(encoding) => Ok(encoding), + None => Err(UnknownEncodingError(arg.to_string())), + } +} diff --git a/rust/pspp/src/convert.rs b/rust/pspp/src/cli/convert.rs similarity index 99% rename from rust/pspp/src/convert.rs rename to rust/pspp/src/cli/convert.rs index 249df65b7a..af206c0639 100644 --- a/rust/pspp/src/convert.rs +++ b/rust/pspp/src/cli/convert.rs @@ -37,7 +37,7 @@ use pspp::{ variable::Variable, }; -use crate::parse_encoding; +use super::parse_encoding; /// Convert SPSS data files into other formats. #[derive(Args, Clone, Debug)] diff --git a/rust/pspp/src/decrypt.rs b/rust/pspp/src/cli/decrypt.rs similarity index 100% rename from rust/pspp/src/decrypt.rs rename to rust/pspp/src/cli/decrypt.rs diff --git a/rust/pspp/src/identify.rs b/rust/pspp/src/cli/identify.rs similarity index 100% rename from rust/pspp/src/identify.rs rename to rust/pspp/src/cli/identify.rs diff --git a/rust/pspp/src/show.rs b/rust/pspp/src/cli/show.rs similarity index 99% rename from rust/pspp/src/show.rs rename to rust/pspp/src/cli/show.rs index c83cddde78..3d1bfe737f 100644 --- a/rust/pspp/src/show.rs +++ b/rust/pspp/src/cli/show.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License along with // this program. If not, see . -use crate::parse_encoding; +use super::parse_encoding; use anyhow::{Result, anyhow}; use clap::{Args, ValueEnum}; use encoding_rs::Encoding; diff --git a/rust/pspp/src/show_pc.rs b/rust/pspp/src/cli/show_pc.rs similarity index 100% rename from rust/pspp/src/show_pc.rs rename to rust/pspp/src/cli/show_pc.rs diff --git a/rust/pspp/src/show_por.rs b/rust/pspp/src/cli/show_por.rs similarity index 100% rename from rust/pspp/src/show_por.rs rename to rust/pspp/src/cli/show_por.rs diff --git a/rust/pspp/src/show_spv.rs b/rust/pspp/src/cli/show_spv.rs similarity index 100% rename from rust/pspp/src/show_spv.rs rename to rust/pspp/src/cli/show_spv.rs diff --git a/rust/pspp/src/main.rs b/rust/pspp/src/main.rs index df4d3715b3..a1f72129d9 100644 --- a/rust/pspp/src/main.rs +++ b/rust/pspp/src/main.rs @@ -14,68 +14,12 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use anyhow::Result; -use clap::{Parser, Subcommand}; -use encoding_rs::Encoding; -use thiserror::Error as ThisError; +use clap::Parser; -use crate::{ - convert::Convert, decrypt::Decrypt, identify::Identify, show::Show, show_pc::ShowPc, - show_por::ShowPor, show_spv::ShowSpv, -}; +use crate::cli::Cli; -mod convert; -mod decrypt; -mod identify; -mod show; -mod show_pc; -mod show_por; -mod show_spv; +mod cli; -/// PSPP, a program for statistical analysis of sampled data. -#[derive(Parser, Debug)] -#[command(author, version, about, long_about = None)] -struct Cli { - #[command(subcommand)] - command: Command, -} - -#[derive(Subcommand, Clone, Debug)] -enum Command { - Convert(Convert), - Decrypt(Decrypt), - Identify(Identify), - Show(Show), - ShowPor(ShowPor), - ShowPc(ShowPc), - ShowSpv(ShowSpv), -} - -impl Command { - fn run(self) -> Result<()> { - match self { - Command::Convert(convert) => convert.run(), - Command::Decrypt(decrypt) => decrypt.run(), - Command::Identify(identify) => identify.run(), - Command::Show(show) => show.run(), - Command::ShowPor(show_por) => show_por.run(), - Command::ShowPc(show_pc) => show_pc.run(), - Command::ShowSpv(show_spv) => show_spv.run(), - } - } -} - -#[derive(ThisError, Debug)] -#[error("{0}: unknown encoding")] -struct UnknownEncodingError(String); - -fn parse_encoding(arg: &str) -> Result<&'static Encoding, UnknownEncodingError> { - match Encoding::for_label_no_replacement(arg.as_bytes()) { - Some(encoding) => Ok(encoding), - None => Err(UnknownEncodingError(arg.to_string())), - } -} - -fn main() -> Result<()> { +fn main() -> anyhow::Result<()> { Cli::parse().command.run() } -- 2.30.2