use thiserror::Error as ThisError;
-use crate::raw::VarType;
+use crate::raw::{VarType, self};
#[derive(ThisError, Debug)]
pub enum Error {
pub const MAX_STRING: Width = 32767;
-type Width = u16;
-type SignedWidth = i16;
+pub type Width = u16;
+pub type SignedWidth = i16;
-type Decimals = u8;
+pub type Decimals = u8;
impl Format {
pub fn max_width(self) -> Width {
self.d
}
+ pub fn default_for_width(w: Width) -> Self {
+ match w {
+ 0 => Spec { format: Format::F, w: 8, d: 2 },
+ _ => Spec { format: Format::A, w: w, d: 0 },
+ }
+ }
+
pub fn fixed_from(source: &UncheckedSpec) -> Self {
let UncheckedSpec { format, w, d } = *source;
let (min, max) = format.width_range().into_inner();
self.format.var_type()
}
- pub fn check_width_compatibility(self, variable: Option<&str>, w: Width) -> Result<(), Error> {
+ 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 {
})
}
} else {
- Ok(())
+ Ok(self)
}
}
}
pub d: Decimals,
}
-impl TryFrom<u32> for UncheckedSpec {
+impl TryFrom<raw::Spec> for UncheckedSpec {
type Error = Error;
- fn try_from(source: u32) -> Result<Self, Self::Error> {
- let raw_format = (source >> 16) as u16;
+ fn try_from(raw: raw::Spec) -> Result<Self, Self::Error> {
+ let raw = raw.0;
+ let raw_format = (raw >> 16) as u16;
let format = raw_format.try_into()?;
- let w = ((source >> 8) & 0xff) as Width;
- let d = (source & 0xff) as Decimals;
+ let w = ((raw >> 8) & 0xff) as Width;
+ let d = (raw & 0xff) as Decimals;
Ok(Self { format, w, d })
}
}