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

Commit 04add0c

Browse files
scampitopecongiro
authored andcommitted
implement Display for enums with attribute config_type (rust-lang#3621)
1 parent 71fa794 commit 04add0c

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

config_proc_macro/src/item_enum.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn define_config_type_on_enum(em: &syn::ItemEnum) -> syn::Result<TokenStream
2323

2424
let impl_doc_hint = impl_doc_hint(&em.ident, &em.variants);
2525
let impl_from_str = impl_from_str(&em.ident, &em.variants);
26+
let impl_display = impl_display(&em.ident, &em.variants);
2627
let impl_serde = impl_serde(&em.ident, &em.variants);
2728
let impl_deserialize = impl_deserialize(&em.ident, &em.variants);
2829

@@ -31,6 +32,7 @@ pub fn define_config_type_on_enum(em: &syn::ItemEnum) -> syn::Result<TokenStream
3132
mod #mod_name {
3233
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
3334
pub #enum_token #ident #generics { #variants }
35+
#impl_display
3436
#impl_doc_hint
3537
#impl_from_str
3638
#impl_serde
@@ -68,6 +70,29 @@ fn impl_doc_hint(ident: &syn::Ident, variants: &Variants) -> TokenStream {
6870
}
6971
}
7072

73+
fn impl_display(ident: &syn::Ident, variants: &Variants) -> TokenStream {
74+
let vs = variants
75+
.iter()
76+
.filter(|v| is_unit(v))
77+
.map(|v| (config_value_of_variant(v), &v.ident));
78+
let match_patterns = fold_quote(vs, |(s, v)| {
79+
quote! {
80+
#ident::#v => write!(f, "{}", #s),
81+
}
82+
});
83+
quote! {
84+
use std::fmt;
85+
impl fmt::Display for #ident {
86+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
87+
match self {
88+
#match_patterns
89+
_ => unimplemented!(),
90+
}
91+
}
92+
}
93+
}
94+
}
95+
7196
fn impl_from_str(ident: &syn::Ident, variants: &Variants) -> TokenStream {
7297
let vs = variants
7398
.iter()

src/config/config_type.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,15 @@ macro_rules! create_config {
249249
}
250250
name_out.push_str(name_raw);
251251
name_out.push(' ');
252+
let mut default_str = format!("{}", $def);
253+
if default_str.is_empty() {
254+
default_str = String::from("\"\"");
255+
}
252256
writeln!(out,
253-
"{}{} Default: {:?}{}",
257+
"{}{} Default: {}{}",
254258
name_out,
255259
<$ty>::doc_hint(),
256-
$def,
260+
default_str,
257261
if !$stb { " (unstable)" } else { "" }).unwrap();
258262
$(
259263
writeln!(out, "{}{}", space_str, $dstring).unwrap();

src/config/file_lines.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! This module contains types and functions to support formatting specific line ranges.
22
3+
use itertools::Itertools;
34
use std::collections::HashMap;
45
use std::path::PathBuf;
56
use std::rc::Rc;
@@ -92,6 +93,12 @@ impl<'a> From<&'a LineRange> for Range {
9293
}
9394
}
9495

96+
impl fmt::Display for Range {
97+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98+
write!(f, "{}..{}", self.lo, self.hi)
99+
}
100+
}
101+
95102
impl Range {
96103
pub fn new(lo: usize, hi: usize) -> Range {
97104
Range { lo, hi }
@@ -149,6 +156,21 @@ impl Range {
149156
#[derive(Clone, Debug, Default, PartialEq)]
150157
pub struct FileLines(Option<HashMap<FileName, Vec<Range>>>);
151158

159+
impl fmt::Display for FileLines {
160+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161+
match &self.0 {
162+
None => write!(f, "None")?,
163+
Some(map) => {
164+
for (file_name, ranges) in map.iter() {
165+
write!(f, "{}: ", file_name)?;
166+
write!(f, "{}\n", ranges.iter().format(", "))?;
167+
}
168+
}
169+
};
170+
Ok(())
171+
}
172+
}
173+
152174
/// Normalizes the ranges so that the invariants for `FileLines` hold: ranges are non-overlapping,
153175
/// and ordered by their start point.
154176
fn normalize_ranges(ranges: &mut HashMap<FileName, Vec<Range>>) {

src/config/options.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fmt;
33
use std::path::{Path, PathBuf};
44

55
use atty;
6+
use itertools::Itertools;
67
use rustfmt_config_proc_macro::config_type;
78
use serde::de::{SeqAccess, Visitor};
89
use serde::ser::SerializeSeq;
@@ -193,6 +194,12 @@ pub struct WidthHeuristics {
193194
pub single_line_if_else_max_width: usize,
194195
}
195196

197+
impl fmt::Display for WidthHeuristics {
198+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199+
write!(f, "{:?}", self)
200+
}
201+
}
202+
196203
impl WidthHeuristics {
197204
// Using this WidthHeuristics means we ignore heuristics.
198205
pub fn null() -> WidthHeuristics {
@@ -264,6 +271,21 @@ pub struct IgnoreList {
264271
rustfmt_toml_path: PathBuf,
265272
}
266273

274+
impl fmt::Display for IgnoreList {
275+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
276+
write!(
277+
f,
278+
"[{}]",
279+
self.path_set
280+
.iter()
281+
.format_with(", ", |path, f| f(&format_args!(
282+
"{}",
283+
path.to_string_lossy()
284+
)))
285+
)
286+
}
287+
}
288+
267289
impl Serialize for IgnoreList {
268290
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
269291
where

0 commit comments

Comments
 (0)