},
#[error("At offset {offset:#x}, {record} has bad size {size} bytes instead of the expected {expected_size}.")]
- BadRecordSize { offset: u64, record: String, size: u32, expected_size: u32 },
+ BadRecordSize {
+ offset: u64,
+ record: String,
+ size: u32,
+ expected_size: u32,
+ },
#[error("At offset {offset:#x}, {record} has bad count {count} instead of the expected {expected_count}.")]
- BadRecordCount { offset: u64, record: String, count: u32, expected_count: u32 },
+ BadRecordCount {
+ offset: u64,
+ record: String,
+ count: u32,
+ expected_count: u32,
+ },
#[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.")]
- BadLongMissingValueLength { record_offset: u64, offset: u64, value_len: u32 },
+ BadLongMissingValueLength {
+ record_offset: u64,
+ offset: u64,
+ value_len: u32,
+ },
#[error("The encoding record at offset {offset:#x} contains an encoding name that is not valid UTF-8.")]
BadEncodingName { offset: u64 },
#[derive(Clone, Debug)]
pub enum Record {
- Header(Header),
- Variable(Variable),
- ValueLabel(ValueLabel),
- VarIndexes(VarIndexes),
- Document(Document),
- IntegerInfo(IntegerInfo),
- FloatInfo(FloatInfo),
+ Header(HeaderRecord),
+ Variable(VariableRecord),
+ ValueLabel(ValueLabelRecord),
+ VarIndexes(VarIndexRecord),
+ Document(DocumentRecord),
+ IntegerInfo(IntegerInfoRecord),
+ FloatInfo(FloatInfoRecord),
VariableSets(UnencodedString),
VarDisplay(VarDisplayRecord),
MultipleResponse(MultipleResponseRecord),
LongStrings(UnencodedString),
FileAttributes(UnencodedString),
VariableAttributes(UnencodedString),
- TextExtension(TextExtension),
OtherExtension(Extension),
EndOfHeaders(u32),
ZHeader(ZHeader),
fn read<R: Read + Seek>(reader: &mut R, endian: Endian) -> Result<Record, Error> {
let rec_type: u32 = endian.parse(read_bytes(reader)?);
match rec_type {
- 2 => Ok(Record::Variable(Variable::read(reader, endian)?)),
- 3 => Ok(Record::ValueLabel(ValueLabel::read(reader, endian)?)),
- 4 => Ok(Record::VarIndexes(VarIndexes::read(reader, endian)?)),
- 6 => Ok(Record::Document(Document::read(reader, endian)?)),
+ 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)?))),
_ => Err(Error::BadRecordType {
}
#[derive(Clone)]
-pub struct Header {
+pub struct HeaderRecord {
/// Magic number.
pub magic: Magic,
pub endian: Endian,
}
-impl Header {
+impl HeaderRecord {
fn debug_field<T: Debug>(&self, f: &mut Formatter, name: &str, value: T) -> FmtResult {
writeln!(f, "{name:>17}: {:?}", value)
}
}
-impl Debug for Header {
+impl Debug for HeaderRecord {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
writeln!(f, "File header record:")?;
self.debug_field(f, "Magic", self.magic)?;
}
}
-impl Header {
- fn read<R: Read>(r: &mut R) -> Result<Header, Error> {
+impl HeaderRecord {
+ fn read<R: Read>(r: &mut R) -> Result<HeaderRecord, Error> {
let magic: [u8; 4] = read_bytes(r)?;
let magic: Magic = magic.try_into().map_err(|_| Error::NotASystemFile)?;
let file_label = UnencodedStr::<64>(read_bytes(r)?);
let _: [u8; 3] = read_bytes(r)?;
- Ok(Header {
+ Ok(HeaderRecord {
magic,
layout_code,
nominal_case_size,
mod state {
use super::{
- Compression, Error, Header, Record, Value, VarType, Variable, ZHeader, ZTrailer,
- ZlibDecodeMultiple,
+ Compression, Error, HeaderRecord, Record, Value, VarType, VariableRecord, ZHeader,
+ ZTrailer, ZlibDecodeMultiple,
};
use crate::endian::Endian;
use std::{
impl<R: Read + Seek + 'static> State for Start<R> {
fn read(mut self: Box<Self>) -> Result<Option<(Record, Box<dyn State>)>, Error> {
- let header = Header::read(&mut self.reader)?;
+ let header = HeaderRecord::read(&mut self.reader)?;
let next_state = Headers(CommonState {
reader: self.reader,
endian: header.endian,
fn read(mut self: Box<Self>) -> Result<Option<(Record, Box<dyn State>)>, Error> {
let record = Record::read(&mut self.0.reader, self.0.endian)?;
match record {
- Record::Variable(Variable { width, .. }) => {
+ Record::Variable(VariableRecord { width, .. }) => {
self.0.var_types.push(VarType::from_width(width));
}
Record::EndOfHeaders(_) => {
impl Value {
fn read<R: Read>(r: &mut R, var_type: VarType, endian: Endian) -> Result<Value, IoError> {
- Ok(Self::from_raw(UntypedValue(read_bytes(r)?), var_type, endian))
+ Ok(Self::from_raw(
+ UntypedValue(read_bytes(r)?),
+ var_type,
+ endian,
+ ))
}
pub fn from_raw(raw: UntypedValue, var_type: VarType, endian: Endian) -> Value {
});
}
}
- 253 => break Value::from_raw(UntypedValue(read_bytes(reader)?), var_type, endian),
+ 253 => {
+ break Value::from_raw(UntypedValue(read_bytes(reader)?), var_type, endian)
+ }
254 => match var_type {
VarType::String => break Value::String(UnencodedStr(*b" ")), // XXX EBCDIC
VarType::Numeric => {
39 => "SDATE",
40 => "MTIME",
41 => "YMDHMS",
- _ => return format!("<unknown format {type_}>").into()
- }.into()
+ _ => return format!("<unknown format {type_}>").into(),
+ }
+ .into()
}
#[derive(Clone)]
}
#[derive(Clone)]
-pub struct Variable {
+pub struct VariableRecord {
/// Offset from the start of the file to the start of the record.
pub offset: u64,
pub label: Option<UnencodedString>,
}
-impl Debug for Variable {
+impl Debug for VariableRecord {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
writeln!(
f,
}
}
-impl Variable {
- fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<Variable, Error> {
+impl VariableRecord {
+ fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<VariableRecord, Error> {
let offset = r.stream_position()?;
let width: i32 = endian.parse(read_bytes(r)?);
let has_variable_label: u32 = endian.parse(read_bytes(r)?);
let missing_values = MissingValues::read(r, offset, width, missing_value_code, endian)?;
- Ok(Variable {
+ Ok(VariableRecord {
offset,
width,
name,
}
#[derive(Clone)]
-pub struct ValueLabel {
+pub struct ValueLabelRecord {
/// Offset from the start of the file to the start of the record.
pub offset: u64,
pub labels: Vec<(UntypedValue, UnencodedString)>,
}
-impl Debug for ValueLabel {
+impl Debug for ValueLabelRecord {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
for (value, label) in self.labels.iter() {
writeln!(f, "{value:?}: {label:?}")?;
}
}
-impl ValueLabel {
+impl ValueLabelRecord {
/// Maximum number of value labels in a record.
pub const MAX: u32 = u32::MAX / 8;
- fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<ValueLabel, Error> {
+ fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<ValueLabelRecord, Error> {
let offset = r.stream_position()?;
let n: u32 = endian.parse(read_bytes(r)?);
- if n > ValueLabel::MAX {
+ if n > ValueLabelRecord::MAX {
return Err(Error::BadNumberOfValueLabels {
offset,
n,
- max: ValueLabel::MAX,
+ max: ValueLabelRecord::MAX,
});
}
label.truncate(label_len);
labels.push((value, UnencodedString(label)));
}
- Ok(ValueLabel { offset, labels })
+ Ok(ValueLabelRecord { offset, labels })
}
}
#[derive(Clone)]
-pub struct VarIndexes {
+pub struct VarIndexRecord {
/// Offset from the start of the file to the start of the record.
pub offset: u64,
pub dict_indexes: Vec<u32>,
}
-impl Debug for VarIndexes {
+impl Debug for VarIndexRecord {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(f, "apply to variables")?;
for dict_index in self.dict_indexes.iter() {
}
}
-impl VarIndexes {
+impl VarIndexRecord {
/// Maximum number of variable indexes in a record.
pub const MAX: u32 = u32::MAX / 8;
- fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<VarIndexes, Error> {
+ fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<VarIndexRecord, Error> {
let offset = r.stream_position()?;
let n: u32 = endian.parse(read_bytes(r)?);
- if n > VarIndexes::MAX {
+ if n > VarIndexRecord::MAX {
return Err(Error::BadNumberOfVarIndexes {
offset,
n,
- max: VarIndexes::MAX,
+ max: VarIndexRecord::MAX,
});
}
let mut dict_indexes = Vec::with_capacity(n as usize);
dict_indexes.push(endian.parse(read_bytes(r)?));
}
- Ok(VarIndexes {
+ Ok(VarIndexRecord {
offset,
dict_indexes,
})
}
#[derive(Clone, Debug)]
-pub struct Document {
+pub struct DocumentRecord {
/// Offset from the start of the file to the start of the record.
pub pos: u64,
/// The document, as an array of 80-byte lines.
- pub lines: Vec<DocumentLine>
+ pub lines: Vec<DocumentLine>,
}
-pub type DocumentLine = UnencodedStr<{Document::LINE_LEN}>;
+pub type DocumentLine = UnencodedStr<{ DocumentRecord::LINE_LEN }>;
-impl Document {
+impl DocumentRecord {
/// Length of a line in a document. Document lines are fixed-length and
/// padded on the right with spaces.
pub const LINE_LEN: usize = 80;
/// the maximum number that will fit in a 32-bit space.
pub const MAX_LINES: usize = i32::MAX as usize / Self::LINE_LEN;
- fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<Document, Error> {
+ fn read<R: Read + Seek>(r: &mut R, endian: Endian) -> Result<DocumentRecord, Error> {
let offset = r.stream_position()?;
let n: u32 = endian.parse(read_bytes(r)?);
let n = n as usize;
let pos = r.stream_position()?;
let mut lines = Vec::with_capacity(n);
for _ in 0..n {
- lines.push(UnencodedStr::<{Document::LINE_LEN}>(read_bytes(r)?));
+ lines.push(UnencodedStr::<{ DocumentRecord::LINE_LEN }>(read_bytes(r)?));
}
- Ok(Document { pos, lines })
+ Ok(DocumentRecord { pos, lines })
}
}
}
}
#[derive(Clone, Debug)]
-pub struct IntegerInfo {
+pub struct IntegerInfoRecord {
pub version: (i32, i32, i32),
pub machine_code: i32,
pub floating_point_rep: i32,
pub character_code: i32,
}
-impl ExtensionRecord for IntegerInfo {
+impl ExtensionRecord for IntegerInfoRecord {
const SUBTYPE: u32 = 3;
const SIZE: Option<u32> = Some(4);
const COUNT: Option<u32> = Some(8);
let data: Vec<i32> = (0..8)
.map(|_| endian.parse(read_bytes(&mut input).unwrap()))
.collect();
- Ok(IntegerInfo {
+ Ok(IntegerInfoRecord {
version: (data[0], data[1], data[2]),
machine_code: data[3],
floating_point_rep: data[4],
}
#[derive(Clone, Debug)]
-pub struct FloatInfo {
+pub struct FloatInfoRecord {
pub sysmis: f64,
pub highest: f64,
pub lowest: f64,
}
-impl ExtensionRecord for FloatInfo {
+impl ExtensionRecord for FloatInfoRecord {
const SUBTYPE: u32 = 4;
const SIZE: Option<u32> = Some(8);
const COUNT: Option<u32> = Some(3);
let data: Vec<f64> = (0..3)
.map(|_| endian.parse(read_bytes(&mut input).unwrap()))
.collect();
- Ok(FloatInfo {
+ Ok(FloatInfoRecord {
sysmis: data[0],
highest: data[1],
lowest: data[2],
}
}
-
#[derive(Clone, Debug)]
pub struct NumberOfCasesRecord {
/// Always observed as 1.
}
}
-#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
-pub enum TextExtensionSubtype {
- VariableSets = 5,
- ProductInfo = 10,
- LongNames = 13,
- LongStrings = 14,
- FileAttributes = 17,
- VariableAttributes = 18,
-}
-
-#[derive(Clone, Debug)]
-pub struct TextExtension {
- pub subtype: TextExtensionSubtype,
- pub string: UnencodedString,
-}
-
#[derive(Clone, Debug)]
pub struct Extension {
/// Offset from the start of the file to the start of the record.
data,
};
match subtype {
- IntegerInfo::SUBTYPE => Ok(Record::IntegerInfo(IntegerInfo::parse(
+ IntegerInfoRecord::SUBTYPE => Ok(Record::IntegerInfo(IntegerInfoRecord::parse(
&extension,
endian,
|_| (),
)?)),
- FloatInfo::SUBTYPE => Ok(Record::FloatInfo(FloatInfo::parse(
+ FloatInfoRecord::SUBTYPE => Ok(Record::FloatInfo(FloatInfoRecord::parse(
&extension,
endian,
|_| (),
endian,
|_| (),
)?)),
- x if x == TextExtensionSubtype::VariableSets as u32 => {
- Ok(Record::VariableSets(UnencodedString(extension.data)))
- }
- x if x == TextExtensionSubtype::ProductInfo as u32 => {
- Ok(Record::ProductInfo(UnencodedString(extension.data)))
- }
- x if x == TextExtensionSubtype::LongNames as u32 => {
- Ok(Record::LongNames(UnencodedString(extension.data)))
- }
- x if x == TextExtensionSubtype::LongStrings as u32 => {
- Ok(Record::LongStrings(UnencodedString(extension.data)))
- }
- x if x == TextExtensionSubtype::FileAttributes as u32 => {
- Ok(Record::FileAttributes(UnencodedString(extension.data)))
- }
- x if x == TextExtensionSubtype::VariableAttributes as u32 => {
- Ok(Record::VariableAttributes(UnencodedString(extension.data)))
- }
+ 5 => Ok(Record::VariableSets(UnencodedString(extension.data))),
+ 10 => Ok(Record::ProductInfo(UnencodedString(extension.data))),
+ 13 => Ok(Record::LongNames(UnencodedString(extension.data))),
+ 14 => Ok(Record::LongStrings(UnencodedString(extension.data))),
+ 17 => Ok(Record::FileAttributes(UnencodedString(extension.data))),
+ 18 => Ok(Record::VariableAttributes(UnencodedString(extension.data))),
_ => Ok(Record::OtherExtension(extension)),
}
}
Ok(LongStringValueLabelRecord(label_set))
}
}
-