- pub fn check_width_compatibility(self, variable: Option<&str>, w: Width) -> Result<Self, Error> {
- self.format.check_type_compatibility(variable, self.var_type())?;
- let expected_width = self.var_width();
- if w != expected_width {
- let bad_spec = self;
- let good_spec = if self.format == Format::A {
- Spec { w, ..self }
- } else {
- Spec { w: w * 2, ..self }
- };
- if let Some(variable) = variable {
- Err(Error::NamedStringVariableBadSpecWidth {
- variable: variable.into(),
- width: w,
- bad_spec,
- good_spec,
- })
- } else {
- Err(Error::UnnamedStringVariableBadSpecWidth {
- width: w,
- bad_spec,
- good_spec,
- })
+ /// Checks whether this format specification is valid for a variable with
+ /// width `var_width`.
+ pub fn check_width_compatibility(
+ self,
+ variable: Option<&str>,
+ var_width: VarWidth,
+ ) -> Result<Self, Error> {
+ // Verify that the format is right for the variable's type.
+ self.format
+ .check_type_compatibility(variable, var_width.into())?;
+
+ if let VarWidth::String(w) = var_width {
+ if var_width != self.var_width() {
+ let bad_spec = self;
+ let good_spec = if self.format == Format::A {
+ Spec { w, ..self }
+ } else {
+ Spec { w: w * 2, ..self }
+ };
+ if let Some(variable) = variable {
+ return Err(Error::NamedStringVariableBadSpecWidth {
+ variable: variable.into(),
+ width: w,
+ bad_spec,
+ good_spec,
+ });
+ } else {
+ return Err(Error::UnnamedStringVariableBadSpecWidth {
+ width: w,
+ bad_spec,
+ good_spec,
+ });
+ }