cleanup
[pspp] / rust / src / main.rs
1 /* PSPP - a program for statistical analysis.
2  * Copyright (C) 2023 Free Software Foundation, Inc.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 use anyhow::Result;
18 use clap::{Parser, ValueEnum};
19 use encoding_rs::Encoding;
20 use pspp::cooked::decode;
21 use pspp::raw::{Reader, Record, Magic};
22 use std::fs::File;
23 use std::io::BufReader;
24 use std::path::{Path, PathBuf};
25 use std::str;
26 use thiserror::Error as ThisError;
27
28 /// A utility to dissect SPSS system files.
29 #[derive(Parser, Debug)]
30 #[command(author, version, about, long_about = None)]
31 struct Args {
32     /// Maximum number of cases to print.
33     #[arg(long = "data", default_value_t = 0)]
34     max_cases: u64,
35
36     /// Files to dissect.
37     #[arg(required = true)]
38     files: Vec<PathBuf>,
39
40     /// How to dissect the file.
41     #[arg(short, long, value_enum, default_value_t)]
42     mode: Mode,
43
44     /// The encoding to use.
45     #[arg(long, value_parser = parse_encoding)]
46     encoding: Option<&'static Encoding>,
47 }
48
49 #[derive(ThisError, Debug)]
50 #[error("{0}: unknown encoding")]
51 struct UnknownEncodingError(String);
52
53 fn parse_encoding(arg: &str) -> Result<&'static Encoding, UnknownEncodingError> {
54     match Encoding::for_label_no_replacement(arg.as_bytes()) {
55         Some(encoding) => Ok(encoding),
56         None => Err(UnknownEncodingError(arg.to_string())),
57     }
58 }
59
60 #[derive(Clone, Copy, Debug, Default, ValueEnum)]
61 enum Mode {
62     Identify,
63     Raw,
64     #[default]
65     Cooked,
66 }
67
68 fn main() -> Result<()> {
69     let Args {
70         max_cases,
71         files,
72         mode,
73         encoding,
74     } = Args::parse();
75
76     for file in files {
77         dissect(&file, max_cases, mode, encoding)?;
78     }
79     Ok(())
80 }
81
82 fn dissect(file_name: &Path, max_cases: u64, mode: Mode, encoding: Option<&'static Encoding>) -> Result<()> {
83     let reader = File::open(file_name)?;
84     let reader = BufReader::new(reader);
85     let mut reader = Reader::new(reader)?;
86
87     match mode {
88         Mode::Identify => {
89             let Record::Header(header) = reader.next().unwrap()? else { unreachable!() };
90             match header.magic {
91                 Magic::Sav => println!("SPSS System File"),
92                 Magic::Zsav => println!("SPSS System File with Zlib compression"),
93                 Magic::Ebcdic => println!("EBCDIC-encoded SPSS System File"),
94             }
95             return Ok(())
96         }
97         Mode::Raw => {
98             let headers: Vec<Record> = reader.collect_headers()?;
99             for header in headers {
100                 println!("{header:?}");
101             }
102         }
103         Mode::Cooked => {
104             let headers: Vec<Record> = reader.collect_headers()?;
105             let headers = decode(headers, encoding, &|e| panic!("{e}"))?;
106             for header in headers {
107                 println!("{header:?}");
108             }
109         }
110     }
111
112     for _ in 0..max_cases {
113         let Some(Ok(record)) = reader.next() else {
114             break;
115         };
116         println!("{:?}", record);
117     }
118     Ok(())
119 }