3 encoding::{default_encoding, get_encoding, Error as EncodingError},
4 endian::{Endian, Parse, ToBytes},
5 identifier::{Error as IdError, Identifier},
8 use encoding_rs::{mem::decode_latin1, DecoderResult, Encoding};
9 use flate2::read::ZlibDecoder;
15 collections::{HashMap, VecDeque},
16 fmt::{Debug, Display, Formatter, Result as FmtResult},
17 io::{Error as IoError, Read, Seek, SeekFrom},
24 use thiserror::Error as ThisError;
26 #[derive(ThisError, Debug)]
28 #[error("Not an SPSS system file")]
31 #[error("Invalid magic number {0:?}")]
34 #[error("I/O error ({0})")]
37 #[error("Invalid SAV compression code {0}")]
38 InvalidSavCompression(u32),
40 #[error("Invalid ZSAV compression code {0}")]
41 InvalidZsavCompression(u32),
43 #[error("Document record at offset {offset:#x} has document line count ({n}) greater than the maximum number {max}.")]
44 BadDocumentLength { offset: u64, n: usize, max: usize },
46 #[error("At offset {offset:#x}, unrecognized record type {rec_type}.")]
47 BadRecordType { offset: u64, rec_type: u32 },
49 #[error("In variable record starting at offset {start_offset:#x}, variable label code {code} at offset {code_offset:#x} is not 0 or 1.")]
50 BadVariableLabelCode {
57 "At offset {offset:#x}, numeric missing value code ({code}) is not -3, -2, 0, 1, 2, or 3."
59 BadNumericMissingValueCode { offset: u64, code: i32 },
61 #[error("At offset {offset:#x}, string missing value code ({code}) is not 0, 1, 2, or 3.")]
62 BadStringMissingValueCode { offset: u64, code: i32 },
64 #[error("At offset {offset:#x}, number of value labels ({n}) is greater than the maximum number {max}.")]
65 BadNumberOfValueLabels { offset: u64, n: u32, max: u32 },
67 #[error("At offset {offset:#x}, following value label record, found record type {rec_type} instead of expected type 4 for variable index record")]
68 ExpectedVarIndexRecord { offset: u64, rec_type: u32 },
70 #[error("At offset {offset:#x}, number of variables indexes for value labels ({n}) is greater than the maximum number ({max}).")]
71 TooManyVarIndexes { offset: u64, n: u32, max: u32 },
73 #[error("At offset {offset:#x}, record type 7 subtype {subtype} is too large with element size {size} and {count} elements.")]
74 ExtensionRecordTooLarge {
81 #[error("Unexpected end of file at offset {offset:#x}, {case_ofs} bytes into a {case_len}-byte case.")]
89 "Unexpected end of file at offset {offset:#x}, {case_ofs} bytes into a compressed case."
91 EofInCompressedCase { offset: u64, case_ofs: u64 },
93 #[error("Data ends at offset {offset:#x}, {case_ofs} bytes into a compressed case.")]
94 PartialCompressedCase { offset: u64, case_ofs: u64 },
96 #[error("At {case_ofs} bytes into compressed case starting at offset {offset:#x}, a string was found where a number was expected.")]
97 CompressedNumberExpected { offset: u64, case_ofs: u64 },
99 #[error("At {case_ofs} bytes into compressed case starting at offset {offset:#x}, a number was found where a string was expected.")]
100 CompressedStringExpected { offset: u64, case_ofs: u64 },
102 #[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}.")]
103 BadZlibTrailerNBlocks {
106 expected_n_blocks: u64,
111 EncodingError(EncodingError),
114 #[derive(ThisError, Debug)]
116 #[error("Unexpected end of data inside extension record.")]
119 #[error("At offset {offset:#x}, at least one valid variable index for value labels is required but none were specified.")]
120 NoVarIndexes { offset: u64 },
122 #[error("At offset {offset:#x}, the first variable index is for a {var_type} variable but the following variable indexes are for {} variables: {wrong_types:?}", var_type.opposite())]
126 wrong_types: Vec<u32>,
129 #[error("At offset {offset:#x}, one or more variable indexes for value labels were not in the valid range [1,{max}]: {invalid:?}")]
136 #[error("At offset {offset:#x}, {record} has bad size {size} bytes instead of the expected {expected_size}.")]
144 #[error("At offset {offset:#x}, {record} has bad count {count} instead of the expected {expected_count}.")]
152 #[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.")]
153 BadLongMissingValueLength {
159 #[error("The encoding record at offset {offset:#x} contains an encoding name that is not valid UTF-8.")]
160 BadEncodingName { offset: u64 },
162 // XXX This is risky because `text` might be arbitarily long.
163 #[error("Text string contains invalid bytes for {encoding} encoding: {text}")]
164 MalformedString { encoding: String, text: String },
166 #[error("Invalid variable measurement level value {0}")]
167 InvalidMeasurement(u32),
169 #[error("Invalid variable display alignment value {0}")]
170 InvalidAlignment(u32),
172 #[error("Invalid attribute name. {0}")]
173 InvalidAttributeName(IdError),
175 #[error("Invalid variable name in attribute record. {0}")]
176 InvalidAttributeVariableName(IdError),
178 #[error("Invalid short name in long variable name record. {0}")]
179 InvalidShortName(IdError),
181 #[error("Invalid name in long variable name record. {0}")]
182 InvalidLongName(IdError),
184 #[error("Invalid variable name in very long string record. {0}")]
185 InvalidLongStringName(IdError),
187 #[error("Invalid variable name in variable set record. {0}")]
188 InvalidVariableSetName(IdError),
190 #[error("Invalid multiple response set name. {0}")]
191 InvalidMrSetName(IdError),
193 #[error("Invalid multiple response set variable name. {0}")]
194 InvalidMrSetVariableName(IdError),
196 #[error("Invalid variable name in long string missing values record. {0}")]
197 InvalidLongStringMissingValueVariableName(IdError),
199 #[error("Invalid variable name in long string value label record. {0}")]
200 InvalidLongStringValueLabelName(IdError),
203 EncodingError(EncodingError),
205 #[error("Details TBD")]
209 impl From<IoError> for Warning {
210 fn from(_source: IoError) -> Self {
211 Self::UnexpectedEndOfData
215 #[derive(Clone, Debug)]
217 Header(HeaderRecord<RawString>),
218 Variable(VariableRecord<RawString, RawStr<8>>),
219 ValueLabel(ValueLabelRecord<RawStr<8>, RawString>),
220 Document(DocumentRecord<RawDocumentLine>),
221 IntegerInfo(IntegerInfoRecord),
222 FloatInfo(FloatInfoRecord),
223 VarDisplay(VarDisplayRecord),
224 MultipleResponse(MultipleResponseRecord<RawString, RawString>),
225 LongStringValueLabels(LongStringValueLabelRecord<RawString, RawString>),
226 LongStringMissingValues(LongStringMissingValueRecord<RawString, RawStr<8>>),
227 Encoding(EncodingRecord),
228 NumberOfCases(NumberOfCasesRecord),
230 OtherExtension(Extension),
234 Cases(Rc<RefCell<Cases>>),
237 #[derive(Clone, Debug)]
238 pub enum DecodedRecord {
239 Header(HeaderRecord<String>),
240 Variable(VariableRecord<String, String>),
241 ValueLabel(ValueLabelRecord<RawStr<8>, String>),
242 Document(DocumentRecord<String>),
243 IntegerInfo(IntegerInfoRecord),
244 FloatInfo(FloatInfoRecord),
245 VarDisplay(VarDisplayRecord),
246 MultipleResponse(MultipleResponseRecord<Identifier, String>),
247 LongStringValueLabels(LongStringValueLabelRecord<Identifier, String>),
248 LongStringMissingValues(LongStringMissingValueRecord<Identifier, String>),
249 Encoding(EncodingRecord),
250 NumberOfCases(NumberOfCasesRecord),
251 VariableSets(VariableSetRecord),
252 ProductInfo(ProductInfoRecord),
253 LongNames(LongNamesRecord),
254 VeryLongStrings(VeryLongStringsRecord),
255 FileAttributes(FileAttributeRecord),
256 VariableAttributes(VariableAttributeRecord),
257 OtherExtension(Extension),
261 Cases(Rc<RefCell<Cases>>),
268 var_types: &[VarType],
269 warn: &dyn Fn(Warning),
270 ) -> Result<Option<Record>, Error>
274 let rec_type: u32 = endian.parse(read_bytes(reader)?);
276 2 => Ok(Some(VariableRecord::read(reader, endian)?)),
277 3 => Ok(ValueLabelRecord::read(reader, endian, var_types, warn)?),
278 6 => Ok(Some(DocumentRecord::read(reader, endian)?)),
279 7 => Extension::read(reader, endian, var_types.len(), warn),
280 999 => Ok(Some(Record::EndOfHeaders(
281 endian.parse(read_bytes(reader)?),
283 _ => Err(Error::BadRecordType {
284 offset: reader.stream_position()?,
290 pub fn decode(self, decoder: &Decoder) -> Result<DecodedRecord, Error> {
292 Record::Header(record) => record.decode(decoder),
293 Record::Variable(record) => record.decode(decoder),
294 Record::ValueLabel(record) => DecodedRecord::ValueLabel(record.decode(decoder)),
295 Record::Document(record) => record.decode(decoder),
296 Record::IntegerInfo(record) => DecodedRecord::IntegerInfo(record.clone()),
297 Record::FloatInfo(record) => DecodedRecord::FloatInfo(record.clone()),
298 Record::VarDisplay(record) => DecodedRecord::VarDisplay(record.clone()),
299 Record::MultipleResponse(record) => record.decode(decoder),
300 Record::LongStringValueLabels(record) => {
301 DecodedRecord::LongStringValueLabels(record.decode(decoder))
303 Record::LongStringMissingValues(record) => {
304 DecodedRecord::LongStringMissingValues(record.decode(decoder))
306 Record::Encoding(record) => DecodedRecord::Encoding(record.clone()),
307 Record::NumberOfCases(record) => DecodedRecord::NumberOfCases(record.clone()),
308 Record::Text(record) => record.decode(decoder),
309 Record::OtherExtension(record) => DecodedRecord::OtherExtension(record.clone()),
310 Record::EndOfHeaders(record) => DecodedRecord::EndOfHeaders(record),
311 Record::ZHeader(record) => DecodedRecord::ZHeader(record.clone()),
312 Record::ZTrailer(record) => DecodedRecord::ZTrailer(record.clone()),
313 Record::Cases(record) => DecodedRecord::Cases(record.clone()),
318 pub fn encoding_from_headers(
319 headers: &Vec<Record>,
320 warn: &impl Fn(Warning),
321 ) -> Result<&'static Encoding, Error> {
322 let mut encoding_record = None;
323 let mut integer_info_record = None;
324 for record in headers {
326 Record::Encoding(record) => encoding_record = Some(record),
327 Record::IntegerInfo(record) => integer_info_record = Some(record),
331 let encoding = encoding_record.map(|record| record.0.as_str());
332 let character_code = integer_info_record.map(|record| record.character_code);
333 match get_encoding(encoding, character_code) {
334 Ok(encoding) => Ok(encoding),
335 Err(err @ EncodingError::Ebcdic) => Err(Error::EncodingError(err)),
337 warn(Warning::EncodingError(err));
338 // Warn that we're using the default encoding.
339 Ok(default_encoding())
344 // If `s` is valid UTF-8, returns it decoded as UTF-8, otherwise returns it
345 // decoded as Latin-1 (actually bytes interpreted as Unicode code points).
346 fn default_decode(s: &[u8]) -> Cow<str> {
347 from_utf8(s).map_or_else(|_| decode_latin1(s), Cow::from)
350 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
351 pub enum Compression {
357 fn offsets(&self) -> Range<u64>;
361 pub struct HeaderRecord<S>
366 pub offsets: Range<u64>,
371 /// Eye-catcher string, product name, in the file's encoding. Padded
372 /// on the right with spaces.
375 /// Layout code, normally either 2 or 3.
376 pub layout_code: u32,
378 /// Number of variable positions, or `None` if the value in the file is
379 /// questionably trustworthy.
380 pub nominal_case_size: Option<u32>,
382 /// Compression type, if any,
383 pub compression: Option<Compression>,
385 /// 1-based variable index of the weight variable, or `None` if the file is
387 pub weight_index: Option<u32>,
389 /// Claimed number of cases, if known.
390 pub n_cases: Option<u32>,
392 /// Compression bias, usually 100.0.
395 /// `dd mmm yy` in the file's encoding.
396 pub creation_date: S,
398 /// `HH:MM:SS` in the file's encoding.
399 pub creation_time: S,
401 /// File label, in the file's encoding. Padded on the right with spaces.
404 /// Endianness of the data in the file header.
408 impl<S> HeaderRecord<S>
412 fn debug_field<T>(&self, f: &mut Formatter, name: &str, value: T) -> FmtResult
416 writeln!(f, "{name:>17}: {:?}", value)
420 impl<S> Debug for HeaderRecord<S>
424 fn fmt(&self, f: &mut Formatter) -> FmtResult {
425 writeln!(f, "File header record:")?;
426 self.debug_field(f, "Magic", self.magic)?;
427 self.debug_field(f, "Product name", &self.eye_catcher)?;
428 self.debug_field(f, "Layout code", self.layout_code)?;
429 self.debug_field(f, "Nominal case size", self.nominal_case_size)?;
430 self.debug_field(f, "Compression", self.compression)?;
431 self.debug_field(f, "Weight index", self.weight_index)?;
432 self.debug_field(f, "Number of cases", self.n_cases)?;
433 self.debug_field(f, "Compression bias", self.bias)?;
434 self.debug_field(f, "Creation date", &self.creation_date)?;
435 self.debug_field(f, "Creation time", &self.creation_time)?;
436 self.debug_field(f, "File label", &self.file_label)?;
437 self.debug_field(f, "Endianness", self.endian)
441 impl HeaderRecord<RawString> {
442 fn read<R: Read + Seek>(r: &mut R) -> Result<Self, Error> {
443 let start = r.stream_position()?;
445 let magic: [u8; 4] = read_bytes(r)?;
446 let magic: Magic = magic.try_into().map_err(|_| Error::NotASystemFile)?;
448 let eye_catcher = RawString(read_vec(r, 60)?);
449 let layout_code: [u8; 4] = read_bytes(r)?;
450 let endian = Endian::identify_u32(2, layout_code)
451 .or_else(|| Endian::identify_u32(2, layout_code))
452 .ok_or_else(|| Error::NotASystemFile)?;
453 let layout_code = endian.parse(layout_code);
455 let nominal_case_size: u32 = endian.parse(read_bytes(r)?);
456 let nominal_case_size =
457 (nominal_case_size <= i32::MAX as u32 / 16).then_some(nominal_case_size);
459 let compression_code: u32 = endian.parse(read_bytes(r)?);
460 let compression = match (magic, compression_code) {
461 (Magic::Zsav, 2) => Some(Compression::ZLib),
462 (Magic::Zsav, code) => return Err(Error::InvalidZsavCompression(code)),
464 (_, 1) => Some(Compression::Simple),
465 (_, code) => return Err(Error::InvalidSavCompression(code)),
468 let weight_index: u32 = endian.parse(read_bytes(r)?);
469 let weight_index = (weight_index > 0).then_some(weight_index);
471 let n_cases: u32 = endian.parse(read_bytes(r)?);
472 let n_cases = (n_cases < i32::MAX as u32 / 2).then_some(n_cases);
474 let bias: f64 = endian.parse(read_bytes(r)?);
476 let creation_date = RawString(read_vec(r, 9)?);
477 let creation_time = RawString(read_vec(r, 8)?);
478 let file_label = RawString(read_vec(r, 64)?);
479 let _: [u8; 3] = read_bytes(r)?;
482 offsets: start..r.stream_position()?,
498 pub fn decode(self, decoder: &Decoder) -> DecodedRecord {
499 let eye_catcher = decoder.decode(&self.eye_catcher).to_string();
500 let file_label = decoder.decode(&self.file_label).to_string();
501 let creation_date = decoder.decode(&self.creation_date).to_string();
502 let creation_time = decoder.decode(&self.creation_time).to_string();
503 DecodedRecord::Header(HeaderRecord {
505 weight_index: self.weight_index,
506 n_cases: self.n_cases,
508 offsets: self.offsets.clone(),
510 layout_code: self.layout_code,
511 nominal_case_size: self.nominal_case_size,
512 compression: self.compression,
522 pub encoding: &'static Encoding,
523 pub warn: Box<dyn Fn(Warning)>,
527 pub fn new<F>(encoding: &'static Encoding, warn: F) -> Self
529 F: Fn(Warning) + 'static,
533 warn: Box::new(warn),
536 fn warn(&self, warning: Warning) {
539 fn decode_slice<'a>(&self, input: &'a [u8]) -> Cow<'a, str> {
540 let (output, malformed) = self.encoding.decode_without_bom_handling(input);
542 self.warn(Warning::MalformedString {
543 encoding: self.encoding.name().into(),
544 text: output.clone().into(),
550 fn decode<'a>(&self, input: &'a RawString) -> Cow<'a, str> {
551 self.decode_slice(input.0.as_slice())
554 /// Returns `input` decoded from `self.encoding` into UTF-8 such that
555 /// re-encoding the result back into `self.encoding` will have exactly the
556 /// same length in bytes.
558 /// XXX warn about errors?
559 pub fn decode_exact_length<'a>(&self, input: &'a [u8]) -> Cow<'a, str> {
560 if let (s, false) = self.encoding.decode_without_bom_handling(input) {
561 // This is the common case. Usually there will be no errors.
564 // Unusual case. Don't bother to optimize it much.
565 let mut decoder = self.encoding.new_decoder_without_bom_handling();
566 let mut output = String::with_capacity(
568 .max_utf8_buffer_length_without_replacement(input.len())
571 let mut rest = input;
572 while !rest.is_empty() {
573 match decoder.decode_to_string_without_replacement(rest, &mut output, true) {
574 (DecoderResult::InputEmpty, _) => break,
575 (DecoderResult::OutputFull, _) => unreachable!(),
576 (DecoderResult::Malformed(a, b), consumed) => {
577 let skipped = a as usize + b as usize;
578 output.extend(repeat('?').take(skipped));
579 rest = &rest[consumed..];
583 assert_eq!(self.encoding.encode(&output).0.len(), input.len());
588 pub fn decode_identifier(&self, input: &RawString) -> Result<Identifier, IdError> {
589 self.new_identifier(&self.decode(input))
592 pub fn new_identifier(&self, name: &str) -> Result<Identifier, IdError> {
593 Identifier::new(name, self.encoding)
597 impl<S> Header for HeaderRecord<S>
601 fn offsets(&self) -> Range<u64> {
606 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
608 /// Regular system file.
611 /// System file with Zlib-compressed data.
614 /// EBCDIC-encoded system file.
619 /// Magic number for a regular system file.
620 pub const SAV: [u8; 4] = *b"$FL2";
622 /// Magic number for a system file that contains zlib-compressed data.
623 pub const ZSAV: [u8; 4] = *b"$FL3";
625 /// Magic number for an EBCDIC-encoded system file. This is `$FL2` encoded
627 pub const EBCDIC: [u8; 4] = [0x5b, 0xc6, 0xd3, 0xf2];
630 impl Debug for Magic {
631 fn fmt(&self, f: &mut Formatter) -> FmtResult {
632 let s = match *self {
633 Magic::Sav => "$FL2",
634 Magic::Zsav => "$FL3",
635 Magic::Ebcdic => "($FL2 in EBCDIC)",
641 impl TryFrom<[u8; 4]> for Magic {
644 fn try_from(value: [u8; 4]) -> Result<Self, Self::Error> {
646 Magic::SAV => Ok(Magic::Sav),
647 Magic::ZSAV => Ok(Magic::Zsav),
648 Magic::EBCDIC => Ok(Magic::Ebcdic),
649 _ => Err(Error::BadMagic(value)),
654 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
661 pub fn from_width(width: VarWidth) -> VarType {
663 VarWidth::Numeric => Self::Numeric,
664 VarWidth::String(_) => Self::String,
668 pub fn opposite(self) -> VarType {
670 Self::Numeric => Self::String,
671 Self::String => Self::Numeric,
676 impl Display for VarType {
677 fn fmt(&self, f: &mut Formatter) -> FmtResult {
679 VarType::Numeric => write!(f, "numeric"),
680 VarType::String => write!(f, "string"),
685 #[derive(Copy, Clone)]
694 type RawValue = Value<RawStr<8>>;
696 impl<S> Debug for Value<S>
700 fn fmt(&self, f: &mut Formatter) -> FmtResult {
702 Value::Number(Some(number)) => write!(f, "{number:?}"),
703 Value::Number(None) => write!(f, "SYSMIS"),
704 Value::String(s) => write!(f, "{:?}", s),
710 fn read<R: Read>(r: &mut R, var_type: VarType, endian: Endian) -> Result<Self, IoError> {
712 &UntypedValue(read_bytes(r)?),
718 pub fn from_raw(raw: &UntypedValue, var_type: VarType, endian: Endian) -> Self {
720 VarType::String => Value::String(RawStr(raw.0)),
721 VarType::Numeric => {
722 let number: f64 = endian.parse(raw.0);
723 Value::Number((number != -f64::MAX).then_some(number))
728 fn read_case<R: Read + Seek>(
730 var_types: &[VarType],
732 ) -> Result<Option<Vec<Self>>, Error> {
733 let case_start = reader.stream_position()?;
734 let mut values = Vec::with_capacity(var_types.len());
735 for (i, &var_type) in var_types.iter().enumerate() {
736 let Some(raw) = try_read_bytes(reader)? else {
740 let offset = reader.stream_position()?;
741 return Err(Error::EofInCase {
743 case_ofs: offset - case_start,
744 case_len: var_types.len() * 8,
748 values.push(Value::from_raw(&UntypedValue(raw), var_type, endian));
753 fn read_compressed_case<R: Read + Seek>(
755 var_types: &[VarType],
756 codes: &mut VecDeque<u8>,
759 ) -> Result<Option<Vec<Self>>, Error> {
760 let case_start = reader.stream_position()?;
761 let mut values = Vec::with_capacity(var_types.len());
762 for (i, &var_type) in var_types.iter().enumerate() {
764 let Some(code) = codes.pop_front() else {
765 let Some(new_codes): Option<[u8; 8]> = try_read_bytes(reader)? else {
769 let offset = reader.stream_position()?;
770 return Err(Error::EofInCompressedCase {
772 case_ofs: offset - case_start,
776 codes.extend(new_codes.into_iter());
781 1..=251 => match var_type {
782 VarType::Numeric => break Self::Number(Some(code as f64 - bias)),
784 break Self::String(RawStr(endian.to_bytes(code as f64 - bias)))
791 let offset = reader.stream_position()?;
792 return Err(Error::PartialCompressedCase {
794 case_ofs: offset - case_start,
799 break Self::from_raw(&UntypedValue(read_bytes(reader)?), var_type, endian)
801 254 => match var_type {
802 VarType::String => break Self::String(RawStr(*b" ")), // XXX EBCDIC
803 VarType::Numeric => {
804 return Err(Error::CompressedStringExpected {
806 case_ofs: reader.stream_position()? - case_start,
810 255 => match var_type {
811 VarType::Numeric => break Self::Number(None),
813 return Err(Error::CompressedNumberExpected {
815 case_ofs: reader.stream_position()? - case_start,
826 fn decode(self, decoder: &Decoder) -> Value<String> {
828 Self::Number(x) => Value::Number(x),
829 Self::String(s) => Value::String(decoder.decode_exact_length(&s.0).into()),
834 struct ZlibDecodeMultiple<R>
838 reader: Option<ZlibDecoder<R>>,
841 impl<R> ZlibDecodeMultiple<R>
845 fn new(reader: R) -> ZlibDecodeMultiple<R> {
847 reader: Some(ZlibDecoder::new(reader)),
852 impl<R> Read for ZlibDecodeMultiple<R>
856 fn read(&mut self, buf: &mut [u8]) -> Result<usize, IoError> {
858 match self.reader.as_mut().unwrap().read(buf)? {
860 let inner = self.reader.take().unwrap().into_inner();
861 self.reader = Some(ZlibDecoder::new(inner));
869 impl<R> Seek for ZlibDecodeMultiple<R>
873 fn seek(&mut self, pos: SeekFrom) -> Result<u64, IoError> {
874 self.reader.as_mut().unwrap().get_mut().seek(pos)
883 ztrailer_offset: u64,
892 R: Read + Seek + 'static,
895 warn: Box<dyn Fn(Warning)>,
897 header: HeaderRecord<RawString>,
898 var_types: Vec<VarType>,
905 R: Read + Seek + 'static,
907 pub fn new<F>(mut reader: R, warn: F) -> Result<Self, Error>
909 F: Fn(Warning) + 'static,
911 let header = HeaderRecord::read(&mut reader)?;
913 reader: Some(reader),
914 warn: Box::new(warn),
916 var_types: Vec::new(),
917 state: ReaderState::Start,
920 fn cases(&mut self) -> Cases {
921 self.state = ReaderState::End;
923 self.reader.take().unwrap(),
924 take(&mut self.var_types),
928 fn _next(&mut self) -> Option<<Self as Iterator>::Item> {
930 ReaderState::Start => {
931 self.state = ReaderState::Headers;
932 Some(Ok(Record::Header(self.header.clone())))
934 ReaderState::Headers => {
937 self.reader.as_mut().unwrap(),
939 self.var_types.as_slice(),
942 Ok(Some(record)) => break record,
944 Err(error) => return Some(Err(error)),
948 Record::Variable(VariableRecord { width, .. }) => {
949 self.var_types.push(if width == 0 {
955 Record::EndOfHeaders(_) => {
956 self.state = if let Some(Compression::ZLib) = self.header.compression {
957 ReaderState::ZlibHeader
966 ReaderState::ZlibHeader => {
967 let zheader = match ZHeader::read(self.reader.as_mut().unwrap(), self.header.endian)
969 Ok(zheader) => zheader,
970 Err(error) => return Some(Err(error)),
972 self.state = ReaderState::ZlibTrailer {
973 ztrailer_offset: zheader.ztrailer_offset,
974 ztrailer_len: zheader.ztrailer_len,
976 Some(Ok(Record::ZHeader(zheader)))
978 ReaderState::ZlibTrailer {
982 match ZTrailer::read(
983 self.reader.as_mut().unwrap(),
988 Ok(None) => Some(Ok(Record::Cases(Rc::new(RefCell::new(self.cases()))))),
989 Ok(Some(ztrailer)) => Some(Ok(Record::ZTrailer(ztrailer))),
990 Err(error) => Some(Err(error)),
993 ReaderState::Cases => Some(Ok(Record::Cases(Rc::new(RefCell::new(self.cases()))))),
994 ReaderState::End => None,
999 impl<R> Iterator for Reader<R>
1001 R: Read + Seek + 'static,
1003 type Item = Result<Record, Error>;
1005 fn next(&mut self) -> Option<Self::Item> {
1006 let retval = self._next();
1007 if matches!(retval, Some(Err(_))) {
1008 self.state = ReaderState::End;
1014 trait ReadSeek: Read + Seek {}
1015 impl<T> ReadSeek for T where T: Read + Seek {}
1018 reader: Box<dyn ReadSeek>,
1019 var_types: Vec<VarType>,
1020 compression: Option<Compression>,
1023 codes: VecDeque<u8>,
1027 impl Debug for Cases {
1028 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1034 fn new<R>(reader: R, var_types: Vec<VarType>, header: &HeaderRecord<RawString>) -> Self
1036 R: Read + Seek + 'static,
1039 reader: if header.compression == Some(Compression::ZLib) {
1040 Box::new(ZlibDecodeMultiple::new(reader))
1045 compression: header.compression,
1047 endian: header.endian,
1048 codes: VecDeque::with_capacity(8),
1054 impl Iterator for Cases {
1055 type Item = Result<Vec<RawValue>, Error>;
1057 fn next(&mut self) -> Option<Self::Item> {
1062 let retval = if self.compression.is_some() {
1063 Value::read_compressed_case(
1072 Value::read_case(&mut self.reader, &self.var_types, self.endian).transpose()
1074 self.eof = matches!(retval, None | Some(Err(_)));
1079 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
1080 pub struct Spec(pub u32);
1082 impl Debug for Spec {
1083 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1084 let type_ = format_name(self.0 >> 16);
1085 let w = (self.0 >> 8) & 0xff;
1086 let d = self.0 & 0xff;
1087 write!(f, "{:06x} ({type_}{w}.{d})", self.0)
1091 fn format_name(type_: u32) -> Cow<'static, str> {
1130 _ => return format!("<unknown format {type_}>").into(),
1136 pub struct MissingValues<S = String>
1140 /// Individual missing values, up to 3 of them.
1141 pub values: Vec<Value<S>>,
1143 /// Optional range of missing values.
1144 pub range: Option<(Value<S>, Value<S>)>,
1147 impl<S> Debug for MissingValues<S>
1151 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1152 for (i, value) in self.values.iter().enumerate() {
1156 write!(f, "{value:?}")?;
1159 if let Some((low, high)) = &self.range {
1160 if !self.values.is_empty() {
1163 write!(f, "{low:?} THRU {high:?}")?;
1166 if self.is_empty() {
1174 impl<S> MissingValues<S>
1178 fn is_empty(&self) -> bool {
1179 self.values.is_empty() && self.range.is_none()
1183 impl<S> Default for MissingValues<S>
1187 fn default() -> Self {
1195 impl MissingValues<RawStr<8>> {
1196 fn read<R: Read + Seek>(
1202 ) -> Result<Self, Error> {
1203 let (n_values, has_range) = match (width, code) {
1204 (_, 0..=3) => (code, false),
1205 (0, -2) => (0, true),
1206 (0, -3) => (1, true),
1207 (0, _) => return Err(Error::BadNumericMissingValueCode { offset, code }),
1208 (_, _) => return Err(Error::BadStringMissingValueCode { offset, code }),
1211 let var_type = if width == 0 {
1217 let mut values = Vec::new();
1218 for _ in 0..n_values {
1219 values.push(RawValue::read(r, var_type, endian)?);
1221 let range = if has_range {
1222 let low = RawValue::read(r, var_type, endian)?;
1223 let high = RawValue::read(r, var_type, endian)?;
1228 Ok(Self { values, range })
1230 fn decode(&self, decoder: &Decoder) -> MissingValues<String> {
1235 .map(|value| value.decode(decoder))
1240 .map(|(low, high)| (low.decode(decoder), high.decode(decoder))),
1246 pub struct VariableRecord<S, V>
1251 /// Range of offsets in file.
1252 pub offsets: Range<u64>,
1254 /// Variable width, in the range -1..=255.
1257 /// Variable name, padded on the right with spaces.
1261 pub print_format: Spec,
1264 pub write_format: Spec,
1267 pub missing_values: MissingValues<V>,
1269 /// Optional variable label.
1270 pub label: Option<S>,
1273 impl<S, V> Debug for VariableRecord<S, V>
1278 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1283 match self.width.cmp(&0) {
1284 Ordering::Greater => "string",
1285 Ordering::Equal => "numeric",
1286 Ordering::Less => "long string continuation record",
1289 writeln!(f, "Print format: {:?}", self.print_format)?;
1290 writeln!(f, "Write format: {:?}", self.write_format)?;
1291 writeln!(f, "Name: {:?}", &self.name)?;
1292 writeln!(f, "Variable label: {:?}", self.label)?;
1293 writeln!(f, "Missing values: {:?}", self.missing_values)
1297 impl VariableRecord<RawString, RawStr<8>> {
1298 fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<Record, Error> {
1299 let start_offset = r.stream_position()?;
1300 let width: i32 = endian.parse(read_bytes(r)?);
1301 let code_offset = r.stream_position()?;
1302 let has_variable_label: u32 = endian.parse(read_bytes(r)?);
1303 let missing_value_code: i32 = endian.parse(read_bytes(r)?);
1304 let print_format = Spec(endian.parse(read_bytes(r)?));
1305 let write_format = Spec(endian.parse(read_bytes(r)?));
1306 let name = RawString(read_vec(r, 8)?);
1308 let label = match has_variable_label {
1311 let len: u32 = endian.parse(read_bytes(r)?);
1312 let read_len = len.min(65535) as usize;
1313 let label = RawString(read_vec(r, read_len)?);
1315 let padding_bytes = Integer::next_multiple_of(&len, &4) - len;
1316 let _ = read_vec(r, padding_bytes as usize)?;
1321 return Err(Error::BadVariableLabelCode {
1324 code: has_variable_label,
1329 let missing_values =
1330 MissingValues::read(r, start_offset, width, missing_value_code, endian)?;
1332 let end_offset = r.stream_position()?;
1334 Ok(Record::Variable(VariableRecord {
1335 offsets: start_offset..end_offset,
1345 pub fn decode(self, decoder: &Decoder) -> DecodedRecord {
1346 DecodedRecord::Variable(VariableRecord {
1347 offsets: self.offsets.clone(),
1349 name: decoder.decode(&self.name).to_string(),
1350 print_format: self.print_format,
1351 write_format: self.write_format,
1352 missing_values: self.missing_values.decode(decoder),
1356 .map(|label| decoder.decode(label).to_string()),
1361 #[derive(Copy, Clone)]
1362 pub struct UntypedValue(pub [u8; 8]);
1364 impl Debug for UntypedValue {
1365 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1366 let little: f64 = Endian::Little.parse(self.0);
1367 let little = format!("{:?}", little);
1368 let big: f64 = Endian::Big.parse(self.0);
1369 let big = format!("{:?}", big);
1370 let number = if little.len() <= big.len() {
1375 write!(f, "{number}")?;
1377 let string = default_decode(&self.0);
1379 .split(|c: char| c == '\0' || c.is_control())
1382 write!(f, "{string:?}")?;
1388 pub struct RawString(pub Vec<u8>);
1390 impl From<Vec<u8>> for RawString {
1391 fn from(source: Vec<u8>) -> Self {
1396 impl From<&[u8]> for RawString {
1397 fn from(source: &[u8]) -> Self {
1402 impl Debug for RawString {
1403 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1404 write!(f, "{:?}", default_decode(self.0.as_slice()))
1408 #[derive(Copy, Clone)]
1409 pub struct RawStr<const N: usize>(pub [u8; N]);
1411 impl<const N: usize> From<[u8; N]> for RawStr<N> {
1412 fn from(source: [u8; N]) -> Self {
1417 impl<const N: usize> Debug for RawStr<N> {
1418 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1419 write!(f, "{:?}", default_decode(&self.0))
1423 #[derive(Clone, Debug)]
1424 pub struct ValueLabel<V, S>
1429 pub value: Value<V>,
1434 pub struct ValueLabelRecord<V, S>
1439 /// Range of offsets in file.
1440 pub offsets: Range<u64>,
1443 pub labels: Vec<ValueLabel<V, S>>,
1445 /// The 1-based indexes of the variable indexes.
1446 pub dict_indexes: Vec<u32>,
1448 /// The types of the variables.
1449 pub var_type: VarType,
1452 impl<V, S> Debug for ValueLabelRecord<V, S>
1457 fn fmt(&self, f: &mut Formatter) -> FmtResult {
1458 writeln!(f, "labels: ")?;
1459 for label in self.labels.iter() {
1460 writeln!(f, "{label:?}")?;
1462 write!(f, "apply to {} variables", self.var_type)?;
1463 for dict_index in self.dict_indexes.iter() {
1464 write!(f, " #{dict_index}")?;
1470 impl<V, S> Header for ValueLabelRecord<V, S>
1475 fn offsets(&self) -> Range<u64> {
1476 self.offsets.clone()
1480 impl<V, S> ValueLabelRecord<V, S>
1485 /// Maximum number of value labels in a record.
1486 pub const MAX_LABELS: u32 = u32::MAX / 8;
1488 /// Maximum number of variable indexes in a record.
1489 pub const MAX_INDEXES: u32 = u32::MAX / 8;
1492 impl ValueLabelRecord<RawStr<8>, RawString> {
1493 fn read<R: Read + Seek>(
1496 var_types: &[VarType],
1497 warn: &dyn Fn(Warning),
1498 ) -> Result<Option<Record>, Error> {
1499 let label_offset = r.stream_position()?;
1500 let n: u32 = endian.parse(read_bytes(r)?);
1501 if n > Self::MAX_LABELS {
1502 return Err(Error::BadNumberOfValueLabels {
1503 offset: label_offset,
1505 max: Self::MAX_LABELS,
1509 let mut labels = Vec::new();
1511 let value = UntypedValue(read_bytes(r)?);
1512 let label_len: u8 = endian.parse(read_bytes(r)?);
1513 let label_len = label_len as usize;
1514 let padded_len = Integer::next_multiple_of(&(label_len + 1), &8);
1516 let mut label = read_vec(r, padded_len - 1)?;
1517 label.truncate(label_len);
1518 labels.push((value, RawString(label)));
1521 let index_offset = r.stream_position()?;
1522 let rec_type: u32 = endian.parse(read_bytes(r)?);
1524 return Err(Error::ExpectedVarIndexRecord {
1525 offset: index_offset,
1530 let n: u32 = endian.parse(read_bytes(r)?);
1531 if n > Self::MAX_INDEXES {
1532 return Err(Error::TooManyVarIndexes {
1533 offset: index_offset,
1535 max: Self::MAX_INDEXES,
1539 let index_offset = r.stream_position()?;
1540 let mut dict_indexes = Vec::with_capacity(n as usize);
1541 let mut invalid_indexes = Vec::new();
1543 let index: u32 = endian.parse(read_bytes(r)?);
1544 if index == 0 || index as usize > var_types.len() {
1545 dict_indexes.push(index);
1547 invalid_indexes.push(index);
1550 if !invalid_indexes.is_empty() {
1551 warn(Warning::InvalidVarIndexes {
1552 offset: index_offset,
1553 max: var_types.len(),
1554 invalid: invalid_indexes,
1558 let Some(&first_index) = dict_indexes.first() else {
1559 warn(Warning::NoVarIndexes {
1560 offset: index_offset,
1564 let var_type = var_types[first_index as usize - 1];
1565 let mut wrong_type_indexes = Vec::new();
1566 dict_indexes.retain(|&index| {
1567 if var_types[index as usize - 1] != var_type {
1568 wrong_type_indexes.push(index);
1574 if !wrong_type_indexes.is_empty() {
1575 warn(Warning::MixedVarTypes {
1576 offset: index_offset,
1578 wrong_types: wrong_type_indexes,
1584 .map(|(value, label)| ValueLabel {
1585 value: Value::from_raw(&value, var_type, endian),
1590 let end_offset = r.stream_position()?;
1591 Ok(Some(Record::ValueLabel(ValueLabelRecord {
1592 offsets: label_offset..end_offset,
1599 fn decode(self, decoder: &Decoder) -> ValueLabelRecord<RawStr<8>, String> {
1603 .map(|ValueLabel { value, label }| ValueLabel {
1605 label: decoder.decode(label).to_string(),
1609 offsets: self.offsets.clone(),
1611 dict_indexes: self.dict_indexes.clone(),
1612 var_type: self.var_type,
1617 #[derive(Clone, Debug)]
1618 pub struct DocumentRecord<S>
1622 pub offsets: Range<u64>,
1624 /// The document, as an array of 80-byte lines.
1628 pub type RawDocumentLine = RawStr<DOC_LINE_LEN>;
1630 /// Length of a line in a document. Document lines are fixed-length and
1631 /// padded on the right with spaces.
1632 pub const DOC_LINE_LEN: usize = 80;
1634 impl DocumentRecord<RawDocumentLine> {
1635 /// Maximum number of lines we will accept in a document. This is simply
1636 /// the maximum number that will fit in a 32-bit space.
1637 pub const MAX_LINES: usize = i32::MAX as usize / DOC_LINE_LEN;
1639 fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<Record, Error> {
1640 let start_offset = r.stream_position()?;
1641 let n: u32 = endian.parse(read_bytes(r)?);
1643 if n > Self::MAX_LINES {
1644 Err(Error::BadDocumentLength {
1645 offset: start_offset,
1647 max: Self::MAX_LINES,
1650 let mut lines = Vec::with_capacity(n);
1652 lines.push(RawStr(read_bytes(r)?));
1654 let end_offset = r.stream_position()?;
1655 Ok(Record::Document(DocumentRecord {
1656 offsets: start_offset..end_offset,
1662 pub fn decode(self, decoder: &Decoder) -> DecodedRecord {
1663 DecodedRecord::Document(DocumentRecord {
1664 offsets: self.offsets.clone(),
1668 .map(|s| decoder.decode_slice(&s.0).to_string())
1674 impl<S> Header for DocumentRecord<S>
1678 fn offsets(&self) -> Range<u64> {
1679 self.offsets.clone()
1683 trait ExtensionRecord {
1685 const SIZE: Option<u32>;
1686 const COUNT: Option<u32>;
1687 const NAME: &'static str;
1688 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Warning>;
1691 #[derive(Clone, Debug)]
1692 pub struct IntegerInfoRecord {
1693 pub offsets: Range<u64>,
1694 pub version: (i32, i32, i32),
1695 pub machine_code: i32,
1696 pub floating_point_rep: i32,
1697 pub compression_code: i32,
1698 pub endianness: i32,
1699 pub character_code: i32,
1702 impl ExtensionRecord for IntegerInfoRecord {
1703 const SUBTYPE: u32 = 3;
1704 const SIZE: Option<u32> = Some(4);
1705 const COUNT: Option<u32> = Some(8);
1706 const NAME: &'static str = "integer record";
1708 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Warning> {
1709 ext.check_size::<Self>()?;
1711 let mut input = &ext.data[..];
1712 let data: Vec<i32> = (0..8)
1713 .map(|_| endian.parse(read_bytes(&mut input).unwrap()))
1715 Ok(Record::IntegerInfo(IntegerInfoRecord {
1716 offsets: ext.offsets.clone(),
1717 version: (data[0], data[1], data[2]),
1718 machine_code: data[3],
1719 floating_point_rep: data[4],
1720 compression_code: data[5],
1721 endianness: data[6],
1722 character_code: data[7],
1727 #[derive(Clone, Debug)]
1728 pub struct FloatInfoRecord {
1734 impl ExtensionRecord for FloatInfoRecord {
1735 const SUBTYPE: u32 = 4;
1736 const SIZE: Option<u32> = Some(8);
1737 const COUNT: Option<u32> = Some(3);
1738 const NAME: &'static str = "floating point record";
1740 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Warning> {
1741 ext.check_size::<Self>()?;
1743 let mut input = &ext.data[..];
1744 let data: Vec<f64> = (0..3)
1745 .map(|_| endian.parse(read_bytes(&mut input).unwrap()))
1747 Ok(Record::FloatInfo(FloatInfoRecord {
1755 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1756 pub enum CategoryLabels {
1761 #[derive(Clone, Debug)]
1762 pub enum MultipleResponseType {
1765 labels: CategoryLabels,
1770 impl MultipleResponseType {
1771 fn parse(input: &[u8]) -> Result<(MultipleResponseType, &[u8]), Warning> {
1772 let (mr_type, input) = match input.split_first() {
1773 Some((b'C', input)) => (MultipleResponseType::MultipleCategory, input),
1774 Some((b'D', input)) => {
1775 let (value, input) = parse_counted_string(input)?;
1777 MultipleResponseType::MultipleDichotomy {
1779 labels: CategoryLabels::VarLabels,
1784 Some((b'E', input)) => {
1785 let (labels, input) = if let Some(rest) = input.strip_prefix(b" 1 ") {
1786 (CategoryLabels::CountedValues, rest)
1787 } else if let Some(rest) = input.strip_prefix(b" 11 ") {
1788 (CategoryLabels::VarLabels, rest)
1790 return Err(Warning::TBD);
1792 let (value, input) = parse_counted_string(input)?;
1794 MultipleResponseType::MultipleDichotomy { value, labels },
1798 _ => return Err(Warning::TBD),
1800 Ok((mr_type, input))
1804 #[derive(Clone, Debug)]
1805 pub struct MultipleResponseSet<I, S>
1812 pub mr_type: MultipleResponseType,
1813 pub short_names: Vec<I>,
1816 impl MultipleResponseSet<RawString, RawString> {
1817 fn parse(input: &[u8]) -> Result<(Self, &[u8]), Warning> {
1818 let Some(equals) = input.iter().position(|&b| b == b'=') else {
1819 return Err(Warning::TBD);
1821 let (name, input) = input.split_at(equals);
1822 let (mr_type, input) = MultipleResponseType::parse(input)?;
1823 let Some(input) = input.strip_prefix(b" ") else {
1824 return Err(Warning::TBD);
1826 let (label, mut input) = parse_counted_string(input)?;
1827 let mut vars = Vec::new();
1828 while input.first() != Some(&b'\n') {
1829 match input.split_first() {
1830 Some((b' ', rest)) => {
1831 let Some(length) = rest.iter().position(|b| b" \n".contains(b)) else {
1832 return Err(Warning::TBD);
1834 let (var, rest) = rest.split_at(length);
1835 if !var.is_empty() {
1836 vars.push(var.into());
1840 _ => return Err(Warning::TBD),
1843 while input.first() == Some(&b'\n') {
1844 input = &input[1..];
1847 MultipleResponseSet {
1860 ) -> Result<MultipleResponseSet<Identifier, String>, Warning> {
1861 let mut short_names = Vec::with_capacity(self.short_names.len());
1862 for short_name in self.short_names.iter() {
1863 if let Some(short_name) = decoder
1864 .decode_identifier(short_name)
1865 .map_err(Warning::InvalidMrSetName)
1866 .issue_warning(&decoder.warn)
1868 short_names.push(short_name);
1871 Ok(MultipleResponseSet {
1873 .decode_identifier(&self.name)
1874 .map_err(Warning::InvalidMrSetVariableName)?,
1875 label: decoder.decode(&self.label).to_string(),
1876 mr_type: self.mr_type.clone(),
1882 #[derive(Clone, Debug)]
1883 pub struct MultipleResponseRecord<I, S>(pub Vec<MultipleResponseSet<I, S>>)
1888 impl ExtensionRecord for MultipleResponseRecord<RawString, RawString> {
1889 const SUBTYPE: u32 = 7;
1890 const SIZE: Option<u32> = Some(1);
1891 const COUNT: Option<u32> = None;
1892 const NAME: &'static str = "multiple response set record";
1894 fn parse(ext: &Extension, _endian: Endian) -> Result<Record, Warning> {
1895 ext.check_size::<Self>()?;
1897 let mut input = &ext.data[..];
1898 let mut sets = Vec::new();
1899 while !input.is_empty() {
1900 let (set, rest) = MultipleResponseSet::parse(input)?;
1904 Ok(Record::MultipleResponse(MultipleResponseRecord(sets)))
1908 impl MultipleResponseRecord<RawString, RawString> {
1909 fn decode(self, decoder: &Decoder) -> DecodedRecord {
1910 let mut sets = Vec::new();
1911 for set in self.0.iter() {
1912 if let Some(set) = set.decode(decoder).issue_warning(&decoder.warn) {
1916 DecodedRecord::MultipleResponse(MultipleResponseRecord(sets))
1920 fn parse_counted_string(input: &[u8]) -> Result<(RawString, &[u8]), Warning> {
1921 let Some(space) = input.iter().position(|&b| b == b' ') else {
1922 return Err(Warning::TBD);
1924 let Ok(length) = from_utf8(&input[..space]) else {
1925 return Err(Warning::TBD);
1927 let Ok(length): Result<usize, _> = length.parse() else {
1928 return Err(Warning::TBD);
1931 let input = &input[space + 1..];
1932 if input.len() < length {
1933 return Err(Warning::TBD);
1936 let (string, rest) = input.split_at(length);
1937 Ok((string.into(), rest))
1940 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1948 pub fn default_for_type(var_type: VarType) -> Option<Measure> {
1950 VarType::Numeric => None,
1951 VarType::String => Some(Self::Nominal),
1955 fn try_decode(source: u32) -> Result<Option<Measure>, Warning> {
1958 1 => Ok(Some(Measure::Nominal)),
1959 2 => Ok(Some(Measure::Ordinal)),
1960 3 => Ok(Some(Measure::Scale)),
1961 _ => Err(Warning::InvalidMeasurement(source)),
1966 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1967 pub enum Alignment {
1974 fn try_decode(source: u32) -> Result<Option<Alignment>, Warning> {
1977 1 => Ok(Some(Alignment::Left)),
1978 2 => Ok(Some(Alignment::Right)),
1979 3 => Ok(Some(Alignment::Center)),
1980 _ => Err(Warning::InvalidAlignment(source)),
1984 pub fn default_for_type(var_type: VarType) -> Self {
1986 VarType::Numeric => Self::Right,
1987 VarType::String => Self::Left,
1992 #[derive(Clone, Debug)]
1993 pub struct VarDisplay {
1994 pub measure: Option<Measure>,
1995 pub width: Option<u32>,
1996 pub alignment: Option<Alignment>,
1999 #[derive(Clone, Debug)]
2000 pub struct VarDisplayRecord(pub Vec<VarDisplay>);
2002 impl VarDisplayRecord {
2003 const SUBTYPE: u32 = 11;
2009 warn: &dyn Fn(Warning),
2010 ) -> Result<Record, Warning> {
2012 return Err(Warning::BadRecordSize {
2013 offset: ext.offsets.start,
2014 record: String::from("variable display record"),
2020 let has_width = if ext.count as usize == 3 * n_vars {
2022 } else if ext.count as usize == 2 * n_vars {
2025 return Err(Warning::TBD);
2028 let mut var_displays = Vec::new();
2029 let mut input = &ext.data[..];
2030 for _ in 0..n_vars {
2031 let measure = Measure::try_decode(endian.parse(read_bytes(&mut input).unwrap()))
2032 .issue_warning(&warn)
2034 let width = has_width.then(|| endian.parse(read_bytes(&mut input).unwrap()));
2035 let alignment = Alignment::try_decode(endian.parse(read_bytes(&mut input).unwrap()))
2036 .issue_warning(&warn)
2038 var_displays.push(VarDisplay {
2044 Ok(Record::VarDisplay(VarDisplayRecord(var_displays)))
2048 #[derive(Clone, Debug)]
2049 pub struct LongStringMissingValues<N, V>
2058 pub missing_values: MissingValues<V>,
2061 impl LongStringMissingValues<RawString, RawStr<8>> {
2065 ) -> Result<LongStringMissingValues<Identifier, String>, IdError> {
2066 Ok(LongStringMissingValues {
2067 var_name: decoder.decode_identifier(&self.var_name)?,
2068 missing_values: self.missing_values.decode(decoder),
2073 #[derive(Clone, Debug)]
2074 pub struct LongStringMissingValueRecord<N, V>(pub Vec<LongStringMissingValues<N, V>>)
2079 impl ExtensionRecord for LongStringMissingValueRecord<RawString, RawStr<8>> {
2080 const SUBTYPE: u32 = 22;
2081 const SIZE: Option<u32> = Some(1);
2082 const COUNT: Option<u32> = None;
2083 const NAME: &'static str = "long string missing values record";
2085 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Warning> {
2086 ext.check_size::<Self>()?;
2088 let mut input = &ext.data[..];
2089 let mut missing_value_set = Vec::new();
2090 while !input.is_empty() {
2091 let var_name = read_string(&mut input, endian)?;
2092 let n_missing_values: u8 = endian.parse(read_bytes(&mut input)?);
2093 let value_len: u32 = endian.parse(read_bytes(&mut input)?);
2095 let offset = (ext.data.len() - input.len() - 8) as u64 + ext.offsets.start;
2096 return Err(Warning::BadLongMissingValueLength {
2097 record_offset: ext.offsets.start,
2102 let mut values = Vec::new();
2103 for i in 0..n_missing_values {
2104 let value: [u8; 8] = read_bytes(&mut input)?;
2105 let numeric_value: u64 = endian.parse(value);
2106 let value = if i > 0 && numeric_value == 8 {
2107 // Tolerate files written by old, buggy versions of PSPP
2108 // where we believed that the value_length was repeated
2109 // before each missing value.
2110 read_bytes(&mut input)?
2114 values.push(Value::String(RawStr(value)));
2116 let missing_values = MissingValues {
2120 missing_value_set.push(LongStringMissingValues {
2125 Ok(Record::LongStringMissingValues(
2126 LongStringMissingValueRecord(missing_value_set),
2131 impl LongStringMissingValueRecord<RawString, RawStr<8>> {
2132 pub fn decode(self, decoder: &Decoder) -> LongStringMissingValueRecord<Identifier, String> {
2133 let mut mvs = Vec::with_capacity(self.0.len());
2134 for mv in self.0.iter() {
2135 if let Some(mv) = mv
2137 .map_err(Warning::InvalidLongStringMissingValueVariableName)
2138 .issue_warning(&decoder.warn)
2143 LongStringMissingValueRecord(mvs)
2147 #[derive(Clone, Debug)]
2148 pub struct EncodingRecord(pub String);
2150 impl ExtensionRecord for EncodingRecord {
2151 const SUBTYPE: u32 = 20;
2152 const SIZE: Option<u32> = Some(1);
2153 const COUNT: Option<u32> = None;
2154 const NAME: &'static str = "encoding record";
2156 fn parse(ext: &Extension, _endian: Endian) -> Result<Record, Warning> {
2157 ext.check_size::<Self>()?;
2159 Ok(Record::Encoding(EncodingRecord(
2160 String::from_utf8(ext.data.clone()).map_err(|_| Warning::BadEncodingName {
2161 offset: ext.offsets.start,
2167 #[derive(Clone, Debug)]
2168 pub struct NumberOfCasesRecord {
2169 /// Always observed as 1.
2172 /// Number of cases.
2176 impl ExtensionRecord for NumberOfCasesRecord {
2177 const SUBTYPE: u32 = 16;
2178 const SIZE: Option<u32> = Some(8);
2179 const COUNT: Option<u32> = Some(2);
2180 const NAME: &'static str = "extended number of cases record";
2182 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Warning> {
2183 ext.check_size::<Self>()?;
2185 let mut input = &ext.data[..];
2186 let one = endian.parse(read_bytes(&mut input)?);
2187 let n_cases = endian.parse(read_bytes(&mut input)?);
2189 Ok(Record::NumberOfCases(NumberOfCasesRecord { one, n_cases }))
2193 #[derive(Clone, Debug)]
2194 pub struct TextRecord {
2195 pub offsets: Range<u64>,
2198 pub rec_type: TextRecordType,
2200 /// The text content of the record.
2201 pub text: RawString,
2204 #[derive(Clone, Copy, Debug)]
2205 pub enum TextRecordType {
2215 fn new(extension: Extension, rec_type: TextRecordType) -> Self {
2217 offsets: extension.offsets,
2219 text: extension.data.into(),
2222 pub fn decode(self, decoder: &Decoder) -> DecodedRecord {
2223 match self.rec_type {
2224 TextRecordType::VariableSets => {
2225 DecodedRecord::VariableSets(VariableSetRecord::decode(&self, decoder))
2227 TextRecordType::ProductInfo => {
2228 DecodedRecord::ProductInfo(ProductInfoRecord::decode(&self, decoder))
2230 TextRecordType::LongNames => {
2231 DecodedRecord::LongNames(LongNamesRecord::decode(&self, decoder))
2233 TextRecordType::VeryLongStrings => {
2234 DecodedRecord::VeryLongStrings(VeryLongStringsRecord::decode(&self, decoder))
2236 TextRecordType::FileAttributes => {
2237 DecodedRecord::FileAttributes(FileAttributeRecord::decode(&self, decoder))
2239 TextRecordType::VariableAttributes => {
2240 DecodedRecord::VariableAttributes(VariableAttributeRecord::decode(&self, decoder))
2246 #[derive(Clone, Debug)]
2247 pub struct VeryLongString {
2248 pub short_name: Identifier,
2252 impl VeryLongString {
2253 fn parse(decoder: &Decoder, input: &str) -> Result<VeryLongString, Warning> {
2254 let Some((short_name, length)) = input.split_once('=') else {
2255 return Err(Warning::TBD);
2257 let short_name = decoder
2258 .new_identifier(short_name)
2259 .map_err(Warning::InvalidLongStringName)?;
2260 let length = length.parse().map_err(|_| Warning::TBD)?;
2261 Ok(VeryLongString { short_name, length })
2265 #[derive(Clone, Debug)]
2266 pub struct VeryLongStringsRecord(Vec<VeryLongString>);
2268 impl VeryLongStringsRecord {
2269 fn decode(source: &TextRecord, decoder: &Decoder) -> Self {
2270 let input = decoder.decode(&source.text);
2271 let mut very_long_strings = Vec::new();
2274 .map(|s| s.trim_end_matches('\t'))
2275 .filter(|s| !s.is_empty())
2277 if let Some(vls) = VeryLongString::parse(decoder, tuple).issue_warning(&decoder.warn) {
2278 very_long_strings.push(vls)
2281 VeryLongStringsRecord(very_long_strings)
2285 #[derive(Clone, Debug)]
2286 pub struct Attribute {
2287 pub name: Identifier,
2288 pub values: Vec<String>,
2292 fn parse<'a>(decoder: &Decoder, input: &'a str) -> Result<(Attribute, &'a str), Warning> {
2293 let Some((name, mut input)) = input.split_once('(') else {
2294 return Err(Warning::TBD);
2297 .new_identifier(name)
2298 .map_err(Warning::InvalidAttributeName)?;
2299 let mut values = Vec::new();
2301 let Some((value, rest)) = input.split_once('\n') else {
2302 return Err(Warning::TBD);
2304 if let Some(stripped) = value
2306 .and_then(|value| value.strip_suffix('\''))
2308 values.push(stripped.into());
2310 decoder.warn(Warning::TBD);
2311 values.push(value.into());
2313 if let Some(rest) = rest.strip_prefix(')') {
2314 let attribute = Attribute { name, values };
2315 return Ok((attribute, rest));
2322 #[derive(Clone, Debug, Default)]
2323 pub struct AttributeSet(pub HashMap<Identifier, Vec<String>>);
2329 sentinel: Option<char>,
2330 ) -> Result<(AttributeSet, &'a str), Warning> {
2331 let mut attributes = HashMap::new();
2333 match input.chars().next() {
2334 None => break input,
2335 c if c == sentinel => break &input[1..],
2337 let (attribute, rest) = Attribute::parse(decoder, input)?;
2338 // XXX report duplicate name
2339 attributes.insert(attribute.name, attribute.values);
2344 Ok((AttributeSet(attributes), rest))
2348 #[derive(Clone, Debug, Default)]
2349 pub struct FileAttributeRecord(AttributeSet);
2351 impl FileAttributeRecord {
2352 fn decode(source: &TextRecord, decoder: &Decoder) -> Self {
2353 let input = decoder.decode(&source.text);
2354 match AttributeSet::parse(decoder, &input, None).issue_warning(&decoder.warn) {
2355 Some((set, rest)) => {
2356 if !rest.is_empty() {
2357 decoder.warn(Warning::TBD);
2359 FileAttributeRecord(set)
2361 None => FileAttributeRecord::default(),
2366 #[derive(Clone, Debug)]
2367 pub struct VarAttributeSet {
2368 pub long_var_name: Identifier,
2369 pub attributes: AttributeSet,
2372 impl VarAttributeSet {
2373 fn parse<'a>(decoder: &Decoder, input: &'a str) -> Result<(VarAttributeSet, &'a str), Warning> {
2374 let Some((long_var_name, rest)) = input.split_once(':') else {
2375 return Err(Warning::TBD);
2377 let long_var_name = decoder
2378 .new_identifier(long_var_name)
2379 .map_err(Warning::InvalidAttributeVariableName)?;
2380 let (attributes, rest) = AttributeSet::parse(decoder, rest, Some('/'))?;
2381 let var_attribute = VarAttributeSet {
2385 Ok((var_attribute, rest))
2389 #[derive(Clone, Debug)]
2390 pub struct VariableAttributeRecord(Vec<VarAttributeSet>);
2392 impl VariableAttributeRecord {
2393 fn decode(source: &TextRecord, decoder: &Decoder) -> Self {
2394 let decoded = decoder.decode(&source.text);
2395 let mut input = decoded.as_ref();
2396 let mut var_attribute_sets = Vec::new();
2397 while !input.is_empty() {
2398 let Some((var_attribute, rest)) =
2399 VarAttributeSet::parse(decoder, input).issue_warning(&decoder.warn)
2403 var_attribute_sets.push(var_attribute);
2406 VariableAttributeRecord(var_attribute_sets)
2410 #[derive(Clone, Debug)]
2411 pub struct LongName {
2412 pub short_name: Identifier,
2413 pub long_name: Identifier,
2417 fn parse(input: &str, decoder: &Decoder) -> Result<Self, Warning> {
2418 let Some((short_name, long_name)) = input.split_once('=') else {
2419 return Err(Warning::TBD);
2421 let short_name = decoder
2422 .new_identifier(short_name)
2423 .map_err(Warning::InvalidShortName)?;
2424 let long_name = decoder
2425 .new_identifier(long_name)
2426 .map_err(Warning::InvalidLongName)?;
2434 #[derive(Clone, Debug)]
2435 pub struct LongNamesRecord(Vec<LongName>);
2437 impl LongNamesRecord {
2438 fn decode(source: &TextRecord, decoder: &Decoder) -> Self {
2439 let input = decoder.decode(&source.text);
2440 let mut names = Vec::new();
2441 for pair in input.split('\t').filter(|s| !s.is_empty()) {
2442 if let Some(long_name) = LongName::parse(pair, decoder).issue_warning(&decoder.warn) {
2443 names.push(long_name);
2446 LongNamesRecord(names)
2450 #[derive(Clone, Debug)]
2451 pub struct ProductInfoRecord(pub String);
2453 impl ProductInfoRecord {
2454 fn decode(source: &TextRecord, decoder: &Decoder) -> Self {
2455 Self(decoder.decode(&source.text).into())
2458 #[derive(Clone, Debug)]
2459 pub struct VariableSet {
2461 pub vars: Vec<Identifier>,
2465 fn parse(input: &str, decoder: &Decoder) -> Result<Self, Warning> {
2466 let (name, input) = input.split_once('=').ok_or(Warning::TBD)?;
2467 let mut vars = Vec::new();
2468 for var in input.split_ascii_whitespace() {
2469 if let Some(identifier) = decoder
2470 .new_identifier(var)
2471 .map_err(Warning::InvalidVariableSetName)
2472 .issue_warning(&decoder.warn)
2474 vars.push(identifier);
2484 #[derive(Clone, Debug)]
2485 pub struct VariableSetRecord {
2486 pub offsets: Range<u64>,
2487 pub sets: Vec<VariableSet>,
2490 impl VariableSetRecord {
2491 fn decode(source: &TextRecord, decoder: &Decoder) -> VariableSetRecord {
2492 let mut sets = Vec::new();
2493 let input = decoder.decode(&source.text);
2494 for line in input.lines() {
2495 if let Some(set) = VariableSet::parse(line, decoder).issue_warning(&decoder.warn) {
2500 offsets: source.offsets.clone(),
2506 trait IssueWarning<T> {
2507 fn issue_warning<F>(self, warn: &F) -> Option<T>
2511 impl<T> IssueWarning<T> for Result<T, Warning> {
2512 fn issue_warning<F>(self, warn: &F) -> Option<T>
2517 Ok(result) => Some(result),
2526 #[derive(Clone, Debug)]
2527 pub struct Extension {
2528 pub offsets: Range<u64>,
2533 /// Size of each data element.
2536 /// Number of data elements.
2539 /// `size * count` bytes of data.
2544 fn check_size<E: ExtensionRecord>(&self) -> Result<(), Warning> {
2545 if let Some(expected_size) = E::SIZE {
2546 if self.size != expected_size {
2547 return Err(Warning::BadRecordSize {
2548 offset: self.offsets.start,
2549 record: E::NAME.into(),
2555 if let Some(expected_count) = E::COUNT {
2556 if self.count != expected_count {
2557 return Err(Warning::BadRecordCount {
2558 offset: self.offsets.start,
2559 record: E::NAME.into(),
2568 fn read<R: Read + Seek>(
2572 warn: &dyn Fn(Warning),
2573 ) -> Result<Option<Record>, Error> {
2574 let subtype = endian.parse(read_bytes(r)?);
2575 let header_offset = r.stream_position()?;
2576 let size: u32 = endian.parse(read_bytes(r)?);
2577 let count = endian.parse(read_bytes(r)?);
2578 let Some(product) = size.checked_mul(count) else {
2579 return Err(Error::ExtensionRecordTooLarge {
2580 offset: header_offset,
2586 let start_offset = r.stream_position()?;
2587 let data = read_vec(r, product as usize)?;
2588 let end_offset = start_offset + product as u64;
2589 let extension = Extension {
2590 offsets: start_offset..end_offset,
2596 let result = match subtype {
2597 IntegerInfoRecord::SUBTYPE => IntegerInfoRecord::parse(&extension, endian),
2598 FloatInfoRecord::SUBTYPE => FloatInfoRecord::parse(&extension, endian),
2599 VarDisplayRecord::SUBTYPE => VarDisplayRecord::parse(&extension, n_vars, endian, warn),
2600 MultipleResponseRecord::SUBTYPE | 19 => {
2601 MultipleResponseRecord::parse(&extension, endian)
2603 LongStringValueLabelRecord::SUBTYPE => {
2604 LongStringValueLabelRecord::parse(&extension, endian)
2606 EncodingRecord::SUBTYPE => EncodingRecord::parse(&extension, endian),
2607 NumberOfCasesRecord::SUBTYPE => NumberOfCasesRecord::parse(&extension, endian),
2608 5 => Ok(Record::Text(TextRecord::new(
2610 TextRecordType::VariableSets,
2612 10 => Ok(Record::Text(TextRecord::new(
2614 TextRecordType::ProductInfo,
2616 13 => Ok(Record::Text(TextRecord::new(
2618 TextRecordType::LongNames,
2620 14 => Ok(Record::Text(TextRecord::new(
2622 TextRecordType::VeryLongStrings,
2624 17 => Ok(Record::Text(TextRecord::new(
2626 TextRecordType::FileAttributes,
2628 18 => Ok(Record::Text(TextRecord::new(
2630 TextRecordType::VariableAttributes,
2632 _ => Ok(Record::OtherExtension(extension)),
2635 Ok(result) => Ok(Some(result)),
2644 #[derive(Clone, Debug)]
2645 pub struct ZHeader {
2646 /// File offset to the start of the record.
2649 /// File offset to the ZLIB data header.
2650 pub zheader_offset: u64,
2652 /// File offset to the ZLIB trailer.
2653 pub ztrailer_offset: u64,
2655 /// Length of the ZLIB trailer in bytes.
2656 pub ztrailer_len: u64,
2660 fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<ZHeader, Error> {
2661 let offset = r.stream_position()?;
2662 let zheader_offset: u64 = endian.parse(read_bytes(r)?);
2663 let ztrailer_offset: u64 = endian.parse(read_bytes(r)?);
2664 let ztrailer_len: u64 = endian.parse(read_bytes(r)?);
2675 #[derive(Clone, Debug)]
2676 pub struct ZTrailer {
2677 /// File offset to the start of the record.
2680 /// Compression bias as a negative integer, e.g. -100.
2683 /// Always observed as zero.
2686 /// Uncompressed size of each block, except possibly the last. Only
2687 /// `0x3ff000` has been observed so far.
2688 pub block_size: u32,
2690 /// Block descriptors, always `(ztrailer_len - 24) / 24)` of them.
2691 pub blocks: Vec<ZBlock>,
2694 #[derive(Clone, Debug)]
2696 /// Offset of block of data if simple compression were used.
2697 pub uncompressed_ofs: u64,
2699 /// Actual offset within the file of the compressed data block.
2700 pub compressed_ofs: u64,
2702 /// The number of bytes in this data block after decompression. This is
2703 /// `block_size` in every data block but the last, which may be smaller.
2704 pub uncompressed_size: u32,
2706 /// The number of bytes in this data block, as stored compressed in this
2708 pub compressed_size: u32,
2712 fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<ZBlock, Error> {
2714 uncompressed_ofs: endian.parse(read_bytes(r)?),
2715 compressed_ofs: endian.parse(read_bytes(r)?),
2716 uncompressed_size: endian.parse(read_bytes(r)?),
2717 compressed_size: endian.parse(read_bytes(r)?),
2723 fn read<R: Read + Seek>(
2728 ) -> Result<Option<ZTrailer>, Error> {
2729 let start_offset = reader.stream_position()?;
2730 if reader.seek(SeekFrom::Start(ztrailer_ofs)).is_err() {
2733 let int_bias = endian.parse(read_bytes(reader)?);
2734 let zero = endian.parse(read_bytes(reader)?);
2735 let block_size = endian.parse(read_bytes(reader)?);
2736 let n_blocks: u32 = endian.parse(read_bytes(reader)?);
2737 let expected_n_blocks = (ztrailer_len - 24) / 24;
2738 if n_blocks as u64 != expected_n_blocks {
2739 return Err(Error::BadZlibTrailerNBlocks {
2740 offset: ztrailer_ofs,
2746 let blocks = (0..n_blocks)
2747 .map(|_| ZBlock::read(reader, endian))
2748 .collect::<Result<Vec<_>, _>>()?;
2749 reader.seek(SeekFrom::Start(start_offset))?;
2751 offset: ztrailer_ofs,
2760 fn try_read_bytes<const N: usize, R: Read>(r: &mut R) -> Result<Option<[u8; N]>, IoError> {
2761 let mut buf = [0; N];
2762 let n = r.read(&mut buf)?;
2765 r.read_exact(&mut buf[n..])?;
2773 fn read_bytes<const N: usize, R: Read>(r: &mut R) -> Result<[u8; N], IoError> {
2774 let mut buf = [0; N];
2775 r.read_exact(&mut buf)?;
2779 fn read_vec<R: Read>(r: &mut R, n: usize) -> Result<Vec<u8>, IoError> {
2780 let mut vec = vec![0; n];
2781 r.read_exact(&mut vec)?;
2785 fn read_string<R: Read>(r: &mut R, endian: Endian) -> Result<RawString, IoError> {
2786 let length: u32 = endian.parse(read_bytes(r)?);
2787 Ok(read_vec(r, length as usize)?.into())
2790 #[derive(Clone, Debug)]
2791 pub struct LongStringValueLabels<N, S>
2798 /// `(value, label)` pairs, where each value is `width` bytes.
2799 pub labels: Vec<(S, S)>,
2802 impl LongStringValueLabels<RawString, RawString> {
2806 ) -> Result<LongStringValueLabels<Identifier, String>, Warning> {
2807 let var_name = decoder.decode(&self.var_name);
2808 let var_name = Identifier::new(var_name.trim_end(), decoder.encoding)
2809 .map_err(Warning::InvalidLongStringValueLabelName)?;
2811 let mut labels = Vec::with_capacity(self.labels.len());
2812 for (value, label) in self.labels.iter() {
2813 let value = decoder.decode_exact_length(&value.0).to_string();
2814 let label = decoder.decode(label).to_string();
2815 labels.push((value, label));
2818 Ok(LongStringValueLabels {
2826 #[derive(Clone, Debug)]
2827 pub struct LongStringValueLabelRecord<N, S>(pub Vec<LongStringValueLabels<N, S>>)
2832 impl ExtensionRecord for LongStringValueLabelRecord<RawString, RawString> {
2833 const SUBTYPE: u32 = 21;
2834 const SIZE: Option<u32> = Some(1);
2835 const COUNT: Option<u32> = None;
2836 const NAME: &'static str = "long string value labels record";
2838 fn parse(ext: &Extension, endian: Endian) -> Result<Record, Warning> {
2839 ext.check_size::<Self>()?;
2841 let mut input = &ext.data[..];
2842 let mut label_set = Vec::new();
2843 while !input.is_empty() {
2844 let var_name = read_string(&mut input, endian)?;
2845 let width: u32 = endian.parse(read_bytes(&mut input)?);
2846 let n_labels: u32 = endian.parse(read_bytes(&mut input)?);
2847 let mut labels = Vec::new();
2848 for _ in 0..n_labels {
2849 let value = read_string(&mut input, endian)?;
2850 let label = read_string(&mut input, endian)?;
2851 labels.push((value, label));
2853 label_set.push(LongStringValueLabels {
2859 Ok(Record::LongStringValueLabels(LongStringValueLabelRecord(
2865 impl LongStringValueLabelRecord<RawString, RawString> {
2866 fn decode(self, decoder: &Decoder) -> LongStringValueLabelRecord<Identifier, String> {
2867 let mut labels = Vec::with_capacity(self.0.len());
2868 for label in &self.0 {
2869 match label.decode(decoder) {
2870 Ok(set) => labels.push(set),
2871 Err(error) => decoder.warn(error),
2874 LongStringValueLabelRecord(labels)