Skip to content

Commit 5851923

Browse files
committed
Merge pull request #98 from cassiersg/misc
Use impl_enum_decodable for SeparatorTactic
2 parents 81c581e + 4d6d0b8 commit 5851923

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

src/lib.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ use visitor::FmtVisitor;
4848

4949
#[macro_use]
5050
mod config;
51+
#[macro_use]
52+
mod utils;
5153
mod changes;
5254
mod visitor;
5355
mod items;
5456
mod missed_spans;
5557
mod lists;
56-
mod utils;
5758
mod types;
5859
mod expr;
5960
mod imports;
@@ -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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,21 @@ 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+
}

0 commit comments

Comments
 (0)