Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 20bdb2f

Browse files
committed
Use config_type proc macro
1 parent ee02cdf commit 20bdb2f

File tree

3 files changed

+43
-147
lines changed

3 files changed

+43
-147
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config/lists.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Configuration options related to rewriting a list.
22
3-
use crate::config::config_type::ConfigType;
3+
use config_proc_macro::config_type;
4+
45
use crate::config::IndentStyle;
56

67
/// The definitive formatting tactic for lists.
@@ -25,7 +26,7 @@ impl DefinitiveListTactic {
2526
/// Formatting tactic for lists. This will be cast down to a
2627
/// `DefinitiveListTactic` depending on the number and length of the items and
2728
/// their comments.
28-
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
29+
#[config_type]
2930
pub enum ListTactic {
3031
// One item per row.
3132
Vertical,
@@ -39,17 +40,13 @@ pub enum ListTactic {
3940
Mixed,
4041
}
4142

42-
impl_enum_serialize_and_deserialize!(ListTactic, Vertical, Horizontal, HorizontalVertical, Mixed);
43-
44-
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
43+
#[config_type]
4544
pub enum SeparatorTactic {
4645
Always,
4746
Never,
4847
Vertical,
4948
}
5049

51-
impl_enum_serialize_and_deserialize!(SeparatorTactic, Always, Never, Vertical);
52-
5350
impl SeparatorTactic {
5451
pub fn from_bool(b: bool) -> SeparatorTactic {
5552
if b {
@@ -61,14 +58,12 @@ impl SeparatorTactic {
6158
}
6259

6360
/// Where to put separator.
64-
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
61+
#[config_type]
6562
pub enum SeparatorPlace {
6663
Front,
6764
Back,
6865
}
6966

70-
impl_enum_serialize_and_deserialize!(SeparatorPlace, Front, Back);
71-
7267
impl SeparatorPlace {
7368
pub fn is_front(self) -> bool {
7469
self == SeparatorPlace::Front

src/config/options.rs

Lines changed: 36 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -3,133 +3,18 @@ use std::fmt;
33
use std::path::{Path, PathBuf};
44

55
use atty;
6+
use config_proc_macro::config_type;
67
use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
78

8-
use crate::config::config_type::ConfigType;
99
use crate::config::lists::*;
1010
use crate::config::Config;
1111

12-
/// Macro that will stringify the enum variants or a provided textual repr
13-
#[macro_export]
14-
macro_rules! configuration_option_enum_stringify {
15-
($variant:ident) => {
16-
stringify!($variant)
17-
};
18-
19-
($_variant:ident: $value:expr) => {
20-
stringify!($value)
21-
};
22-
}
23-
24-
/// Macro for deriving implementations of Serialize/Deserialize for enums
25-
#[macro_export]
26-
macro_rules! impl_enum_serialize_and_deserialize {
27-
( $e:ident, $( $variant:ident $(: $value:expr)* ),* ) => {
28-
impl ::serde::ser::Serialize for $e {
29-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
30-
where S: ::serde::ser::Serializer
31-
{
32-
use serde::ser::Error;
33-
34-
// We don't know whether the user of the macro has given us all options.
35-
#[allow(unreachable_patterns)]
36-
match *self {
37-
$(
38-
$e::$variant => serializer.serialize_str(
39-
configuration_option_enum_stringify!($variant $(: $value)*)
40-
),
41-
)*
42-
_ => {
43-
Err(S::Error::custom(format!("Cannot serialize {:?}", self)))
44-
}
45-
}
46-
}
47-
}
48-
49-
impl<'de> ::serde::de::Deserialize<'de> for $e {
50-
fn deserialize<D>(d: D) -> Result<Self, D::Error>
51-
where D: ::serde::Deserializer<'de> {
52-
use serde::de::{Error, Visitor};
53-
use std::marker::PhantomData;
54-
use std::fmt;
55-
struct StringOnly<T>(PhantomData<T>);
56-
impl<'de, T> Visitor<'de> for StringOnly<T>
57-
where T: ::serde::Deserializer<'de> {
58-
type Value = String;
59-
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
60-
formatter.write_str("string")
61-
}
62-
fn visit_str<E>(self, value: &str) -> Result<String, E> {
63-
Ok(String::from(value))
64-
}
65-
}
66-
let s = d.deserialize_string(StringOnly::<D>(PhantomData))?;
67-
$(
68-
if configuration_option_enum_stringify!($variant $(: $value)*)
69-
.eq_ignore_ascii_case(&s) {
70-
return Ok($e::$variant);
71-
}
72-
)*
73-
static ALLOWED: &'static[&str] = &[
74-
$(configuration_option_enum_stringify!($variant $(: $value)*),)*];
75-
Err(D::Error::unknown_variant(&s, ALLOWED))
76-
}
77-
}
78-
79-
impl ::std::str::FromStr for $e {
80-
type Err = &'static str;
81-
82-
fn from_str(s: &str) -> Result<Self, Self::Err> {
83-
$(
84-
if configuration_option_enum_stringify!($variant $(: $value)*)
85-
.eq_ignore_ascii_case(s) {
86-
return Ok($e::$variant);
87-
}
88-
)*
89-
Err("Bad variant")
90-
}
91-
}
92-
93-
impl ConfigType for $e {
94-
fn doc_hint() -> String {
95-
let mut variants = Vec::new();
96-
$(
97-
variants.push(
98-
configuration_option_enum_stringify!($variant $(: $value)*)
99-
);
100-
)*
101-
format!("[{}]", variants.join("|"))
102-
}
103-
}
104-
};
105-
}
106-
107-
macro_rules! configuration_option_enum {
108-
($e:ident: $( $name:ident $(: $value:expr)* ),+ $(,)*) => (
109-
#[derive(Copy, Clone, Eq, PartialEq)]
110-
pub enum $e {
111-
$( $name ),+
112-
}
113-
114-
impl ::std::fmt::Debug for $e {
115-
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
116-
f.write_str(match self {
117-
$(
118-
$e::$name => configuration_option_enum_stringify!($name $(: $value)*),
119-
)+
120-
})
121-
}
122-
}
123-
124-
impl_enum_serialize_and_deserialize!($e, $( $name $(: $value)* ),+);
125-
);
126-
}
127-
128-
configuration_option_enum! { NewlineStyle:
129-
Auto, // Auto-detect based on the raw source input
12+
#[config_type]
13+
pub enum NewlineStyle {
14+
Auto, // Auto-detect based on the raw source input
13015
Windows, // \r\n
131-
Unix, // \n
132-
Native, // \r\n in Windows, \n on other platforms
16+
Unix, // \n
17+
Native, // \r\n in Windows, \n on other platforms
13318
}
13419

13520
impl NewlineStyle {
@@ -188,15 +73,17 @@ impl NewlineStyle {
18873
}
18974
}
19075

191-
configuration_option_enum! { BraceStyle:
76+
#[config_type]
77+
pub enum BraceStyle {
19278
AlwaysNextLine,
19379
PreferSameLine,
19480
// Prefer same line except where there is a where-clause, in which case force
19581
// the brace to the next line.
19682
SameLineWhere,
19783
}
19884

199-
configuration_option_enum! { ControlBraceStyle:
85+
#[config_type]
86+
pub enum ControlBraceStyle {
20087
// K&R style, Rust community default
20188
AlwaysSameLine,
20289
// Stroustrup style
@@ -205,15 +92,17 @@ configuration_option_enum! { ControlBraceStyle:
20592
AlwaysNextLine,
20693
}
20794

208-
configuration_option_enum! { IndentStyle:
95+
#[config_type]
96+
pub enum IndentStyle {
20997
// First line on the same line as the opening brace, all lines aligned with
21098
// the first line.
21199
Visual,
212100
// First line is on a new line and all lines align with block indent.
213101
Block,
214102
}
215103

216-
configuration_option_enum! { Density:
104+
#[config_type]
105+
pub enum Density {
217106
// Fit as much on one line as possible.
218107
Compressed,
219108
// Use more lines.
@@ -222,14 +111,16 @@ configuration_option_enum! { Density:
222111
Vertical,
223112
}
224113

225-
configuration_option_enum! { TypeDensity:
114+
#[config_type]
115+
pub enum TypeDensity {
226116
// No spaces around "=" and "+"
227117
Compressed,
228118
// Spaces around " = " and " + "
229119
Wide,
230120
}
231121

232-
configuration_option_enum! { Heuristics:
122+
#[config_type]
123+
pub enum Heuristics {
233124
// Turn off any heuristics
234125
Off,
235126
// Turn on max heuristics
@@ -249,15 +140,17 @@ impl Density {
249140
}
250141
}
251142

252-
configuration_option_enum! { ReportTactic:
143+
#[config_type]
144+
pub enum ReportTactic {
253145
Always,
254146
Unnumbered,
255147
Never,
256148
}
257149

258150
// What Rustfmt should emit. Mostly corresponds to the `--emit` command line
259151
// option.
260-
configuration_option_enum! { EmitMode:
152+
#[config_type]
153+
pub enum EmitMode {
261154
// Emits to files.
262155
Files,
263156
// Writes the output to stdout.
@@ -275,7 +168,8 @@ configuration_option_enum! { EmitMode:
275168
}
276169

277170
// Client-preference for coloured output.
278-
configuration_option_enum! { Color:
171+
#[config_type]
172+
pub enum Color {
279173
// Always use color, whether it is a piped or terminal output
280174
Always,
281175
// Never use color
@@ -284,7 +178,8 @@ configuration_option_enum! { Color:
284178
Auto,
285179
}
286180

287-
configuration_option_enum! { Version:
181+
#[config_type]
182+
pub enum Version {
288183
// 1.x.y
289184
One,
290185
// 2.x.y
@@ -303,7 +198,8 @@ impl Color {
303198
}
304199

305200
// How chatty should Rustfmt be?
306-
configuration_option_enum! { Verbosity:
201+
#[config_type]
202+
pub enum Verbosity {
307203
// Emit more.
308204
Verbose,
309205
Normal,
@@ -474,9 +370,14 @@ pub trait CliOptions {
474370
}
475371

476372
// The edition of the compiler (RFC 2052)
477-
configuration_option_enum! { Edition:
478-
Edition2015: 2015,
479-
Edition2018: 2018,
373+
#[config_type]
374+
pub enum Edition {
375+
#[value = "2015"]
376+
#[doc_hint = "2015"]
377+
Edition2015,
378+
#[value = "2018"]
379+
#[doc_hint = "2018"]
380+
Edition2018,
480381
}
481382

482383
impl Default for Edition {

0 commit comments

Comments
 (0)