319d7aea7c0f32a020db403a21fe60fad96cac67
[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;
19 use pspp::{
20     raw::{Reader, Record},
21 };
22 use std::fs::File;
23 use std::io::BufReader;
24 use std::path::{Path, PathBuf};
25 use std::str;
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
40 fn main() -> Result<()> {
41     let Args { max_cases, files } = Args::parse();
42
43     for file in files {
44         dissect(&file, max_cases)?;
45     }
46     Ok(())
47 }
48
49 fn dissect(file_name: &Path, max_cases: u64) -> Result<()> {
50     let reader = File::open(file_name)?;
51     let reader = BufReader::new(reader);
52     let mut reader = Reader::new(reader)?;
53     let records: Vec<Record> = reader.collect_headers()?;
54
55     let mut n_cases = 0;
56     for record in records {
57         println!("{record:?}");
58         match record {
59             Record::EndOfHeaders(_) if max_cases == 0 => break,
60             Record::Case(_) => {
61                 n_cases += 1;
62                 if n_cases >= max_cases {
63                     break;
64                 }
65             }
66             _ => (),
67         }
68     }
69     Ok(())
70 }