-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Respect comma-separated extras in
--with
(#8946)
## Summary We need to treat `flask,anyio` as two requirements, but `psycopg[binary,pool]` as a single requirement. Closes #8918.
- Loading branch information
1 parent
0b5a061
commit b5a3d09
Showing
4 changed files
with
138 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::str::FromStr; | ||
|
||
/// A comma-separated string of requirements, e.g., `"flask,anyio"`, that takes extras into account | ||
/// (i.e., treats `"psycopg[binary,pool]"` as a single requirement). | ||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub struct CommaSeparatedRequirements(Vec<String>); | ||
|
||
impl IntoIterator for CommaSeparatedRequirements { | ||
type Item = String; | ||
type IntoIter = std::vec::IntoIter<Self::Item>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.0.into_iter() | ||
} | ||
} | ||
|
||
impl FromStr for CommaSeparatedRequirements { | ||
type Err = String; | ||
|
||
fn from_str(input: &str) -> Result<Self, Self::Err> { | ||
// Split on commas _outside_ of brackets. | ||
let mut requirements = Vec::new(); | ||
let mut depth = 0usize; | ||
let mut start = 0usize; | ||
for (i, c) in input.char_indices() { | ||
match c { | ||
'[' => { | ||
depth = depth.saturating_add(1); | ||
} | ||
']' => { | ||
depth = depth.saturating_sub(1); | ||
} | ||
',' if depth == 0 => { | ||
let requirement = input[start..i].trim().to_string(); | ||
if !requirement.is_empty() { | ||
requirements.push(requirement); | ||
} | ||
start = i + ','.len_utf8(); | ||
} | ||
_ => {} | ||
} | ||
} | ||
let requirement = input[start..].trim().to_string(); | ||
if !requirement.is_empty() { | ||
requirements.push(requirement); | ||
} | ||
Ok(Self(requirements)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use super::CommaSeparatedRequirements; | ||
use std::str::FromStr; | ||
|
||
#[test] | ||
fn single() { | ||
assert_eq!( | ||
CommaSeparatedRequirements::from_str("flask").unwrap(), | ||
CommaSeparatedRequirements(vec!["flask".to_string()]) | ||
); | ||
} | ||
|
||
#[test] | ||
fn double() { | ||
assert_eq!( | ||
CommaSeparatedRequirements::from_str("flask,anyio").unwrap(), | ||
CommaSeparatedRequirements(vec!["flask".to_string(), "anyio".to_string()]) | ||
); | ||
} | ||
|
||
#[test] | ||
fn empty() { | ||
assert_eq!( | ||
CommaSeparatedRequirements::from_str("flask,,anyio").unwrap(), | ||
CommaSeparatedRequirements(vec!["flask".to_string(), "anyio".to_string()]) | ||
); | ||
} | ||
|
||
#[test] | ||
fn single_extras() { | ||
assert_eq!( | ||
CommaSeparatedRequirements::from_str("psycopg[binary,pool]").unwrap(), | ||
CommaSeparatedRequirements(vec!["psycopg[binary,pool]".to_string()]) | ||
); | ||
} | ||
|
||
#[test] | ||
fn double_extras() { | ||
assert_eq!( | ||
CommaSeparatedRequirements::from_str("psycopg[binary,pool], flask").unwrap(), | ||
CommaSeparatedRequirements(vec![ | ||
"psycopg[binary,pool]".to_string(), | ||
"flask".to_string() | ||
]) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters