#[derive(ThisError, Debug)]
pub enum Error {
- #[error("Unknown format type {value}")]
+ #[error("Unknown format type {value}.")]
UnknownFormat { value: u16 },
#[error("Output format {0} specifies width {}, but {} requires an even width.", .0.w, .0.format)]
#[error("Numeric variable is not compatible with string format {0}.")]
UnnamedVariableNotCompatibleWithStringFormat(Format),
- #[error("String variable {variable} is not compatible with numeric format {format}.")]
- NamedVariableNotCompatibleWithNumericFormat { variable: String, format: Format },
-
- #[error("Numeric variable {variable} is not compatible with string format {format}.")]
- NamedVariableNotCompatibleWithStringFormat { variable: String, format: Format },
-
#[error("String variable {variable} with width {width} is not compatible with format {bad_spec}. Use format {good_spec} instead.")]
NamedStringVariableBadSpecWidth {
variable: String,
/// Checks whether this format is valid for a variable with the given
/// `var_type`.
- pub fn check_type_compatibility(
- self,
- variable: Option<&str>,
- var_type: VarType,
- ) -> Result<(), Error> {
+ pub fn check_type_compatibility(self, var_type: VarType) -> Result<(), Error> {
let my_type = self.var_type();
match (my_type, var_type) {
(VarType::Numeric, VarType::String) => {
- if let Some(variable) = variable {
- Err(Error::NamedVariableNotCompatibleWithNumericFormat {
- variable: variable.into(),
- format: self,
- })
- } else {
- Err(Error::UnnamedVariableNotCompatibleWithNumericFormat(self))
- }
+ Err(Error::UnnamedVariableNotCompatibleWithNumericFormat(self))
}
(VarType::String, VarType::Numeric) => {
- if let Some(variable) = variable {
- Err(Error::NamedVariableNotCompatibleWithStringFormat {
- variable: variable.into(),
- format: self,
- })
- } else {
- Err(Error::UnnamedVariableNotCompatibleWithStringFormat(self))
- }
+ Err(Error::UnnamedVariableNotCompatibleWithStringFormat(self))
}
_ => Ok(()),
}
/// 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> {
+ pub fn check_width_compatibility(self, 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())?;
+ .check_type_compatibility(var_width.into())?;
if let VarWidth::String(w) = var_width {
if var_width != self.var_width() {
} 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,
- });
- }
+ return Err(Error::UnnamedStringVariableBadSpecWidth {
+ width: w,
+ bad_spec,
+ good_spec,
+ });
}
}