Debian Codegraph @

src/bridge.rs

raw
1 use std::{fmt, str::FromStr};
2
3 use anyhow::Context;
4 use serde::Deserialize;
5
6 use crate::python_interpreter::{
7 MAXIMUM_PYPY_MINOR, MAXIMUM_PYTHON_MINOR, MINIMUM_PYPY_MINOR, MINIMUM_PYTHON_MINOR,
8 };
9
10 /// pyo3 binding crate
11 #[derive(Clone, Copy, PartialEq, Eq)]
12 pub enum PyO3Crate {
13 /// pyo3
14 PyO3,
15 /// pyo3-ffi
16 PyO3Ffi,
17 }
18
19 impl PyO3Crate {
20 /// Returns the name of the crate as a string
21 pub fn as_str(&self) -> &str {
22 match self {
23 PyO3Crate::PyO3 => "pyo3",
24 PyO3Crate::PyO3Ffi => "pyo3-ffi",
25 }
26 }
27 }
28
29 impl fmt::Debug for PyO3Crate {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 write!(f, "{}", self.as_str())
32 }
33 }
34
35 impl fmt::Display for PyO3Crate {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 write!(f, "{}", self.as_str())
38 }
39 }
40
41 impl FromStr for PyO3Crate {
42 type Err = anyhow::Error;
43
44 fn from_str(s: &str) -> Result<Self, Self::Err> {
45 match s {
46 "pyo3" => Ok(PyO3Crate::PyO3),
47 "pyo3-ffi" => Ok(PyO3Crate::PyO3Ffi),
48 _ => anyhow::bail!("unknown binding crate: {}", s),
49 }
50 }
51 }
52
53 #[derive(Debug, Clone, Deserialize)]
54 pub struct PyO3VersionMetadataRaw {
55 #[serde(rename = "min-version")]
56 pub min_version: String,
57 #[serde(rename = "max-version")]
58 pub max_version: String,
59 }
60
61 #[derive(Debug, Clone, Deserialize)]
62 pub struct PyO3MetadataRaw {
63 pub cpython: PyO3VersionMetadataRaw,
64 pub pypy: PyO3VersionMetadataRaw,
65 }
66
67 #[derive(Debug, Clone, PartialEq, Eq)]
68 pub struct PyO3VersionMetadata {
69 pub min_minor: usize,
70 pub max_minor: usize,
71 }
72
73 #[derive(Debug, Clone, PartialEq, Eq)]
74 pub struct PyO3Metadata {
75 pub cpython: PyO3VersionMetadata,
76 pub pypy: PyO3VersionMetadata,
77 }
78
79 impl TryFrom<PyO3VersionMetadataRaw> for PyO3VersionMetadata {
80 type Error = anyhow::Error;
81
82 fn try_from(raw: PyO3VersionMetadataRaw) -> Result<Self, Self::Error> {
83 let min_version = raw
84 .min_version
85 .rsplit('.')
86 .next()
87 .context("invalid min-version in pyo3-ffi metadata")?
88 .parse()?;
89 let max_version = raw
90 .max_version
91 .rsplit('.')
92 .next()
93 .context("invalid max-version in pyo3-ffi metadata")?
94 .parse()?;
95 Ok(Self {
96 min_minor: min_version,
97 max_minor: max_version,
98 })
99 }
100 }
101
102 impl TryFrom<PyO3MetadataRaw> for PyO3Metadata {
103 type Error = anyhow::Error;
104
105 fn try_from(raw: PyO3MetadataRaw) -> Result<Self, Self::Error> {
106 Ok(Self {
107 cpython: PyO3VersionMetadata::try_from(raw.cpython)?,
108 pypy: PyO3VersionMetadata::try_from(raw.pypy)?,
109 })
110 }
111 }
112
113 /// Python version to use as the abi3 target.
114 #[derive(Clone, Debug, PartialEq, Eq)]
115 pub enum Abi3Version {
116 /// abi3 wheels will have a minimum Python version matching the version of
117 /// the current Python interpreter
118 CurrentPython,
119 /// abi3 wheels will have a fixed minimum Python version
120 Version(u8, u8),
121 }
122
123 /// The name and version of the pyo3 bindings crate
124 #[derive(Clone, Debug, PartialEq, Eq)]
125 pub struct PyO3 {
126 /// The name of the bindings crate, `pyo3` or `uniffi`
127 pub crate_name: PyO3Crate,
128 /// pyo3 bindings crate version
129 pub version: semver::Version,
130 /// abi3 support
131 pub abi3: Option<Abi3Version>,
132 /// pyo3 metadata
133 pub metadata: Option<PyO3Metadata>,
134 }
135
136 impl PyO3 {
137 /// Returns the minimum python minor version supported
138 fn minimal_python_minor_version(&self) -> usize {
139 let major_version = self.version.major;
140 let minor_version = self.version.minor;
141 // N.B. must check large minor versions first
142 let min_minor = if let Some(metadata) = self.metadata.as_ref() {
143 metadata.cpython.min_minor
144 } else if (major_version, minor_version) >= (0, 16) {
145 7
146 } else {
147 MINIMUM_PYTHON_MINOR
148 };
149 if let Some(Abi3Version::Version(_, abi3_minor)) = self.abi3.as_ref() {
150 min_minor.max(*abi3_minor as usize)
151 } else {
152 min_minor
153 }
154 }
155
156 /// Returns the maximum python minor version supported
157 fn maximum_python_minor_version(&self) -> usize {
158 // N.B. must check large minor versions first
159 if let Some(metadata) = self.metadata.as_ref() {
160 metadata.cpython.max_minor
161 } else {
162 MAXIMUM_PYTHON_MINOR
163 }
164 }
165
166 /// Returns the minimum PyPy minor version supported
167 fn minimal_pypy_minor_version(&self) -> usize {
168 let major_version = self.version.major;
169 let minor_version = self.version.minor;
170 // N.B. must check large minor versions first
171 if let Some(metadata) = self.metadata.as_ref() {
172 metadata.pypy.min_minor
173 } else if (major_version, minor_version) >= (0, 23) {
174 9
175 } else if (major_version, minor_version) >= (0, 14) {
176 7
177 } else {
178 MINIMUM_PYPY_MINOR
179 }
180 }
181
182 /// Returns the maximum PyPy minor version supported
183 fn maximum_pypy_minor_version(&self) -> usize {
184 // N.B. must check large minor versions first
185 if let Some(metadata) = self.metadata.as_ref() {
186 metadata.pypy.max_minor
187 } else {
188 MAXIMUM_PYPY_MINOR
189 }
190 }
191
192 /// free-threaded Python support
193 fn supports_free_threaded(&self) -> bool {
194 let major_version = self.version.major;
195 let minor_version = self.version.minor;
196 (major_version, minor_version) >= (0, 23)
197 }
198 }
199
200 /// The way the rust code is used in the wheel
201 #[derive(Clone, Debug, PartialEq, Eq)]
202 pub enum BridgeModel {
203 /// A rust binary to be shipped a python package
204 Bin(Option<PyO3>),
205 /// A native module with pyo3 bindings.
206 PyO3(PyO3),
207 /// A native module with c bindings, i.e. `#[no_mangle] extern "C" <some item>`
208 Cffi,
209 /// A native module generated from uniffi
210 UniFfi,
211 }
212
213 impl BridgeModel {
214 /// Returns the pyo3 bindings
215 pub fn pyo3(&self) -> Option<&PyO3> {
216 match self {
217 BridgeModel::Bin(Some(bindings)) => Some(bindings),
218 BridgeModel::PyO3(bindings) => Some(bindings),
219 _ => None,
220 }
221 }
222
223 /// Test whether this is using pyo3/pyo3-ffi
224 pub fn is_pyo3(&self) -> bool {
225 matches!(self, BridgeModel::PyO3(_) | BridgeModel::Bin(Some(_)))
226 }
227
228 /// Test whether this is using a specific pyo3 crate
229 pub fn is_pyo3_crate(&self, name: PyO3Crate) -> bool {
230 match self {
231 BridgeModel::Bin(Some(bindings)) => bindings.crate_name == name,
232 BridgeModel::PyO3(bindings) => bindings.crate_name == name,
233 _ => false,
234 }
235 }
236
237 /// Test whether this is bin bindings
238 pub fn is_bin(&self) -> bool {
239 matches!(self, BridgeModel::Bin(_))
240 }
241
242 /// Returns the minimum python minor version supported
243 pub fn minimal_python_minor_version(&self) -> usize {
244 match self.pyo3() {
245 Some(bindings) => bindings.minimal_python_minor_version(),
246 None => MINIMUM_PYTHON_MINOR,
247 }
248 }
249
250 /// Returns the maximum python minor version supported
251 pub fn maximum_python_minor_version(&self) -> usize {
252 match self.pyo3() {
253 Some(bindings) => bindings.maximum_python_minor_version(),
254 None => MAXIMUM_PYTHON_MINOR,
255 }
256 }
257
258 /// Returns the minimum PyPy minor version supported
259 pub fn minimal_pypy_minor_version(&self) -> usize {
260 match self.pyo3() {
261 Some(bindings) => bindings.minimal_pypy_minor_version(),
262 None => MINIMUM_PYPY_MINOR,
263 }
264 }
265
266 /// Returns the maximum PyPy minor version supported
267 pub fn maximum_pypy_minor_version(&self) -> usize {
268 use crate::python_interpreter::MAXIMUM_PYPY_MINOR;
269
270 match self.pyo3() {
271 Some(bindings) => bindings.maximum_pypy_minor_version(),
272 None => MAXIMUM_PYPY_MINOR,
273 }
274 }
275
276 /// Is using abi3
277 pub fn is_abi3(&self) -> bool {
278 match self.pyo3() {
279 Some(pyo3) => pyo3.abi3.is_some(),
280 None => false,
281 }
282 }
283
284 /// free-threaded Python support
285 pub fn supports_free_threaded(&self) -> bool {
286 match self {
287 BridgeModel::Bin(Some(bindings)) | BridgeModel::PyO3(bindings) => {
288 bindings.supports_free_threaded()
289 }
290 BridgeModel::Bin(None) => true,
291 BridgeModel::Cffi | BridgeModel::UniFfi => false,
292 }
293 }
294 }
295
296 impl fmt::Display for BridgeModel {
297 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
298 match self {
299 BridgeModel::Bin(Some(bindings)) => write!(f, "{} bin", bindings.crate_name),
300 BridgeModel::Bin(None) => write!(f, "bin"),
301 BridgeModel::PyO3(bindings) => write!(f, "{}", bindings.crate_name),
302 BridgeModel::Cffi => write!(f, "cffi"),
303 BridgeModel::UniFfi => write!(f, "uniffi"),
304 }
305 }
306 }