Skip to content

Use impl_enum_decodable for SeparatorTactic #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
13 changes: 1 addition & 12 deletions src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
18 changes: 18 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
}
}
}
};
}