Skip to content

Commit

Permalink
Fix parsing version
Browse files Browse the repository at this point in the history
  • Loading branch information
anti-social committed Aug 1, 2023
1 parent 5912fd0 commit 7d4bab2
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions rye/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,22 +745,31 @@ impl PyProject {

/// Returns the version.
pub fn version(&mut self) -> Result<Version, Error> {
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);
Expand Down

0 comments on commit 7d4bab2

Please sign in to comment.