-use std::{borrow::Cow, cmp::Ordering, collections::HashMap, iter::repeat};
+use std::{borrow::Cow, cmp::Ordering, collections::HashMap, iter::repeat, ops::Range};
use crate::{
encoding::{default_encoding, get_encoding, Error as EncodingError},
endian::Endian,
format::{Error as FormatError, Spec, UncheckedSpec},
identifier::{Error as IdError, Identifier},
- raw::{self, MissingValues, UnencodedStr, VarType},
+ raw::{self, UnencodedStr, VarType},
};
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use encoding_rs::{DecoderResult, Encoding};
#[error("Using default encoding {0}.")]
UsingDefaultEncoding(String),
- #[error("Variable record at offset {offset:#x} specifies width {width} not in valid range [-1,255).")]
- InvalidVariableWidth { offset: u64, width: i32 },
+ #[error("Variable record from offset {:x} to {:x} specifies width {width} not in valid range [-1,255).", offsets.start, offsets.end)]
+ InvalidVariableWidth { offsets: Range<u64>, width: i32 },
#[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.")]
InvalidLongMissingValueFormat,
VariableSets(VariableSetRecord),
VarDisplay(VarDisplayRecord),
MultipleResponse(MultipleResponseRecord),
+ LongStringMissingValues(LongStringMissingValuesRecord),
LongStringValueLabels(LongStringValueLabelRecord),
Encoding(EncodingRecord),
NumberOfCases(NumberOfCasesRecord),
output.push(Record::MultipleResponse(mrr))
}
}
+ raw::Record::LongStringMissingValues(ref input) => {
+ if let Some(mrr) = LongStringMissingValuesRecord::try_decode(&mut decoder, input, warn)? {
+ output.push(Record::LongStringMissingValues(mrr))
+ }
+ }
raw::Record::LongStringValueLabels(ref input) => {
if let Some(mrr) =
LongStringValueLabelRecord::try_decode(&mut decoder, input, warn)?
raw::Record::VeryLongStrings(ref input) => {
let s = decoder.decode_string_cow(&input.text.0, warn);
output.push(Record::VeryLongStrings(VeryLongStringRecord::parse(
- &mut decoder,
- &s,
- warn,
+ &decoder, &s, warn,
)?));
}
raw::Record::FileAttributes(ref input) => {
fn decode_exact_length<'a>(&self, input: &'a [u8]) -> Cow<'a, str> {
if let (s, false) = self.encoding.decode_without_bom_handling(input) {
// This is the common case. Usually there will be no errors.
- s.into()
+ s
} else {
// Unusual case. Don't bother to optimize it much.
let mut decoder = self.encoding.new_decoder_without_bom_handling();
pub label: Option<String>,
}
+#[derive(Clone, Debug)]
+pub struct MissingValues {
+ /// Individual missing values, up to 3 of them.
+ pub values: Vec<Value>,
+
+ /// Optional range of missing values.
+ pub range: Option<(Value, Value)>,
+}
+
+impl Decode<raw::MissingValues> for MissingValues {
+ fn decode(decoder: &Decoder, input: &raw::MissingValues, _warn: impl Fn(Error)) -> Self {
+ MissingValues {
+ values: input
+ .values
+ .iter()
+ .map(|value| Value::decode(value, decoder))
+ .collect(),
+ range: input
+ .range
+ .as_ref()
+ .map(|(low, high)| (Value::decode(low, decoder), Value::decode(high, decoder))),
+ }
+ }
+}
+
fn decode_format(raw: raw::Spec, width: VarWidth, warn: impl Fn(Spec, FormatError)) -> Spec {
UncheckedSpec::try_from(raw)
.and_then(Spec::try_from)
-1 => return Ok(None),
_ => {
return Err(Error::InvalidVariableWidth {
- offset: input.offset,
+ offsets: input.offsets.clone(),
width: input.width,
})
}
name,
print_format,
write_format,
- missing_values: input.missing_values.clone(),
+ missing_values: MissingValues::decode(decoder, &input.missing_values, warn),
label,
}))
}
}
impl Value {
- pub fn decode(raw: raw::Value, decoder: &Decoder) -> Self {
+ pub fn decode(raw: &raw::Value, decoder: &Decoder) -> Self {
match raw {
raw::Value::Number(x) => Value::Number(x.map(|x| x.into())),
raw::Value::String(s) => Value::String(decoder.decode_exact_length(&s.0).into()),
.map(|(value, label)| {
let label = decoder.decode_string(&label.0, &warn);
let value = Value::decode(
- raw::Value::from_raw(*value, var_type, decoder.endian),
- &decoder,
+ &raw::Value::from_raw(value, var_type, decoder.endian),
+ decoder,
);
ValueLabel { value, label }
})
impl LongName {
fn new(decoder: &mut Decoder, short_name: &str, long_name: &str) -> Result<LongName, Error> {
- let short_name = Identifier::new(short_name, decoder.encoding)
- .map_err(|e| Error::InvalidShortName(e))?;
+ let short_name =
+ Identifier::new(short_name, decoder.encoding).map_err(Error::InvalidShortName)?;
let long_name =
- Identifier::new(long_name, decoder.encoding).map_err(|e| Error::InvalidLongName(e))?;
+ Identifier::new(long_name, decoder.encoding).map_err(Error::InvalidLongName)?;
Ok(LongName {
short_name,
long_name,
let Some((short_name, length)) = input.split_once('=') else {
return Err(Error::TBD);
};
- let short_name = Identifier::new(short_name, decoder.encoding)
- .map_err(|e| Error::InvalidLongStringName(e))?;
+ let short_name =
+ Identifier::new(short_name, decoder.encoding).map_err(Error::InvalidLongStringName)?;
let length: u16 = length.parse().map_err(|_| Error::TBD)?;
if length > VarWidth::MAX_STRING {
return Err(Error::TBD);
}
- Ok(VeryLongString {
- short_name: short_name.into(),
- length,
- })
+ Ok(VeryLongString { short_name, length })
}
}
}
if let Some(rest) = rest.strip_prefix(')') {
let attribute = Identifier::new(name, decoder.encoding)
- .map_err(|e| Error::InvalidAttributeName(e))
+ .map_err(Error::InvalidAttributeName)
.warn_on_error(warn)
.map(|name| Attribute { name, values });
return Ok((attribute, rest));
};
let (attributes, rest) = AttributeSet::parse(decoder, rest, Some('/'), warn)?;
let var_attribute = Identifier::new(long_var_name, decoder.encoding)
- .map_err(|e| Error::InvalidAttributeVariableName(e))
+ .map_err(Error::InvalidAttributeVariableName)
.warn_on_error(warn)
.map(|name| VarAttributeSet {
long_var_name: name,
) -> Result<Self, Error> {
let mr_set_name = decoder
.decode_identifier(&input.name.0, warn)
- .map_err(|error| Error::InvalidMrSetName(error))?;
+ .map_err(Error::InvalidMrSetName)?;
let label = decoder.decode_string(&input.label.0, warn);
}
}
+#[derive(Clone, Debug)]
+pub struct LongStringMissingValues {
+ /// Variable name.
+ pub var_name: Identifier,
+
+ /// Missing values.
+ pub missing_values: MissingValues,
+}
+
+impl LongStringMissingValues {
+ fn decode(
+ decoder: &Decoder,
+ input: &raw::LongStringMissingValues,
+ warn: &impl Fn(Error),
+ ) -> Result<Self, Error> {
+ let var_name = decoder.decode_string(&input.var_name.0, warn);
+ let var_name = Identifier::new(var_name.trim_end(), decoder.encoding)
+ .map_err(Error::InvalidLongStringValueLabelName)?;
+
+ let missing_values = MissingValues::decode(decoder, &input.missing_values, warn);
+
+ Ok(LongStringMissingValues {
+ var_name,
+ missing_values
+ })
+ }
+}
+
+#[derive(Clone, Debug)]
+pub struct LongStringMissingValuesRecord(Vec<LongStringMissingValues>);
+
+impl TryDecode for LongStringMissingValuesRecord {
+ type Input = raw::LongStringMissingValueSet;
+
+ fn try_decode(
+ decoder: &mut Decoder,
+ input: &Self::Input,
+ warn: impl Fn(Error),
+ ) -> Result<Option<Self>, Error> {
+ let mut labels = Vec::with_capacity(input.0.len());
+ for label in &input.0 {
+ match LongStringMissingValues::decode(decoder, label, &warn) {
+ Ok(set) => labels.push(set),
+ Err(error) => warn(error),
+ }
+ }
+ Ok(Some(LongStringMissingValuesRecord(labels)))
+ }
+}
+
#[derive(Clone, Debug)]
pub struct LongStringValueLabels {
pub var_name: Identifier,
) -> Result<Self, Error> {
let var_name = decoder.decode_string(&input.var_name.0, warn);
let var_name = Identifier::new(var_name.trim_end(), decoder.encoding)
- .map_err(|e| Error::InvalidLongStringValueLabelName(e))?;
+ .map_err(Error::InvalidLongStringValueLabelName)?;
let min_width = 9;
let max_width = VarWidth::MAX_STRING;
if input.width < 9 || input.width > max_width as u32 {
return Err(Error::InvalidLongValueLabelWidth {
- name: var_name.into(),
+ name: var_name,
width: input.width,
min_width,
max_width,