Skip to content

Commit 89897fe

Browse files
committed
remove duplication of sdist extensions
1 parent bf1d4e8 commit 89897fe

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

src/distribution.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,20 @@ impl Distribution {
7676
/// Open and parse a distribution from `path`
7777
pub fn new(path: impl AsRef<Path>) -> Result<Self, Error> {
7878
let path = path.as_ref();
79-
if let Some(ext) = path.extension().and_then(|ext| ext.to_str()) {
80-
let dist_type = match ext {
81-
"zip" | "gz" => DistributionType::SDist,
82-
"egg" => DistributionType::Egg,
83-
"whl" => DistributionType::Wheel,
84-
#[cfg(feature = "deprecated-formats")]
85-
"tar" => DistributionType::SDist,
86-
#[cfg(feature = "bzip2")]
87-
"bz2" => DistributionType::SDist,
88-
#[cfg(feature = "xz")]
89-
"xz" => DistributionType::SDist,
90-
_ => return Err(Error::UnknownDistributionType),
91-
};
92-
let (metadata, python_version) = match dist_type {
93-
DistributionType::SDist => {
94-
let sdist_type: SDistType = ext.parse()?;
95-
(Self::parse_sdist(path, sdist_type)?, "source".to_string())
96-
}
97-
DistributionType::Egg => {
79+
let ext = path
80+
.extension()
81+
.and_then(|ext| ext.to_str())
82+
.ok_or(Error::UnknownDistributionType)?;
83+
84+
Ok(if let Ok(sdist_type) = ext.parse() {
85+
Self {
86+
dist_type: DistributionType::SDist,
87+
metadata: Self::parse_sdist(path, sdist_type)?,
88+
python_version: "source".to_string(),
89+
}
90+
} else {
91+
match ext {
92+
"egg" => {
9893
let parts: Vec<&str> = path
9994
.file_stem()
10095
.unwrap()
@@ -106,9 +101,13 @@ impl Distribution {
106101
[_name, _version, py_ver] => py_ver,
107102
_ => "any",
108103
};
109-
(Self::parse_egg(path)?, python_version.to_string())
104+
Self {
105+
dist_type: DistributionType::Egg,
106+
metadata: Self::parse_egg(path)?,
107+
python_version: python_version.to_string(),
108+
}
110109
}
111-
DistributionType::Wheel => {
110+
"whl" => {
112111
let parts: Vec<&str> = path
113112
.file_stem()
114113
.unwrap()
@@ -120,16 +119,15 @@ impl Distribution {
120119
[_name, _version, py_ver, _abi_tag, _plat_tag] => py_ver,
121120
_ => "any",
122121
};
123-
(Self::parse_wheel(path)?, python_version.to_string())
122+
Self {
123+
dist_type: DistributionType::Wheel,
124+
metadata: Self::parse_wheel(path)?,
125+
python_version: python_version.to_string(),
126+
}
124127
}
125-
};
126-
return Ok(Self {
127-
dist_type,
128-
metadata,
129-
python_version,
130-
});
131-
}
132-
Err(Error::UnknownDistributionType)
128+
_ => return Err(Error::UnknownDistributionType),
129+
}
130+
})
133131
}
134132

135133
/// Returns distribution type

0 commit comments

Comments
 (0)