progress! and clippy!
[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::raw::{encoding_from_headers, Decoder, Magic, Reader, Record};
21 use std::fs::File;
22 use std::io::BufReader;
23 use std::path::{Path, PathBuf};
24 use std::str;
25 use thiserror::Error as ThisError;
26
27 /// A utility to dissect SPSS system files.
28 #[derive(Parser, Debug)]
29 #[command(author, version, about, long_about = None)]
30 struct Args {
31     /// Maximum number of cases to print.
32     #[arg(long = "data", default_value_t = 0)]
33     max_cases: u64,
34
35     /// Files to dissect.
36     #[arg(required = true)]
37     files: Vec<PathBuf>,
38
39     /// How to dissect the file.
40     #[arg(short, long, value_enum, default_value_t)]
41     mode: Mode,
42
43     /// The encoding to use.
44     #[arg(long, value_parser = parse_encoding)]
45     encoding: Option<&'static Encoding>,
46 }
47
48 #[derive(ThisError, Debug)]
49 #[error("{0}: unknown encoding")]
50 struct UnknownEncodingError(String);
51
52 fn parse_encoding(arg: &str) -> Result<&'static Encoding, UnknownEncodingError> {
53     match Encoding::for_label_no_replacement(arg.as_bytes()) {
54         Some(encoding) => Ok(encoding),
55         None => Err(UnknownEncodingError(arg.to_string())),
56     }
57 }
58
59 #[derive(Clone, Copy, Debug, Default, ValueEnum)]
60 enum Mode {
61     Identify,
62     Raw,
63     Decoded,
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(
83     file_name: &Path,
84     max_cases: u64,
85     mode: Mode,
86     encoding: Option<&'static Encoding>,
87 ) -> Result<()> {
88     let reader = File::open(file_name)?;
89     let reader = BufReader::new(reader);
90     let mut reader = Reader::new(reader, |warning| println!("{warning}"))?;
91
92     match mode {
93         Mode::Identify => {
94             let Record::Header(header) = reader.next().unwrap()? else {
95                 unreachable!()
96             };
97             match header.magic {
98                 Magic::Sav => println!("SPSS System File"),
99                 Magic::Zsav => println!("SPSS System File with Zlib compression"),
100                 Magic::Ebcdic => println!("EBCDIC-encoded SPSS System File"),
101             }
102             return Ok(());
103         }
104         Mode::Raw => {
105             for header in reader {
106                 let header = header?;
107                 println!("{:?}", header);
108                 if let Record::Cases(cases) = header {
109                     let mut cases = cases.borrow_mut();
110                     for _ in 0..max_cases {
111                         let Some(Ok(record)) = cases.next() else {
112                             break;
113                         };
114                         println!("{:?}", record);
115                     }
116                 }
117             }
118         }
119         Mode::Decoded => {
120             let headers: Vec<Record> = reader.collect::<Result<Vec<_>, _>>()?;
121             let encoding = match encoding {
122                 Some(encoding) => encoding,
123                 None => encoding_from_headers(&headers, &|e| eprintln!("{e}"))?,
124             };
125             let decoder = Decoder::new(encoding, |e| eprintln!("{e}"));
126             for header in headers {
127                 let header = header.decode(&decoder);
128                 println!("{:?}", header);
129                 /*
130                                 if let Record::Cases(cases) = header {
131                                     let mut cases = cases.borrow_mut();
132                                     for _ in 0..max_cases {
133                                         let Some(Ok(record)) = cases.next() else {
134                                             break;
135                                         };
136                                         println!("{:?}", record);
137                                     }
138                                 }
139                 */
140             }
141         }
142         Mode::Cooked => {
143             /*
144                 let headers: Vec<Record> = reader.collect::<Result<Vec<_>, _>>()?;
145                 let encoding = encoding_from_headers(&headers, &|e| eprintln!("{e}"))?;
146                 let (headers, _) = decode(headers, encoding, &|e| eprintln!("{e}"))?;
147                 for header in headers {
148                     println!("{header:?}");
149             }
150                 */
151         }
152     }
153
154     Ok(())
155 }