Merge pull request #98 from cassiersg/misc

Use impl_enum_decodable for SeparatorTactic
This commit is contained in:
Nick Cameron 2015-06-09 08:43:22 +12:00
commit 58519236d0
3 changed files with 21 additions and 30 deletions

View file

@ -48,12 +48,13 @@ use visitor::FmtVisitor;
#[macro_use]
mod config;
#[macro_use]
mod utils;
mod changes;
mod visitor;
mod items;
mod missed_spans;
mod lists;
mod utils;
mod types;
mod expr;
mod imports;
@ -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,21 @@ 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")),
}
}
}
};
}