Skip to content

Commit f43dc9a

Browse files
committed
feat: forbid usage of many WildcardGroup::Fallback
1 parent 1f9350c commit f43dc9a

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

Configurations.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,6 +2301,7 @@ 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`).
2304+
- `*` is a special fallback group (i.e used if no other group matches), could only be specified once.
23042305

23052306
For a given config:
23062307

src/config/options.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,27 @@ impl<'de> Deserialize<'de> for GroupImportsTactic {
190190
lines.push(elem);
191191
}
192192

193-
lines
193+
let value = lines
194194
.into_iter()
195195
.map(TryFrom::try_from)
196196
.collect::<Result<Vec<_>, _>>()
197197
.map(WildcardGroups)
198-
.map(GroupImportsTactic::Wildcards)
199-
.map_err(serde::de::Error::custom)
198+
.map_err(serde::de::Error::custom)?;
199+
200+
if value
201+
.0
202+
.iter()
203+
.filter(|g| matches!(g, WildcardGroup::Fallback))
204+
.count()
205+
> 1
206+
{
207+
return Err(serde::de::Error::custom(concat!(
208+
"Wildcard format group must include at ",
209+
"most one fallback (i.e \"*\") group."
210+
)));
211+
}
212+
213+
Ok(GroupImportsTactic::Wildcards(value))
200214
}
201215
}
202216

@@ -246,7 +260,7 @@ impl WildcardGroup {
246260
}
247261

248262
impl TryFrom<Vec<String>> for WildcardGroup {
249-
type Error = regex::Error;
263+
type Error = anyhow::Error;
250264

251265
fn try_from(i: Vec<String>) -> Result<Self, Self::Error> {
252266
enum Kind {
@@ -258,7 +272,14 @@ impl TryFrom<Vec<String>> for WildcardGroup {
258272
return Ok(Self::Fallback);
259273
}
260274

261-
i.into_iter()
275+
if i.iter().any(|x| x == "*") {
276+
return Err(anyhow::anyhow!(concat!(
277+
"'*' is a special wildcard for a fallback group ",
278+
"and could only be a single item within this group"
279+
)));
280+
}
281+
282+
Ok(i.into_iter()
262283
.map(|s| {
263284
let (s, kind) = if let Some(tail) = s.strip_prefix("$std") {
264285
(tail, Some(Kind::Std))
@@ -279,7 +300,7 @@ impl TryFrom<Vec<String>> for WildcardGroup {
279300
regex::Regex::new(&format!("^{wildcard}$"))
280301
})
281302
.collect::<Result<_, _>>()
282-
.map(Self::Group)
303+
.map(Self::Group)?)
283304
}
284305
}
285306

0 commit comments

Comments
 (0)