Skip to content

Commit

Permalink
implement Display for enums with attribute config_type (#3621)
Browse files Browse the repository at this point in the history
  • Loading branch information
scampi authored and topecongiro committed Jun 12, 2019
1 parent 71fa794 commit 04add0c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
25 changes: 25 additions & 0 deletions config_proc_macro/src/item_enum.rs
Expand Up @@ -23,6 +23,7 @@ pub fn define_config_type_on_enum(em: &syn::ItemEnum) -> syn::Result<TokenStream

let impl_doc_hint = impl_doc_hint(&em.ident, &em.variants);
let impl_from_str = impl_from_str(&em.ident, &em.variants);
let impl_display = impl_display(&em.ident, &em.variants);
let impl_serde = impl_serde(&em.ident, &em.variants);
let impl_deserialize = impl_deserialize(&em.ident, &em.variants);

Expand All @@ -31,6 +32,7 @@ pub fn define_config_type_on_enum(em: &syn::ItemEnum) -> syn::Result<TokenStream
mod #mod_name {
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub #enum_token #ident #generics { #variants }
#impl_display
#impl_doc_hint
#impl_from_str
#impl_serde
Expand Down Expand Up @@ -68,6 +70,29 @@ fn impl_doc_hint(ident: &syn::Ident, variants: &Variants) -> TokenStream {
}
}

fn impl_display(ident: &syn::Ident, variants: &Variants) -> TokenStream {
let vs = variants
.iter()
.filter(|v| is_unit(v))
.map(|v| (config_value_of_variant(v), &v.ident));
let match_patterns = fold_quote(vs, |(s, v)| {
quote! {
#ident::#v => write!(f, "{}", #s),
}
});
quote! {
use std::fmt;
impl fmt::Display for #ident {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
#match_patterns
_ => unimplemented!(),
}
}
}
}
}

fn impl_from_str(ident: &syn::Ident, variants: &Variants) -> TokenStream {
let vs = variants
.iter()
Expand Down
8 changes: 6 additions & 2 deletions src/config/config_type.rs
Expand Up @@ -249,11 +249,15 @@ macro_rules! create_config {
}
name_out.push_str(name_raw);
name_out.push(' ');
let mut default_str = format!("{}", $def);
if default_str.is_empty() {
default_str = String::from("\"\"");
}
writeln!(out,
"{}{} Default: {:?}{}",
"{}{} Default: {}{}",
name_out,
<$ty>::doc_hint(),
$def,
default_str,
if !$stb { " (unstable)" } else { "" }).unwrap();
$(
writeln!(out, "{}{}", space_str, $dstring).unwrap();
Expand Down
22 changes: 22 additions & 0 deletions src/config/file_lines.rs
@@ -1,5 +1,6 @@
//! This module contains types and functions to support formatting specific line ranges.

use itertools::Itertools;
use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;
Expand Down Expand Up @@ -92,6 +93,12 @@ impl<'a> From<&'a LineRange> for Range {
}
}

impl fmt::Display for Range {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}..{}", self.lo, self.hi)
}
}

impl Range {
pub fn new(lo: usize, hi: usize) -> Range {
Range { lo, hi }
Expand Down Expand Up @@ -149,6 +156,21 @@ impl Range {
#[derive(Clone, Debug, Default, PartialEq)]
pub struct FileLines(Option<HashMap<FileName, Vec<Range>>>);

impl fmt::Display for FileLines {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
None => write!(f, "None")?,
Some(map) => {
for (file_name, ranges) in map.iter() {
write!(f, "{}: ", file_name)?;
write!(f, "{}\n", ranges.iter().format(", "))?;
}
}
};
Ok(())
}
}

/// Normalizes the ranges so that the invariants for `FileLines` hold: ranges are non-overlapping,
/// and ordered by their start point.
fn normalize_ranges(ranges: &mut HashMap<FileName, Vec<Range>>) {
Expand Down
22 changes: 22 additions & 0 deletions src/config/options.rs
Expand Up @@ -3,6 +3,7 @@ use std::fmt;
use std::path::{Path, PathBuf};

use atty;
use itertools::Itertools;
use rustfmt_config_proc_macro::config_type;
use serde::de::{SeqAccess, Visitor};
use serde::ser::SerializeSeq;
Expand Down Expand Up @@ -193,6 +194,12 @@ pub struct WidthHeuristics {
pub single_line_if_else_max_width: usize,
}

impl fmt::Display for WidthHeuristics {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}

impl WidthHeuristics {
// Using this WidthHeuristics means we ignore heuristics.
pub fn null() -> WidthHeuristics {
Expand Down Expand Up @@ -264,6 +271,21 @@ pub struct IgnoreList {
rustfmt_toml_path: PathBuf,
}

impl fmt::Display for IgnoreList {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"[{}]",
self.path_set
.iter()
.format_with(", ", |path, f| f(&format_args!(
"{}",
path.to_string_lossy()
)))
)
}
}

impl Serialize for IgnoreList {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down

0 comments on commit 04add0c

Please sign in to comment.