Skip to content

Commit

Permalink
Use Version, VersionSpecifiers and Requirement from PEP 440 and PEP 5…
Browse files Browse the repository at this point in the history
…08 parser

Using crates.io releases is currently blocked on PyO3/pyo3#2786
  • Loading branch information
konstin committed Mar 22, 2023
1 parent 1d78642 commit 5e9bd38
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
30 changes: 22 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct BuildSystem {
/// PEP 508 dependencies required to execute the build system
pub requires: Vec<String>,
pub requires: Vec<Requirement>,
/// A string naming a Python object that will be used to perform the build
pub build_backend: Option<String>,
/// Specify that their backend code is hosted in-tree, this key contains a list of directories
Expand All @@ -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<String>,
pub version: Option<Version>,
/// The summary description of the project
pub description: Option<String>,
/// The full description of the project (i.e. the README)
pub readme: Option<ReadMe>,
/// The Python version requirements of the project
pub requires_python: Option<String>,
pub requires_python: Option<VersionSpecifiers>,
/// License
pub license: Option<License>,
/// License Expression (PEP 639) - https://peps.python.org/pep-0639/#add-license-expression-key
Expand All @@ -60,9 +62,9 @@ pub struct Project {
/// Corresponds to the gui_scripts group in the core metadata
pub gui_scripts: Option<IndexMap<String, String>>,
/// Project dependencies
pub dependencies: Option<Vec<String>>,
pub dependencies: Option<Vec<Requirement>>,
/// Optional dependencies
pub optional_dependencies: Option<IndexMap<String, Vec<String>>>,
pub optional_dependencies: Option<IndexMap<String, Vec<Requirement>>>,
/// Specifies which fields listed by PEP 621 were intentionally unspecified
/// so another tool can/will provide such metadata dynamically.
pub dynamic: Option<Vec<String>>,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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!")
Expand All @@ -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")
Expand Down

0 comments on commit 5e9bd38

Please sign in to comment.