Remove vector allocation in WheelFilename (#177)

This commit is contained in:
Charlie Marsh 2023-10-23 18:23:14 -07:00 committed by GitHub
parent 21bb9c29cc
commit d0aeb2ac80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -25,11 +25,66 @@ impl FromStr for WheelFilename {
"Must end with .whl".to_string(),
)
})?;
// The wheel filename should contain either five or six entries. If six, then the third
// entry is the build tag. If five, then the third entry is the Python tag.
// https://www.python.org/dev/peps/pep-0427/#file-name-convention
match basename.split('-').collect::<Vec<_>>().as_slice() {
// TODO(charlie): Build tag precedence
&[distribution, version, _, python_tag, abi_tag, platform_tag]
| &[distribution, version, python_tag, abi_tag, platform_tag] => {
let mut parts = basename.split('-');
let Some(distribution) = parts.next() else {
return Err(WheelFilenameError::InvalidWheelFileName(
filename.to_string(),
"Must have a distribution name".to_string(),
));
};
let Some(version) = parts.next() else {
return Err(WheelFilenameError::InvalidWheelFileName(
filename.to_string(),
"Must have a version".to_string(),
));
};
let Some(build_tag_or_python_tag) = parts.next() else {
return Err(WheelFilenameError::InvalidWheelFileName(
filename.to_string(),
"Must have a Python tag".to_string(),
));
};
let Some(python_tag_or_abi_tag) = parts.next() else {
return Err(WheelFilenameError::InvalidWheelFileName(
filename.to_string(),
"Must have an ABI tag".to_string(),
));
};
let Some(abi_tag_or_platform_tag) = parts.next() else {
return Err(WheelFilenameError::InvalidWheelFileName(
filename.to_string(),
"Must have a platform tag".to_string(),
));
};
let (distribution, version, python_tag, abi_tag, platform_tag) =
if let Some(platform_tag) = parts.next() {
(
distribution,
version,
python_tag_or_abi_tag,
abi_tag_or_platform_tag,
platform_tag,
)
} else {
(
distribution,
version,
build_tag_or_python_tag,
python_tag_or_abi_tag,
abi_tag_or_platform_tag,
)
};
let version = Version::from_str(version)
.map_err(|err| WheelFilenameError::InvalidVersion(filename.to_string(), err))?;
Ok(WheelFilename {
@ -40,12 +95,6 @@ impl FromStr for WheelFilename {
platform_tag: platform_tag.split('.').map(String::from).collect(),
})
}
_ => Err(WheelFilenameError::InvalidWheelFileName(
filename.to_string(),
"Expected four \"-\" in the filename".to_string(),
)),
}
}
}
impl Display for WheelFilename {