From 7d4bab2e208de70f1a756ac99ac1e2a345c94318 Mon Sep 17 00:00:00 2001 From: Alexander Koval Date: Tue, 1 Aug 2023 12:58:54 +0300 Subject: [PATCH] Fix parsing version --- rye/src/pyproject.rs | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/rye/src/pyproject.rs b/rye/src/pyproject.rs index 50c3a297ed..f8adc43d50 100644 --- a/rye/src/pyproject.rs +++ b/rye/src/pyproject.rs @@ -745,22 +745,31 @@ impl PyProject { /// Returns the version. pub fn version(&mut self) -> Result { - let mut version = self - .doc - .get("project") - .and_then(|x| x.get("version")) - .map(|x| x.to_string()); - if let Some(dynamic) = self.dynamic() { - if dynamic.contains(&"version".to_string()) { + let read_version = || { + self.doc + .get("project") + .and_then(|x| x.get("version")) + .and_then(|x| x.as_str().map(String::from)) + }; + + let version = match self.dynamic() { + Some(dynamic) if dynamic.contains(&"version".to_string()) => { if let Ok(metadata) = get_project_metadata(&self.root_path()) { - version = Some(metadata.version); - }; - }; + Some(metadata.version) + } else { + read_version() + } + } + _ => { + read_version() + } }; match version { - Some(version) => Version::from_str(version.as_str()) - .map_err(|msg| anyhow!("invalid version: {}", msg)), + Some(version) => { + Version::from_str(version.as_str()) + .map_err(|msg| anyhow!("invalid version: {}", msg)) + } None => { let version = Version::from_str("0.1.0").unwrap(); self.set_version(&version);