Skip to content

Commit cca9277

Browse files
committed
fix: configuration doctest for custom group_imports
1 parent bcaf80b commit cca9277

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

Configurations.md

+3-14
Original file line numberDiff line numberDiff line change
@@ -2294,27 +2294,16 @@ use std::sync::Arc;
22942294
use uuid::Uuid;
22952295
```
22962296

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

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

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

2306-
For a given config:
2307-
2308-
```toml
2309-
group_imports = [
2310-
["$std::*", "proc_macro::*"],
2311-
["*"],
2312-
["my_crate::*", "crate::*::xyz"],
2313-
["$crate::*"],
2314-
]
2315-
```
2316-
2317-
The following order would be set:
2306+
With the provided config the following order would be set:
23182307

23192308
```rust
23202309
use proc_macro::Span;

config_proc_macro/src/item_enum.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,22 @@ pub fn define_config_type_on_enum(args: &Args, em: &syn::ItemEnum) -> syn::Resul
4646
.unwrap_or_default();
4747

4848
let impl_doc_hint = impl_doc_hint(&em.ident, &em.variants);
49-
let impl_from_str = impl_from_str(&em.ident, &em.variants);
50-
let impl_display = impl_display(&em.ident, &em.variants);
49+
let impl_from_str = if args
50+
.skip_derives()
51+
.all(|s| !"std::str::FromStr".ends_with(s))
52+
{
53+
impl_from_str(&em.ident, &em.variants)
54+
} else {
55+
Default::default()
56+
};
57+
let impl_display = if args
58+
.skip_derives()
59+
.all(|s| !"std::str::Display".ends_with(s))
60+
{
61+
impl_display(&em.ident, &em.variants)
62+
} else {
63+
Default::default()
64+
};
5165

5266
Ok(quote! {
5367
#[allow(non_snake_case)]

src/config/options.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl Density {
104104
}
105105
}
106106

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

222+
impl std::str::FromStr for GroupImportsTactic {
223+
type Err = anyhow::Error;
224+
225+
fn from_str(s: &str) -> Result<Self, Self::Err> {
226+
Ok(match s {
227+
"Preserve" => GroupImportsTactic::Preserve,
228+
"StdExternalCrate" => GroupImportsTactic::StdExternalCrate,
229+
"One" => GroupImportsTactic::One,
230+
line => serde_json::from_str::<Vec<Vec<String>>>(&line)?
231+
.into_iter()
232+
.map(TryFrom::try_from)
233+
.collect::<Result<Vec<WildcardGroup>, _>>()
234+
.map(WildcardGroups)
235+
.map(GroupImportsTactic::Wildcards)?,
236+
})
237+
}
238+
}
239+
222240
#[derive(Debug, Clone, Eq, PartialEq)]
223241
pub struct WildcardGroups(Vec<WildcardGroup>);
224242

0 commit comments

Comments
 (0)