Commit 8a1b1856 authored by Paolo Borelli's avatar Paolo Borelli

attribute: implement fmt::Display

Instead of a custome to_str method
parent b6d1504c
Pipeline #29609 passed with stages
in 7 minutes and 44 seconds
extern crate phf; extern crate phf;
use std::fmt;
use std::str::FromStr; use std::str::FromStr;
include!(concat!(env!("OUT_DIR"), "/attributes-codegen.rs")); include!(concat!(env!("OUT_DIR"), "/attributes-codegen.rs"));
...@@ -12,15 +13,15 @@ impl FromStr for Attribute { ...@@ -12,15 +13,15 @@ impl FromStr for Attribute {
} }
} }
impl Attribute { impl fmt::Display for Attribute {
// This is horribly inefficient, but for now I'm too lazy to have a // This is horribly inefficient, but for now I'm too lazy to have a
// compile-time bijective mapping from attributes to names. Hopefully // compile-time bijective mapping from attributes to names. Hopefully
// this function is only called when *printing* errors, which, uh, // this function is only called when *printing* errors, which, uh,
// should not be done too often. // should not be done too often.
pub fn to_str(&self) -> &'static str { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (k, v) in ATTRIBUTES.entries() { for (k, v) in ATTRIBUTES.entries() {
if *v == *self { if *v == *self {
return k; return write!(f, "{}", k);
} }
} }
...@@ -44,8 +45,8 @@ mod tests { ...@@ -44,8 +45,8 @@ mod tests {
#[test] #[test]
fn converts_attributes_back_to_strings() { fn converts_attributes_back_to_strings() {
assert_eq!(Attribute::ClipPath.to_str(), "clip-path"); assert_eq!(Attribute::ClipPath.to_string(), "clip-path");
assert_eq!(Attribute::KernelUnitLength.to_str(), "kernelUnitLength"); assert_eq!(Attribute::KernelUnitLength.to_string(), "kernelUnitLength");
assert_eq!(Attribute::Offset.to_str(), "offset"); assert_eq!(Attribute::Offset.to_string(), "offset");
} }
} }
...@@ -57,14 +57,14 @@ impl fmt::Display for NodeError { ...@@ -57,14 +57,14 @@ impl fmt::Display for NodeError {
AttributeError::Parse(ref n) => write!( AttributeError::Parse(ref n) => write!(
f, f,
"error parsing value for attribute \"{}\": {}", "error parsing value for attribute \"{}\": {}",
self.attr.to_str(), self.attr.to_string(),
n.display n.display
), ),
AttributeError::Value(ref s) => write!( AttributeError::Value(ref s) => write!(
f, f,
"invalid value for attribute \"{}\": {}", "invalid value for attribute \"{}\": {}",
self.attr.to_str(), self.attr.to_string(),
s s
), ),
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment