5253402699c6340b470a8bef8772c644f11f5850
[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
10 #[derive(ThisError, Debug)]
11 pub enum Error {
12     #[error("Not an SPSS system file")]
13     NotASystemFile,
14
15     #[error("Invalid magic number {0:?}")]
16     BadMagic([u8; 4]),
17
18     #[error("I/O error ({0})")]
19     Io(#[from] IoError),
20
21     #[error("Invalid SAV compression code {0}")]
22     InvalidSavCompression(u32),
23
24     #[error("Invalid ZSAV compression code {0}")]
25     InvalidZsavCompression(u32),
26
27     #[error("Variable record at offset {offset:#x} specifies width {width} not in valid range [-1,255).")]
28     BadVariableWidth { offset: u64, width: i32 },
29
30     #[error("Document record at offset {offset:#x} has document line count ({n}) greater than the maximum number {max}.")]
31     BadDocumentLength { offset: u64, n: u32, max: u32 },
32
33     #[error("At offset {offset:#x}, unrecognized record type {rec_type}.")]
34     BadRecordType { offset: u64, rec_type: u32 },
35
36     #[error("At offset {offset:#x}, variable label code ({code}) is not 0 or 1.")]
37     BadVariableLabelCode { offset: u64, code: u32 },
38
39     #[error(
40         "At offset {offset:#x}, numeric missing value code ({code}) is not -3, -2, 0, 1, 2, or 3."
41     )]
42     BadNumericMissingValueCode { offset: u64, code: i32 },
43
44     #[error("At offset {offset:#x}, string missing value code ({code}) is not 0, 1, 2, or 3.")]
45     BadStringMissingValueCode { offset: u64, code: i32 },
46
47     #[error("At offset {offset:#x}, number of value labels ({n}) is greater than the maximum number {max}.")]
48     BadNumberOfValueLabels { offset: u64, n: u32, max: u32 },
49
50     #[error("At offset {offset:#x}, number of variables indexes ({n}) is greater than the maximum number ({max}).")]
51     BadNumberOfVarIndexes { offset: u64, n: u32, max: u32 },
52
53     #[error("At offset {offset:#x}, record type 7 subtype {subtype} is too large with element size {size} and {count} elements.")]
54     ExtensionRecordTooLarge {
55         offset: u64,
56         subtype: u32,
57         size: u32,
58         count: u32,
59     },
60
61     #[error("Unexpected end of file at offset {offset:#x}, {case_ofs} bytes into a {case_len}-byte case.")]
62     EofInCase {
63         offset: u64,
64         case_ofs: u64,
65         case_len: usize,
66     },
67
68     #[error(
69         "Unexpected end of file at offset {offset:#x}, {case_ofs} bytes into a compressed case."
70     )]
71     EofInCompressedCase { offset: u64, case_ofs: u64 },
72
73     #[error("Data ends at offset {offset:#x}, {case_ofs} bytes into a compressed case.")]
74     PartialCompressedCase { offset: u64, case_ofs: u64 },
75
76     #[error("At {case_ofs} bytes into compressed case starting at offset {offset:#x}, a string was found where a number was expected.")]
77     CompressedNumberExpected { offset: u64, case_ofs: u64 },
78
79     #[error("At {case_ofs} bytes into compressed case starting at offset {offset:#x}, a number was found where a string was expected.")]
80     CompressedStringExpected { offset: u64, case_ofs: u64 },
81
82     #[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}.")]
83     BadZlibTrailerNBlocks {
84         offset: u64,
85         n_blocks: u32,
86         expected_n_blocks: u64,
87         ztrailer_len: u64,
88     },
89
90     #[error("At offset {offset:#x}, {record} has bad size {size} bytes instead of the expected {expected_size}.")]
91     BadRecordSize { offset: u64, record: String, size: u32, expected_size: u32 },
92
93     #[error("At offset {offset:#x}, {record} has bad count {count} instead of the expected {expected_count}.")]
94     BadRecordCount { offset: u64, record: String, count: u32, expected_count: u32 },
95
96     #[error("The encoding record at offset {offset:#x} contains an encoding name that is not valid UTF-8.")]
97     BadEncodingName { offset: u64 },
98
99     #[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.")]
100     BadLongMissingValueLength { record_offset: u64, offset: u64, value_len: u32 },
101
102     #[error("This file has corrupted metadata written by a buggy version of PSPP.  To fix it, save a new copy of the file.")]
103     BadLongMissingValueFormat,
104
105     #[error("Details TBD")]
106     TBD,
107 }