From 5e9bd38be5e06cb119bad7172afdb4bd2e3e133b Mon Sep 17 00:00:00 2001 From: konstin Date: Wed, 22 Mar 2023 10:42:42 +0100 Subject: [PATCH] Use Version, VersionSpecifiers and Requirement from PEP 440 and PEP 508 parser Using crates.io releases is currently blocked on https://github.com/PyO3/pyo3/pull/2786 --- Cargo.toml | 2 ++ src/lib.rs | 30 ++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2156e6b..4477e93 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,5 @@ repository = "https://github.com/PyO3/pyproject-toml-rs.git" indexmap = { version = "1.9.2", features = ["serde-1"] } serde = { version = "1.0.125", features = ["derive"] } toml = { version = "0.7.0", default-features = false, features = ["parse"] } +pep508_rs = { git = "https://github.com/konstin/pep508_rs", rev = "349ce4867c467de92db06983e5cd885c89339cfe", features = ["serde"] } +pep440_rs = { git = "https://github.com/konstin/pep440-rs", rev = "0e9d26526234a6615b1316d6ef7ba79ed6beac09", features = ["serde"] } diff --git a/src/lib.rs b/src/lib.rs index 29b3984..9ab9039 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ use indexmap::IndexMap; +use pep440_rs::{Version, VersionSpecifiers}; +use pep508_rs::Requirement; use serde::{Deserialize, Serialize}; /// The `[build-system]` section of a pyproject.toml as specified in PEP 517 @@ -6,7 +8,7 @@ use serde::{Deserialize, Serialize}; #[serde(rename_all = "kebab-case")] pub struct BuildSystem { /// PEP 508 dependencies required to execute the build system - pub requires: Vec, + pub requires: Vec, /// A string naming a Python object that will be used to perform the build pub build_backend: Option, /// Specify that their backend code is hosted in-tree, this key contains a list of directories @@ -30,13 +32,13 @@ pub struct Project { /// The name of the project pub name: String, /// The version of the project as supported by PEP 440 - pub version: Option, + pub version: Option, /// The summary description of the project pub description: Option, /// The full description of the project (i.e. the README) pub readme: Option, /// The Python version requirements of the project - pub requires_python: Option, + pub requires_python: Option, /// License pub license: Option, /// License Expression (PEP 639) - https://peps.python.org/pep-0639/#add-license-expression-key @@ -60,9 +62,9 @@ pub struct Project { /// Corresponds to the gui_scripts group in the core metadata pub gui_scripts: Option>, /// Project dependencies - pub dependencies: Option>, + pub dependencies: Option>, /// Optional dependencies - pub optional_dependencies: Option>>, + pub optional_dependencies: Option>>, /// Specifies which fields listed by PEP 621 were intentionally unspecified /// so another tool can/will provide such metadata dynamically. pub dynamic: Option>, @@ -139,6 +141,9 @@ impl PyProjectToml { #[cfg(test)] mod tests { use super::{LicenseFiles, PyProjectToml, ReadMe}; + use pep440_rs::{Version, VersionSpecifiers}; + use pep508_rs::Requirement; + use std::str::FromStr; #[test] fn test_parse_pyproject_toml() { @@ -195,12 +200,18 @@ spam-gui = "spam:main_gui" tomatoes = "spam:main_tomatoes""#; let project_toml = PyProjectToml::new(source).unwrap(); let build_system = &project_toml.build_system; - assert_eq!(build_system.requires, &["maturin"]); + assert_eq!( + build_system.requires, + &[Requirement::from_str("maturin").unwrap()] + ); assert_eq!(build_system.build_backend.as_deref(), Some("maturin")); let project = project_toml.project.as_ref().unwrap(); assert_eq!(project.name, "spam"); - assert_eq!(project.version.as_deref(), Some("2020.0.0")); + assert_eq!( + project.version, + Some(Version::from_str("2020.0.0").unwrap()) + ); assert_eq!( project.description.as_deref(), Some("Lovely Spam! Wonderful Spam!") @@ -209,7 +220,10 @@ tomatoes = "spam:main_tomatoes""#; project.readme, Some(ReadMe::RelativePath("README.rst".to_string())) ); - assert_eq!(project.requires_python.as_deref(), Some(">=3.8")); + assert_eq!( + project.requires_python, + Some(VersionSpecifiers::from_str(">=3.8").unwrap()) + ); assert_eq!( project.license.as_ref().unwrap().file.as_deref(), Some("LICENSE.txt")