work
[pspp] / rust / src / lib.rs
1 use std::io::Error as IoError;
2 use thiserror::Error as ThisError;
3
4 pub mod endian;
5 pub mod raw;
6 pub mod cooked;
7 pub mod sack;
8 pub mod encoding;
9 pub mod format;
10
11 #[derive(ThisError, Debug)]
12 pub enum Error {
13     #[error("Not an SPSS system file")]
14     NotASystemFile,
15
16     #[error("Invalid magic number {0:?}")]
17     BadMagic([u8; 4]),
18
19     #[error("I/O error ({0})")]
20     Io(#[from] IoError),
21
22     #[error("Invalid SAV compression code {0}")]
23     InvalidSavCompression(u32),
24
25     #[error("Invalid ZSAV compression code {0}")]
26     InvalidZsavCompression(u32),
27
28     #[error("Variable record at offset {offset:#x} specifies width {width} not in valid range [-1,255).")]
29     BadVariableWidth { offset: u64, width: i32 },
30
31     #[error("Document record at offset {offset:#x} has document line count ({n}) greater than the maximum number {max}.")]
32     BadDocumentLength { offset: u64, n: u32, max: u32 },
33
34     #[error("At offset {offset:#x}, unrecognized record type {rec_type}.")]
35     BadRecordType { offset: u64, rec_type: u32 },
36
37     #[error("At offset {offset:#x}, variable label code ({code}) is not 0 or 1.")]
38     BadVariableLabelCode { offset: u64, code: u32 },
39
40     #[error(
41         "At offset {offset:#x}, numeric missing value code ({code}) is not -3, -2, 0, 1, 2, or 3."
42     )]
43     BadNumericMissingValueCode { offset: u64, code: i32 },
44
45     #[error("At offset {offset:#x}, string missing value code ({code}) is not 0, 1, 2, or 3.")]
46     BadStringMissingValueCode { offset: u64, code: i32 },
47
48     #[error("At offset {offset:#x}, number of value labels ({n}) is greater than the maximum number {max}.")]
49     BadNumberOfValueLabels { offset: u64, n: u32, max: u32 },
50
51     #[error("At offset {offset:#x}, number of variables indexes ({n}) is greater than the maximum number ({max}).")]
52     BadNumberOfVarIndexes { offset: u64, n: u32, max: u32 },
53
54     #[error("At offset {offset:#x}, record type 7 subtype {subtype} is too large with element size {size} and {count} elements.")]
55     ExtensionRecordTooLarge {
56         offset: u64,
57         subtype: u32,
58         size: u32,
59         count: u32,
60     },
61
62     #[error("Unexpected end of file at offset {offset:#x}, {case_ofs} bytes into a {case_len}-byte case.")]
63     EofInCase {
64         offset: u64,
65         case_ofs: u64,
66         case_len: usize,
67     },
68
69     #[error(
70         "Unexpected end of file at offset {offset:#x}, {case_ofs} bytes into a compressed case."
71     )]
72     EofInCompressedCase { offset: u64, case_ofs: u64 },
73
74     #[error("Data ends at offset {offset:#x}, {case_ofs} bytes into a compressed case.")]
75     PartialCompressedCase { offset: u64, case_ofs: u64 },
76
77     #[error("At {case_ofs} bytes into compressed case starting at offset {offset:#x}, a string was found where a number was expected.")]
78     CompressedNumberExpected { offset: u64, case_ofs: u64 },
79
80     #[error("At {case_ofs} bytes into compressed case starting at offset {offset:#x}, a number was found where a string was expected.")]
81     CompressedStringExpected { offset: u64, case_ofs: u64 },
82
83     #[error("Block count {n_blocks} in ZLIB trailer at offset {offset:#x} differs from expected block count {expected_n_blocks} calculated from trailer length {ztrailer_len}.")]
84     BadZlibTrailerNBlocks {
85         offset: u64,
86         n_blocks: u32,
87         expected_n_blocks: u64,
88         ztrailer_len: u64,
89     },
90
91     #[error("At offset {offset:#x}, {record} has bad size {size} bytes instead of the expected {expected_size}.")]
92     BadRecordSize { offset: u64, record: String, size: u32, expected_size: u32 },
93
94     #[error("At offset {offset:#x}, {record} has bad count {count} instead of the expected {expected_count}.")]
95     BadRecordCount { offset: u64, record: String, count: u32, expected_count: u32 },
96
97     #[error("The encoding record at offset {offset:#x} contains an encoding name that is not valid UTF-8.")]
98     BadEncodingName { offset: u64 },
99
100     #[error("In long string missing values record starting at offset {record_offset:#x}, value length at offset {offset:#x} is {value_len} instead of the expected 8.")]
101     BadLongMissingValueLength { record_offset: u64, offset: u64, value_len: u32 },
102
103     #[error("This file has corrupted metadata written by a buggy version of PSPP.  To ensure that other software can read it correctly, save a new copy of the file.")]
104     BadLongMissingValueFormat,
105
106     #[error("File creation date {creation_date} is not in the expected format \"DD MMM YY\" format.  Using 01 Jan 1970.")]
107     InvalidCreationDate { creation_date: String },
108
109     #[error("File creation time {creation_time} is not in the expected format \"HH:MM:SS\" format.  Using midnight.")]
110     InvalidCreationTime { creation_time: String },
111
112     #[error("Details TBD")]
113     TBD,
114 }
115
116 #[derive(Copy, Clone, Debug)]
117 pub enum Compression {
118     Simple,
119     ZLib,
120 }
121
122 #[derive(Clone, Debug)]
123 pub enum CategoryLabels {
124     VarLabels,
125     CountedValues,
126 }