6024b67f2dea68c9395e1b20436d94a7fedc0ac4
[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::raw::{Reader, Record};
20 use std::fs::File;
21 use std::io::BufReader;
22 use std::path::{Path, PathBuf};
23 use std::str;
24
25 /// A utility to dissect SPSS system files.
26 #[derive(Parser, Debug)]
27 #[command(author, version, about, long_about = None)]
28 struct Args {
29     /// Maximum number of cases to print.
30     #[arg(long = "data", default_value_t = 0)]
31     max_cases: u64,
32
33     /// Files to dissect.
34     #[arg(required = true)]
35     files: Vec<PathBuf>,
36 }
37
38 fn main() -> Result<()> {
39     let Args { max_cases, files } = Args::parse();
40
41     for file in files {
42         dissect(&file, max_cases)?;
43     }
44     Ok(())
45 }
46
47 fn dissect(file_name: &Path, max_cases: u64) -> Result<()> {
48     let reader = File::open(file_name)?;
49     let reader = BufReader::new(reader);
50     let mut reader = Reader::new(reader)?;
51     let records: Vec<Record> = reader.collect_headers()?;
52
53     for record in records {
54         println!("{record:?}");
55         if let Record::EndOfHeaders(_) = record {
56             break;
57         };
58     }
59
60     for _ in 0..max_cases {
61         let Some(Ok(Record::Case(data))) = reader.next() else {
62             break;
63         };
64         println!("{:?}", data);
65     }
66     Ok(())
67 }