Skip to content

Commit

Permalink
fix: configuration doctest for custom group_imports
Browse files Browse the repository at this point in the history
  • Loading branch information
l4l committed Jan 30, 2023
1 parent 94819ee commit 3b1d8b8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
17 changes: 3 additions & 14 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2294,27 +2294,16 @@ use std::sync::Arc;
use uuid::Uuid;
```

#### Example for custom groups:
#### `[ ["$std::*", "proc_macro::*"], ["*"], ["my_crate::*", "crate::*::xyz"], ["$crate::*"] ]`:

Discard existing import groups, and create groups as specified by the wildcarded list.
Discards existing import groups, and create groups as specified by the wildcarded list.
Handy aliases are supported:

- `$std` prefix is an alias for standard library (i.e `std`, `core`, `alloc`);
- `$crate` prefix is an alias for crate-local modules (i.e `self`, `crate`, `super`).
- `*` is a special fallback group (i.e used if no other group matches), could only be specified once.

For a given config:

```toml
group_imports = [
["$std::*", "proc_macro::*"],
["*"],
["my_crate::*", "crate::*::xyz"],
["$crate::*"],
]
```

The following order would be set:
With the provided config the following order would be set:

```rust
use proc_macro::Span;
Expand Down
18 changes: 16 additions & 2 deletions config_proc_macro/src/item_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,22 @@ pub fn define_config_type_on_enum(args: &Args, em: &syn::ItemEnum) -> syn::Resul
.unwrap_or_default();

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_from_str = if args
.skip_derives()
.all(|s| !"std::str::FromStr".ends_with(s))
{
impl_from_str(&em.ident, &em.variants)
} else {
Default::default()
};
let impl_display = if args
.skip_derives()
.all(|s| !"std::str::Display".ends_with(s))
{
impl_display(&em.ident, &em.variants)
} else {
Default::default()
};

Ok(quote! {
#[allow(non_snake_case)]
Expand Down
20 changes: 19 additions & 1 deletion src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Density {
}
}

#[config_type(skip_derive(Copy, Serialize, Deserialize))]
#[config_type(skip_derive(Copy, Serialize, Deserialize, FromStr))]
/// Configuration for import groups, i.e. sets of imports separated by newlines.
pub enum GroupImportsTactic {
/// Keep groups as they are.
Expand Down Expand Up @@ -219,6 +219,24 @@ impl<'de> Deserialize<'de> for GroupImportsTactic {
}
}

impl std::str::FromStr for GroupImportsTactic {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"Preserve" => GroupImportsTactic::Preserve,
"StdExternalCrate" => GroupImportsTactic::StdExternalCrate,
"One" => GroupImportsTactic::One,
line => serde_json::from_str::<Vec<Vec<String>>>(&line)?
.into_iter()
.map(TryFrom::try_from)
.collect::<Result<Vec<WildcardGroup>, _>>()
.map(WildcardGroups)
.map(GroupImportsTactic::Wildcards)?,
})
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct WildcardGroups(Vec<WildcardGroup>);

Expand Down

0 comments on commit 3b1d8b8

Please sign in to comment.