From 4a9804c5251ffd73d808a0f3790b683c023db8a1 Mon Sep 17 00:00:00 2001 From: Bas Schoenmaeckers Date: Wed, 13 Mar 2024 16:44:03 +0100 Subject: [PATCH 1/2] Allow parsing the tool table --- src/lib.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 70 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 897c7a6..2ea9f56 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,9 @@ use indexmap::IndexMap; use pep440_rs::{Version, VersionSpecifiers}; use pep508_rs::Requirement; +use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; +use toml::Table; /// The `[build-system]` section of a pyproject.toml as specified in PEP 517 #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] @@ -18,11 +20,13 @@ pub struct BuildSystem { /// A pyproject.toml as specified in PEP 517 #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[serde(rename_all = "kebab-case")] -pub struct PyProjectToml { +pub struct PyProjectToml { /// Build-related data pub build_system: Option, /// Project metadata pub project: Option, + /// Tool section + pub tool: Option, } /// PEP 621 project metadata @@ -161,7 +165,7 @@ pub struct Contact { pub email: Option, } -impl PyProjectToml { +impl PyProjectToml { /// Parse `pyproject.toml` content pub fn new(content: &str) -> Result { toml::de::from_str(content) @@ -173,6 +177,7 @@ mod tests { use super::{License, LicenseFiles, PyProjectToml, ReadMe}; use pep440_rs::{Version, VersionSpecifiers}; use pep508_rs::Requirement; + use serde::Deserialize; use std::str::FromStr; #[test] @@ -228,7 +233,7 @@ spam-gui = "spam:main_gui" [project.entry-points."spam.magical"] tomatoes = "spam:main_tomatoes""#; - let project_toml = PyProjectToml::new(source).unwrap(); + let project_toml: PyProjectToml = PyProjectToml::new(source).unwrap(); let build_system = &project_toml.build_system.unwrap(); assert_eq!( build_system.requires, @@ -285,7 +290,7 @@ build-backend = "maturin" name = "spam" license = "MIT OR BSD-3-Clause" "#; - let project_toml = PyProjectToml::new(source).unwrap(); + let project_toml: PyProjectToml = PyProjectToml::new(source).unwrap(); let project = project_toml.project.as_ref().unwrap(); assert_eq!( project.license, @@ -310,7 +315,7 @@ license-files.paths = [ "setuptools/_vendor/LICENSE.BSD", ] "#; - let project_toml = PyProjectToml::new(source).unwrap(); + let project_toml: PyProjectToml = PyProjectToml::new(source).unwrap(); let project = project_toml.project.as_ref().unwrap(); assert_eq!( @@ -345,7 +350,7 @@ license-files.globs = [ "setuptools/_vendor/LICENSE*", ] "#; - let project_toml = PyProjectToml::new(source).unwrap(); + let project_toml: PyProjectToml = PyProjectToml::new(source).unwrap(); let project = project_toml.project.as_ref().unwrap(); assert_eq!( @@ -372,7 +377,7 @@ build-backend = "maturin" [project] name = "spam" "#; - let project_toml = PyProjectToml::new(source).unwrap(); + let project_toml: PyProjectToml = PyProjectToml::new(source).unwrap(); let project = project_toml.project.as_ref().unwrap(); assert_eq!( @@ -396,7 +401,7 @@ build-backend = "maturin" name = "spam" readme = {text = "ReadMe!", content-type = "text/plain"} "#; - let project_toml = PyProjectToml::new(source).unwrap(); + let project_toml: PyProjectToml = PyProjectToml::new(source).unwrap(); let project = project_toml.project.as_ref().unwrap(); assert_eq!( @@ -408,4 +413,61 @@ readme = {text = "ReadMe!", content-type = "text/plain"} }) ); } + + #[test] + fn test_parse_pyproject_toml_tool_section_as_table() { + let source = r#"[build-system] +requires = ["maturin"] +build-backend = "maturin" + +[tool.maturin] +bindings = "pyo3" +"#; + let project_toml: PyProjectToml = PyProjectToml::new(source).unwrap(); + let tool = project_toml.tool.as_ref().unwrap(); + assert_eq!( + tool.get("maturin") + .unwrap() + .get("bindings") + .unwrap() + .as_str(), + Some("pyo3") + ); + } + + #[test] + fn test_parse_pyproject_toml_tool_section() { + #[derive(Deserialize)] + struct Maturin { + bindings: Option, + } + + #[derive(Deserialize)] + struct Tools { + maturin: Option, + } + + let source = r#"[build-system] +requires = ["maturin"] +build-backend = "maturin" + +[tool.maturin] +bindings = "pyo3" + +[tool.ruff] +line-length = 120 +"#; + + let project_toml: PyProjectToml = PyProjectToml::new(source).unwrap(); + assert_eq!( + project_toml + .tool + .unwrap() + .maturin + .unwrap() + .bindings + .as_deref(), + Some("pyo3") + ); + } } From 9c2b4d00b9dfd31e7f72e0c8f17bc5a0416ee801 Mon Sep 17 00:00:00 2001 From: Bas Schoenmaeckers Date: Fri, 15 Mar 2024 15:33:10 +0100 Subject: [PATCH 2/2] Add try_into & try_from implementations --- src/lib.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 2ea9f56..d94b27a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -170,6 +170,27 @@ impl PyProjectToml { pub fn new(content: &str) -> Result { toml::de::from_str(content) } + + pub fn try_from(value: PyProjectToml) -> Result { + Ok(Self { + build_system: value.build_system, + project: value.project, + tool: value.tool.map(Table::try_into).transpose()?, + }) + } +} + +impl PyProjectToml { + pub fn try_into<'de, T>(self) -> Result, toml::de::Error> + where + T: Deserialize<'de>, + { + Ok(PyProjectToml { + build_system: self.build_system, + project: self.project, + tool: self.tool.map(Table::try_into).transpose()?, + }) + } } #[cfg(test)]