X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=rust%2Fsrc%2Fraw.rs;h=ac8b3a057027cc6649a39c99d294014102da7cec;hb=a8331d2f67af24ce1f9f5da99641b8d1cdc21300;hp=a8c7c858e9e0a766243226066023acd6bc1aead4;hpb=926ed34468e873fd82bc2999ca229cfb424e7fc0;p=pspp diff --git a/rust/src/raw.rs b/rust/src/raw.rs index a8c7c858e9..ac8b3a0570 100644 --- a/rust/src/raw.rs +++ b/rust/src/raw.rs @@ -55,6 +55,9 @@ pub enum Error { #[error("At offset {offset:#x}, number of value labels ({n}) is greater than the maximum number {max}.")] BadNumberOfValueLabels { offset: u64, n: u32, max: u32 }, + #[error("At offset {offset:#x}, following value label record, found record type {rec_type} instead of expected type 4 for variable index record")] + ExpectedVarIndexRecord { offset: u64, rec_type: u32 }, + #[error("At offset {offset:#x}, number of variables indexes ({n}) is greater than the maximum number ({max}).")] BadNumberOfVarIndexes { offset: u64, n: u32, max: u32 }, @@ -130,7 +133,6 @@ pub enum Record { Header(HeaderRecord), Variable(VariableRecord), ValueLabel(ValueLabelRecord), - VarIndexes(VarIndexRecord), Document(DocumentRecord), IntegerInfo(IntegerInfoRecord), FloatInfo(FloatInfoRecord), @@ -158,7 +160,6 @@ impl Record { match rec_type { 2 => Ok(Record::Variable(VariableRecord::read(reader, endian)?)), 3 => Ok(Record::ValueLabel(ValueLabelRecord::read(reader, endian)?)), - 4 => Ok(Record::VarIndexes(VarIndexRecord::read(reader, endian)?)), 6 => Ok(Record::Document(DocumentRecord::read(reader, endian)?)), 7 => Ok(Extension::read(reader, endian)?), 999 => Ok(Record::EndOfHeaders(endian.parse(read_bytes(reader)?))), @@ -997,34 +998,50 @@ impl Debug for UnencodedStr { #[derive(Clone)] pub struct ValueLabelRecord { - /// Offset from the start of the file to the start of the record. - pub offset: u64, + /// Offset from the start of the file to the start of the value label + /// record. + pub label_offset: u64, /// The labels. pub labels: Vec<(UntypedValue, UnencodedString)>, + + /// Offset from the start of the file to the start of the variable index + /// record. + pub index_offset: u64, + + /// The 1-based indexes of the variable indexes. + pub dict_indexes: Vec, } impl Debug for ValueLabelRecord { fn fmt(&self, f: &mut Formatter) -> FmtResult { + writeln!(f, "labels: ")?; for (value, label) in self.labels.iter() { writeln!(f, "{value:?}: {label:?}")?; } + write!(f, "apply to variables")?; + for dict_index in self.dict_indexes.iter() { + write!(f, " #{dict_index}")?; + } Ok(()) } } impl ValueLabelRecord { /// Maximum number of value labels in a record. - pub const MAX: u32 = u32::MAX / 8; + pub const MAX_LABELS: u32 = u32::MAX / 8; + + /// Maximum number of variable indexes in a record. + pub const MAX_INDEXES: u32 = u32::MAX / 8; fn read(r: &mut R, endian: Endian) -> Result { - let offset = r.stream_position()?; + let label_offset = r.stream_position()?; let n: u32 = endian.parse(read_bytes(r)?); - if n > ValueLabelRecord::MAX { + if n > Self::MAX_LABELS { return Err(Error::BadNumberOfValueLabels { - offset, + offset: label_offset, n, - max: ValueLabelRecord::MAX, + max: Self::MAX_LABELS, }); } @@ -1039,41 +1056,22 @@ impl ValueLabelRecord { label.truncate(label_len); labels.push((value, UnencodedString(label))); } - Ok(ValueLabelRecord { offset, labels }) - } -} -#[derive(Clone)] -pub struct VarIndexRecord { - /// Offset from the start of the file to the start of the record. - pub offset: u64, - - /// The 1-based indexes of the variable indexes. - pub dict_indexes: Vec, -} - -impl Debug for VarIndexRecord { - fn fmt(&self, f: &mut Formatter) -> FmtResult { - write!(f, "apply to variables")?; - for dict_index in self.dict_indexes.iter() { - write!(f, " #{dict_index}")?; + let index_offset = r.stream_position()?; + let rec_type: u32 = endian.parse(read_bytes(r)?); + if rec_type != 4 { + return Err(Error::ExpectedVarIndexRecord { + offset: index_offset, + rec_type, + }); } - Ok(()) - } -} -impl VarIndexRecord { - /// Maximum number of variable indexes in a record. - pub const MAX: u32 = u32::MAX / 8; - - fn read(r: &mut R, endian: Endian) -> Result { - let offset = r.stream_position()?; let n: u32 = endian.parse(read_bytes(r)?); - if n > VarIndexRecord::MAX { + if n > Self::MAX_INDEXES { return Err(Error::BadNumberOfVarIndexes { - offset, + offset: index_offset, n, - max: VarIndexRecord::MAX, + max: Self::MAX_INDEXES, }); } let mut dict_indexes = Vec::with_capacity(n as usize); @@ -1081,8 +1079,10 @@ impl VarIndexRecord { dict_indexes.push(endian.parse(read_bytes(r)?)); } - Ok(VarIndexRecord { - offset, + Ok(ValueLabelRecord { + label_offset, + labels, + index_offset, dict_indexes, }) }