Skip to content

Commit adedba4

Browse files
committed
Use impl_enum_decodable for SeparatorTactic
1 parent 55f2de9 commit adedba4

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

src/lib.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ mod visitor;
5353
mod items;
5454
mod missed_spans;
5555
mod lists;
56+
#[macro_use]
5657
mod utils;
5758
mod types;
5859
mod expr;
@@ -64,23 +65,6 @@ const SKIP_ANNOTATION: &'static str = "rustfmt_skip";
6465

6566
static mut CONFIG: Option<config::Config> = None;
6667

67-
// Macro for deriving implementations of Decodable for enums
68-
macro_rules! impl_enum_decodable {
69-
( $e:ident, $( $x:ident ),* ) => {
70-
impl Decodable for $e {
71-
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
72-
let s = try!(d.read_str());
73-
match &*s {
74-
$(
75-
stringify!($x) => Ok($e::$x),
76-
)*
77-
_ => Err(d.error("Bad variant")),
78-
}
79-
}
80-
}
81-
};
82-
}
83-
8468
#[derive(Copy, Clone)]
8569
pub enum WriteMode {
8670
Overwrite,

src/lists.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,7 @@ pub enum SeparatorTactic {
3030
Vertical,
3131
}
3232

33-
// TODO could use a macro for all these Decodable impls.
34-
impl Decodable for SeparatorTactic {
35-
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
36-
let s = try!(d.read_str());
37-
match &*s {
38-
"Always" => Ok(SeparatorTactic::Always),
39-
"Never" => Ok(SeparatorTactic::Never),
40-
"Vertical" => Ok(SeparatorTactic::Vertical),
41-
_ => Err(d.error("Bad variant")),
42-
}
43-
}
44-
}
33+
impl_enum_decodable!(SeparatorTactic, Always, Never, Vertical);
4534

4635
// TODO having some helpful ctors for ListFormatting would be nice.
4736
pub struct ListFormatting<'a> {

src/utils.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,22 @@ pub fn format_visibility(vis: Visibility) -> &'static str {
4747
Visibility::Inherited => ""
4848
}
4949
}
50+
51+
// Macro for deriving implementations of Decodable for enums
52+
#[macro_export]
53+
macro_rules! impl_enum_decodable {
54+
( $e:ident, $( $x:ident ),* ) => {
55+
impl Decodable for $e {
56+
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
57+
let s = try!(d.read_str());
58+
match &*s {
59+
$(
60+
stringify!($x) => Ok($e::$x),
61+
)*
62+
_ => Err(d.error("Bad variant")),
63+
}
64+
}
65+
}
66+
};
67+
}
68+

0 commit comments

Comments
 (0)