| 1 |
use anyhow::{bail, Context, Result}; |
| 2 |
use fs_err as fs; |
| 3 |
use serde::{Deserialize, Serialize}; |
| 4 |
use std::collections::HashMap; |
| 5 |
use std::path::Path; |
| 6 |
|
| 7 |
/// The `[lib]` section of a Cargo.toml |
| 8 |
#[derive(Serialize, Deserialize, Debug, Clone)] |
| 9 |
#[serde(rename_all = "kebab-case")] |
| 10 |
pub(crate) struct CargoTomlLib { |
| 11 |
pub(crate) crate_type: Option<Vec<String>>, |
| 12 |
pub(crate) name: Option<String>, |
| 13 |
} |
| 14 |
|
| 15 |
/// The `[package]` section of a Cargo.toml |
| 16 |
#[derive(Serialize, Deserialize, Debug, Clone)] |
| 17 |
#[serde(rename_all = "kebab-case")] |
| 18 |
pub(crate) struct CargoTomlPackage { |
| 19 |
pub(crate) name: String, |
| 20 |
metadata: Option<CargoTomlMetadata>, |
| 21 |
} |
| 22 |
|
| 23 |
/// Extract of the Cargo.toml that can be reused for the python metadata |
| 24 |
/// |
| 25 |
/// See https://doc.rust-lang.org/cargo/reference/manifest.html for a specification |
| 26 |
#[derive(Serialize, Deserialize, Debug, Clone)] |
| 27 |
pub struct CargoToml { |
| 28 |
pub(crate) lib: Option<CargoTomlLib>, |
| 29 |
pub(crate) package: CargoTomlPackage, |
| 30 |
} |
| 31 |
|
| 32 |
impl CargoToml { |
| 33 |
/// Reads and parses the Cargo.toml at the given location |
| 34 |
pub fn from_path(manifest_file: impl AsRef<Path>) -> Result<Self> { |
| 35 |
let contents = fs::read_to_string(&manifest_file).context(format!( |
| 36 |
"Can't read Cargo.toml at {}", |
| 37 |
manifest_file.as_ref().display(), |
| 38 |
))?; |
| 39 |
let cargo_toml = toml::from_str(&contents).context(format!( |
| 40 |
"Failed to parse Cargo.toml at {}", |
| 41 |
manifest_file.as_ref().display() |
| 42 |
))?; |
| 43 |
Ok(cargo_toml) |
| 44 |
} |
| 45 |
|
| 46 |
/// Returns the value of `[project.metadata.maturin]` or an empty stub |
| 47 |
pub fn remaining_core_metadata(&self) -> RemainingCoreMetadata { |
| 48 |
match &self.package.metadata { |
| 49 |
Some(CargoTomlMetadata { |
| 50 |
maturin: Some(extra_metadata), |
| 51 |
}) => extra_metadata.clone(), |
| 52 |
_ => Default::default(), |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/// Check removed python metadata support in `Cargo.toml` |
| 57 |
pub fn check_removed_python_metadata(&self) -> Result<()> { |
| 58 |
let mut removed = Vec::new(); |
| 59 |
if let Some(CargoTomlMetadata { |
| 60 |
maturin: Some(extra_metadata), |
| 61 |
}) = &self.package.metadata |
| 62 |
{ |
| 63 |
let removed_keys = [ |
| 64 |
"scripts", |
| 65 |
"classifiers", |
| 66 |
"classifier", |
| 67 |
"data", |
| 68 |
"maintainer", |
| 69 |
"maintainer-email", |
| 70 |
"requires-dist", |
| 71 |
"requires-python", |
| 72 |
"requires-external", |
| 73 |
"project-url", |
| 74 |
"provides-extra", |
| 75 |
"description-content-type", |
| 76 |
]; |
| 77 |
for key in removed_keys { |
| 78 |
if extra_metadata.other.contains_key(key) { |
| 79 |
removed.push(key); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
if !removed.is_empty() { |
| 84 |
bail!( |
| 85 |
"The following metadata fields in `package.metadata.maturin` section \ |
| 86 |
of Cargo.toml are removed since maturin 0.14.0: {}, \ |
| 87 |
please set them in pyproject.toml as PEP 621 specifies.", |
| 88 |
removed.join(", ") |
| 89 |
); |
| 90 |
} |
| 91 |
Ok(()) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
#[derive(Serialize, Deserialize, Debug, Clone)] |
| 96 |
#[serde(rename_all = "kebab-case")] |
| 97 |
struct CargoTomlMetadata { |
| 98 |
maturin: Option<RemainingCoreMetadata>, |
| 99 |
} |
| 100 |
|
| 101 |
/// The `[project.metadata.maturin]` with the maturin specific metadata |
| 102 |
#[derive(Serialize, Deserialize, Debug, Clone, Default)] |
| 103 |
#[serde(rename_all = "kebab-case")] |
| 104 |
pub struct RemainingCoreMetadata { |
| 105 |
#[serde(flatten)] |
| 106 |
pub other: HashMap<String, toml::Value>, |
| 107 |
} |
| 108 |
|
| 109 |
#[cfg(test)] |
| 110 |
mod test { |
| 111 |
use super::*; |
| 112 |
use indoc::indoc; |
| 113 |
|
| 114 |
#[test] |
| 115 |
fn test_metadata_from_cargo_toml() { |
| 116 |
let cargo_toml = indoc!( |
| 117 |
r#" |
| 118 |
[package] |
| 119 |
authors = ["konstin <[email protected]>"] |
| 120 |
name = "info-project" |
| 121 |
version = "0.1.0" |
| 122 |
description = "A test project" |
| 123 |
homepage = "https://example.org" |
| 124 |
keywords = ["ffi", "test"] |
| 125 |
|
| 126 |
[lib] |
| 127 |
crate-type = ["cdylib"] |
| 128 |
name = "pyo3_pure" |
| 129 |
|
| 130 |
[package.metadata.maturin.scripts] |
| 131 |
ph = "maturin:print_hello" |
| 132 |
|
| 133 |
[package.metadata.maturin] |
| 134 |
classifiers = ["Programming Language :: Python"] |
| 135 |
requires-dist = ["flask~=1.1.0", "toml==0.10.0"] |
| 136 |
|
| 137 |
[[package.metadata.maturin.targets]] |
| 138 |
name = "pyo3_pure" |
| 139 |
kind = "lib" |
| 140 |
bindings = "pyo3" |
| 141 |
"# |
| 142 |
); |
| 143 |
|
| 144 |
let cargo_toml: Result<CargoToml, _> = toml::from_str(cargo_toml); |
| 145 |
assert!(cargo_toml.is_ok()); |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
fn test_metadata_from_cargo_toml_without_authors() { |
| 150 |
let cargo_toml = indoc!( |
| 151 |
r#" |
| 152 |
[package] |
| 153 |
name = "info-project" |
| 154 |
version = "0.1.0" |
| 155 |
description = "A test project" |
| 156 |
homepage = "https://example.org" |
| 157 |
keywords = ["ffi", "test"] |
| 158 |
|
| 159 |
[lib] |
| 160 |
crate-type = ["cdylib"] |
| 161 |
name = "pyo3_pure" |
| 162 |
|
| 163 |
[package.metadata.maturin.scripts] |
| 164 |
ph = "maturin:print_hello" |
| 165 |
|
| 166 |
[package.metadata.maturin] |
| 167 |
classifiers = ["Programming Language :: Python"] |
| 168 |
requires-dist = ["flask~=1.1.0", "toml==0.10.0"] |
| 169 |
"# |
| 170 |
); |
| 171 |
|
| 172 |
let cargo_toml: Result<CargoToml, _> = toml::from_str(cargo_toml); |
| 173 |
assert!(cargo_toml.is_ok()); |
| 174 |
} |
| 175 |
} |