Use impl_enum_decodable for SeparatorTactic

This commit is contained in:
Gaëtan Cassiers 2015-06-08 19:40:22 +02:00
parent 55f2de9cf1
commit adedba45a8
3 changed files with 21 additions and 29 deletions

View File

@ -53,6 +53,7 @@ mod visitor;
mod items;
mod missed_spans;
mod lists;
#[macro_use]
mod utils;
mod types;
mod expr;
@ -64,23 +65,6 @@ const SKIP_ANNOTATION: &'static str = "rustfmt_skip";
static mut CONFIG: Option<config::Config> = None;
// Macro for deriving implementations of Decodable for enums
macro_rules! impl_enum_decodable {
( $e:ident, $( $x:ident ),* ) => {
impl Decodable for $e {
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
let s = try!(d.read_str());
match &*s {
$(
stringify!($x) => Ok($e::$x),
)*
_ => Err(d.error("Bad variant")),
}
}
}
};
}
#[derive(Copy, Clone)]
pub enum WriteMode {
Overwrite,

View File

@ -30,18 +30,7 @@ pub enum SeparatorTactic {
Vertical,
}
// TODO could use a macro for all these Decodable impls.
impl Decodable for SeparatorTactic {
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
let s = try!(d.read_str());
match &*s {
"Always" => Ok(SeparatorTactic::Always),
"Never" => Ok(SeparatorTactic::Never),
"Vertical" => Ok(SeparatorTactic::Vertical),
_ => Err(d.error("Bad variant")),
}
}
}
impl_enum_decodable!(SeparatorTactic, Always, Never, Vertical);
// TODO having some helpful ctors for ListFormatting would be nice.
pub struct ListFormatting<'a> {

View File

@ -47,3 +47,22 @@ pub fn format_visibility(vis: Visibility) -> &'static str {
Visibility::Inherited => ""
}
}
// Macro for deriving implementations of Decodable for enums
#[macro_export]
macro_rules! impl_enum_decodable {
( $e:ident, $( $x:ident ),* ) => {
impl Decodable for $e {
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
let s = try!(d.read_str());
match &*s {
$(
stringify!($x) => Ok($e::$x),
)*
_ => Err(d.error("Bad variant")),
}
}
}
};
}