Skip to content

Commit

Permalink
Clean up the formatter error code
Browse files Browse the repository at this point in the history
  • Loading branch information
bim9262 committed Feb 16, 2023
1 parent 9fbe93c commit b4148ba
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 68 deletions.
94 changes: 26 additions & 68 deletions src/formatting/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,10 @@ impl Formatter for StrFormatter {
})
}
Value::Icon(icon) => Ok(icon.clone()), // No escaping
Value::Number { .. } => Err(Error::new_format(
"A number cannot be formatted with 'str' formatter",
)),
Value::Datetime(..) => Err(Error::new_format(
"A datetime cannot be formatted with 'str' formatter",
)),
Value::Flag => Err(Error::new_format(
"A flag cannot be formatted with 'str' formatter",
)),
other => Err(Error::new_format(format!(
"{} cannot be formatted with 'str' formatter",
other.type_name(),
))),
}
}

Expand All @@ -217,15 +212,10 @@ impl Formatter for PangoStrFormatter {
fn format(&self, val: &Value) -> Result<String> {
match val {
Value::Text(x) | Value::Icon(x) => Ok(x.clone()), // No escaping
Value::Number { .. } => Err(Error::new_format(
"A number cannot be formatted with 'str' formatter",
)),
Value::Datetime(..) => Err(Error::new_format(
"A datetime cannot be formatted with 'str' formatter",
)),
Value::Flag => Err(Error::new_format(
"A flag cannot be formatted with 'str' formatter",
)),
other => Err(Error::new_format(format!(
"{} cannot be formatted with 'str' formatter",
other.type_name(),
))),
}
}
}
Expand Down Expand Up @@ -253,18 +243,10 @@ impl Formatter for BarFormatter {
})
.collect())
}
Value::Text(_) => Err(Error::new_format(
"Text cannot be formatted with 'bar' formatter",
)),
Value::Icon(_) => Err(Error::new_format(
"An icon cannot be formatted with 'bar' formatter",
)),
Value::Datetime(..) => Err(Error::new_format(
"A datetime cannot be formatted with 'bar' formatter",
)),
Value::Flag => Err(Error::new_format(
"A flag cannot be formatted with 'bar' formatter",
)),
other => Err(Error::new_format(format!(
"{} cannot be formatted with 'bar' formatter",
other.type_name(),
))),
}
}
}
Expand Down Expand Up @@ -403,18 +385,10 @@ impl Formatter for EngFormatter {

Ok(retval)
}
Value::Text(_) => Err(Error::new_format(
"Text cannot be formatted with 'eng' formatter",
)),
Value::Icon(_) => Err(Error::new_format(
"An icon cannot be formatted with 'eng' formatter",
)),
Value::Datetime(..) => Err(Error::new_format(
"A datetime cannot be formatted with 'eng' formatter",
)),
Value::Flag => Err(Error::new_format(
"A flag cannot be formatted with 'eng' formatter",
)),
other => Err(Error::new_format(format!(
"{} cannot be formatted with 'eng' formatter",
other.type_name(),
))),
}
}
}
Expand All @@ -431,18 +405,10 @@ impl Formatter for FixFormatter {
// unit,
// icon,
} => Err(Error::new_format("'fix' formatter is not implemented yet")),
Value::Text(_) => Err(Error::new_format(
"Text cannot be formatted with 'fix' formatter",
)),
Value::Icon(_) => Err(Error::new_format(
"An icon cannot be formatted with 'fix' formatter",
)),
Value::Datetime(..) => Err(Error::new_format(
"A datetime cannot be formatted with 'fix' formatter",
)),
Value::Flag => Err(Error::new_format(
"A flag cannot be formatted with 'fix' formatter",
)),
other => Err(Error::new_format(format!(
"{} cannot be formatted with 'fix' formatter",
other.type_name(),
)))
}
}
}
Expand Down Expand Up @@ -493,18 +459,10 @@ impl Formatter for DatetimeFormatter {
.format_with_items(self.items.iter())
.to_string(),
}),
Value::Text(_) => Err(Error::new_format(
"Text cannot be formatted with 'fix' formatter",
)),
Value::Number { .. } => Err(Error::new_format(
"Number cannot be formatted with 'fix' formatter",
)),
Value::Icon(_) => Err(Error::new_format(
"An icon cannot be formatted with 'fix' formatter",
)),
Value::Flag => Err(Error::new_format(
"A flag cannot be formatted with 'fix' formatter",
)),
other => Err(Error::new_format(format!(
"{} cannot be formatted with 'datetime' formatter",
other.type_name(),
))),
}
}
}
Expand All @@ -515,10 +473,10 @@ pub struct FlagFormatter;
impl Formatter for FlagFormatter {
fn format(&self, val: &Value) -> Result<String> {
match val {
Value::Number { .. } | Value::Text(_) | Value::Icon(_) | Value::Datetime(..) => {
Value::Flag => Ok(String::new()),
_ => {
unreachable!()
}
Value::Flag => Ok(String::new()),
}
}
}
12 changes: 12 additions & 0 deletions src/formatting/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ pub enum ValueInner {
Flag,
}

impl ValueInner {
pub fn type_name(&self) -> &'static str {
match self {
ValueInner::Text(..) => "Text",
ValueInner::Icon(..) => "Icon",
ValueInner::Number { .. } => "Number",
ValueInner::Datetime(..) => "Datetime",
ValueInner::Flag => "Flag",
}
}
}

pub trait IntoF64 {
fn into_f64(self) -> f64;
}
Expand Down

0 comments on commit b4148ba

Please sign in to comment.