Skip to content

Commit 68cb361

Browse files
committed
Migrated to PyO3 v0.27
1 parent a16b410 commit 68cb361

14 files changed

Lines changed: 78 additions & 67 deletions

File tree

Cargo.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nelsie/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ thiserror = { workspace = true }
1515
xmltree = { workspace = true }
1616
smallvec = { workspace = true }
1717

18-
pyo3 = { version = "0.26", features = ["abi3-py311", "extension-module"] }
18+
pyo3 = { version = "0.27", features = ["abi3-py311", "extension-module"] }
1919
itertools = "0.14"
2020
strict-num = "*"
2121
imagesize = { version = "0.14", default-features = false, features = ["jpeg", "png"] }

nelsie/src/common/steps.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use pyo3::exceptions::PyException;
2-
use pyo3::types::{PyAnyMethods, PyTuple};
3-
use pyo3::{Bound, FromPyObject, IntoPyObject, IntoPyObjectExt, PyAny, PyErr, PyResult, Python};
4-
use smallvec::{smallvec, SmallVec};
2+
use pyo3::types::PyTuple;
3+
use pyo3::{
4+
Borrowed, Bound, FromPyObject, IntoPyObject, IntoPyObjectExt, PyAny, PyErr, PyResult, Python,
5+
};
6+
use smallvec::{SmallVec, smallvec};
57
use std::cmp::Ordering;
68
use std::fmt::{Debug, Display, Formatter};
79

@@ -94,8 +96,9 @@ impl<'py> IntoPyObject<'py> for &Step {
9496
}
9597
}
9698

97-
impl<'py> FromPyObject<'py> for Step {
98-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
99+
impl<'py> FromPyObject<'_, 'py> for Step {
100+
type Error = PyErr;
101+
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
99102
if let Ok(v) = ob.extract::<u32>() {
100103
return Ok(Step::from_int(v));
101104
}

nelsie/src/pyinterface/check.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use pyo3::exceptions::PyException;
22
use pyo3::types::PyAnyMethods;
3-
use pyo3::{pyfunction, Bound, PyAny, PyResult};
3+
use pyo3::{Bound, PyAny, PyResult, pyfunction};
44
use std::str::FromStr;
55

66
/// Formats the sum of two numbers as string.
77
#[pyfunction]
88
pub(crate) fn check_color<'py>(obj: &Bound<'py, PyAny>) -> PyResult<()> {
9-
if let Ok(s) = obj.extract::<&str>() {
10-
if renderer::Color::from_str(s).is_ok() {
11-
return Ok(());
12-
}
9+
if let Ok(s) = obj.extract::<&str>()
10+
&& renderer::Color::from_str(s).is_ok()
11+
{
12+
return Ok(());
1313
}
1414
Err(PyException::new_err(format!("Invalid color: '{}'", obj)))
1515
}

nelsie/src/pyinterface/common.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use pyo3::types::PyAnyMethods;
2-
use pyo3::{Bound, FromPyObject, PyAny, PyResult};
1+
use pyo3::{Borrowed, FromPyObject, PyAny, PyErr, PyResult};
32
use renderer::Color;
43
use std::str::FromStr;
54

65
pub(crate) struct PyColor(Color);
76

8-
impl<'py> FromPyObject<'py> for PyColor {
9-
fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self> {
7+
impl<'py> FromPyObject<'_, 'py> for PyColor {
8+
type Error = PyErr;
9+
fn extract(obj: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
1010
let s: &str = obj.extract()?;
1111
Ok(PyColor(Color::from_str(s).map_err(crate::Error::from)?))
1212
}

nelsie/src/pyinterface/extract.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::pyinterface::text::PyTextContent;
77
use pyo3::exceptions::PyValueError;
88
use pyo3::prelude::PyAnyMethods;
99
use pyo3::types::PyList;
10-
use pyo3::{intern, Bound, FromPyObject, PyAny, PyResult};
10+
use pyo3::{Borrowed, Bound, FromPyObject, PyAny, PyErr, PyResult, intern};
1111
use renderer::taffy::style_helpers::{
1212
FromFlex, FromLength, FromPercent, TaffyGridLine, TaffyGridSpan,
1313
};
@@ -29,8 +29,9 @@ struct PyPage<'py> {
2929

3030
struct PyLengthOrExpr(LengthOrExpr);
3131

32-
impl<'py> FromPyObject<'py> for PyLengthOrExpr {
33-
fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self> {
32+
impl<'py> FromPyObject<'_, 'py> for PyLengthOrExpr {
33+
type Error = PyErr;
34+
fn extract(obj: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
3435
Ok(PyLengthOrExpr(if let Ok(value) = obj.extract::<f32>() {
3536
LengthOrExpr::points(value)
3637
} else if let Ok(value) = obj.extract::<&str>() {
@@ -43,8 +44,9 @@ impl<'py> FromPyObject<'py> for PyLengthOrExpr {
4344

4445
struct PyLength(Length);
4546

46-
impl<'py> FromPyObject<'py> for PyLength {
47-
fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self> {
47+
impl<'py> FromPyObject<'_, 'py> for PyLength {
48+
type Error = PyErr;
49+
fn extract(obj: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
4850
Ok(PyLength(if let Ok(value) = obj.extract::<f32>() {
4951
Length::Points { value }
5052
} else if let Ok(value) = obj.extract::<&str>() {
@@ -57,8 +59,9 @@ impl<'py> FromPyObject<'py> for PyLength {
5759

5860
struct PyLengthOrAuto(LengthOrAuto);
5961

60-
impl<'py> FromPyObject<'py> for PyLengthOrAuto {
61-
fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self> {
62+
impl<'py> FromPyObject<'_, 'py> for PyLengthOrAuto {
63+
type Error = PyErr;
64+
fn extract(obj: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
6265
Ok(PyLengthOrAuto(if let Ok(value) = obj.extract::<f32>() {
6366
LengthOrAuto::Length(Length::Points { value })
6467
} else if let Ok(value) = obj.extract::<&str>() {
@@ -71,8 +74,9 @@ impl<'py> FromPyObject<'py> for PyLengthOrAuto {
7174

7275
struct PyAlignItems(AlignItems);
7376

74-
impl<'py> FromPyObject<'py> for PyAlignItems {
75-
fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self> {
77+
impl<'py> FromPyObject<'_, 'py> for PyAlignItems {
78+
type Error = PyErr;
79+
fn extract(obj: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
7680
let s = obj.extract::<&str>()?;
7781
Ok(PyAlignItems(match s {
7882
"start" => AlignItems::Start,
@@ -95,8 +99,9 @@ impl From<PyAlignItems> for AlignItems {
9599

96100
struct PyAlignContent(AlignContent);
97101

98-
impl<'py> FromPyObject<'py> for PyAlignContent {
99-
fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self> {
102+
impl<'py> FromPyObject<'_, 'py> for PyAlignContent {
103+
type Error = PyErr;
104+
fn extract(obj: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
100105
let s = obj.extract::<&str>()?;
101106
Ok(PyAlignContent(match s {
102107
"start" => AlignContent::Start,
@@ -121,8 +126,9 @@ impl From<PyAlignContent> for AlignContent {
121126

122127
struct PyGridTemplateItem(NonRepeatedTrackSizingFunction);
123128

124-
impl<'py> FromPyObject<'py> for PyGridTemplateItem {
125-
fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self> {
129+
impl<'py> FromPyObject<'_, 'py> for PyGridTemplateItem {
130+
type Error = PyErr;
131+
fn extract(obj: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
126132
Ok(PyGridTemplateItem(
127133
if let Ok(value) = obj.extract::<f32>() {
128134
NonRepeatedTrackSizingFunction::from_length(value)
@@ -156,7 +162,7 @@ impl From<PyGridTemplateItem> for NonRepeatedTrackSizingFunction {
156162

157163
struct PyGridLinePlacement(Line<GridPlacement>);
158164

159-
fn parse_grid_placement_item(obj: &Bound<PyAny>) -> PyResult<GridPlacement> {
165+
fn parse_grid_placement_item(obj: Borrowed<'_, '_, PyAny>) -> PyResult<GridPlacement> {
160166
if obj.is_none() {
161167
return Ok(GridPlacement::Auto);
162168
}
@@ -180,20 +186,19 @@ fn parse_grid_placement_item(obj: &Bound<PyAny>) -> PyResult<GridPlacement> {
180186
}
181187
}
182188

183-
impl<'py> FromPyObject<'py> for PyGridLinePlacement {
184-
fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self> {
189+
impl<'py> FromPyObject<'_, 'py> for PyGridLinePlacement {
190+
type Error = PyErr;
191+
fn extract(obj: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
185192
if let Ok(value) = parse_grid_placement_item(obj) {
186193
return Ok(PyGridLinePlacement(Line {
187194
start: value,
188195
end: GridPlacement::Auto,
189196
}));
190197
} else if let Ok((value1, value2)) = obj.extract::<(Bound<'py, PyAny>, Bound<'py, PyAny>)>()
198+
&& let Ok(start) = parse_grid_placement_item(value1.as_borrowed())
199+
&& let Ok(end) = parse_grid_placement_item(value2.as_borrowed())
191200
{
192-
if let Ok(start) = parse_grid_placement_item(&value1) {
193-
if let Ok(end) = parse_grid_placement_item(&value2) {
194-
return Ok(PyGridLinePlacement(Line { start, end }));
195-
}
196-
}
201+
return Ok(PyGridLinePlacement(Line { start, end }));
197202
}
198203
Err(PyValueError::new_err("Invalid grid placement"))
199204
}

nelsie/src/pyinterface/image.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use super::ora::create_ora;
2-
use crate::common::steps::{bool_at_step, Step};
2+
use crate::common::steps::{Step, bool_at_step};
33
use crate::parsers::steps::parse_bool_steps;
44
use itertools::Itertools;
55
use pyo3::exceptions::{PyException, PyValueError};
66
use pyo3::types::PyAnyMethods;
77
use pyo3::{
8-
pyclass, pyfunction, pymethods, Bound, FromPyObject, IntoPyObject, PyAny, PyResult, Python,
8+
Borrowed, Bound, FromPyObject, IntoPyObject, PyAny, PyErr, PyResult, Python, pyclass,
9+
pyfunction, pymethods,
910
};
1011
use renderer::{InMemoryBinImage, InMemorySvgImage, Rectangle};
1112
use resvg::usvg::roxmltree;
@@ -292,8 +293,9 @@ pub(crate) enum PyImageFormat {
292293
Svg,
293294
}
294295

295-
impl<'py> FromPyObject<'py> for PyImageFormat {
296-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
296+
impl<'py> FromPyObject<'_, 'py> for PyImageFormat {
297+
type Error = PyErr;
298+
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
297299
let s: &str = ob.extract()?;
298300
Ok(match s {
299301
"png" => Self::Png,

nelsie/src/pyinterface/layoutexpr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use pyo3::exceptions::PyValueError;
22
use pyo3::types::PyAnyMethods;
3-
use pyo3::{intern, Bound, PyAny, PyResult};
3+
use pyo3::{Borrowed, Bound, PyAny, PyResult, intern};
44
use renderer::{InlineId, LayoutExpr, NodeId};
55

66
// #[derive(Debug)]
@@ -21,7 +21,7 @@ use renderer::{InlineId, LayoutExpr, NodeId};
2121
// }
2222
// }
2323

24-
pub(crate) fn extract_layout_expr(obj: &Bound<PyAny>) -> PyResult<LayoutExpr> {
24+
pub(crate) fn extract_layout_expr(obj: Borrowed<'_, '_, PyAny>) -> PyResult<LayoutExpr> {
2525
if let Ok(value) = obj.extract() {
2626
return Ok(LayoutExpr::const_value(value));
2727
}
@@ -35,8 +35,8 @@ pub(crate) fn extract_layout_expr(obj: &Bound<PyAny>) -> PyResult<LayoutExpr> {
3535
let name: &str = op.extract()?;
3636
match name {
3737
"+" | "-" | "*" => {
38-
let expr_a = extract_layout_expr(&v0)?;
39-
let expr_b = extract_layout_expr(&v1)?;
38+
let expr_a = extract_layout_expr(v0.as_borrowed())?;
39+
let expr_b = extract_layout_expr(v1.as_borrowed())?;
4040
Ok(match name {
4141
"+" => LayoutExpr::add(expr_a, expr_b),
4242
"-" => LayoutExpr::sub(expr_a, expr_b),
@@ -97,7 +97,7 @@ pub(crate) fn extract_layout_expr(obj: &Bound<PyAny>) -> PyResult<LayoutExpr> {
9797
"max" => Ok(LayoutExpr::max({
9898
let v: Vec<Bound<PyAny>> = v0.extract()?;
9999
v.into_iter()
100-
.map(|obj| extract_layout_expr(&obj))
100+
.map(|obj| extract_layout_expr(obj.as_borrowed()))
101101
.collect::<PyResult<_>>()?
102102
})),
103103
_ => Err(PyValueError::new_err("Invalid expression")),

nelsie/src/pyinterface/parsers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use pyo3::types::{PyDict, PyDictMethods, PyList};
2-
use pyo3::{pyfunction, Bound, IntoPyObject, IntoPyObjectExt, PyAny, PyResult, Python};
2+
use pyo3::{Bound, IntoPyObject, IntoPyObjectExt, PyAny, PyResult, Python, pyfunction};
33
use std::collections::BTreeMap;
44

55
// fn step_to_pyobj<'py>(py: Python<'py>, step: &Step) -> PyResult<Bound<'py, PyAny>> {

nelsie/src/pyinterface/rendering.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::pyinterface::extract::obj_to_page;
22
use crate::pyinterface::resources::Resources;
33
use pyo3::exceptions::PyException;
44
use pyo3::types::{PyDict, PyDictMethods, PyList};
5-
use pyo3::{pyfunction, Bound, IntoPyObjectExt, PyAny, PyResult, Python};
5+
use pyo3::{Bound, IntoPyObjectExt, PyAny, PyResult, Python, pyfunction};
66
use renderer::{Document, PageLayout, Register, RenderingOptions};
77
use std::collections::HashMap;
88

@@ -128,7 +128,7 @@ fn run_rendering(
128128
_ => {
129129
return Err(PyException::new_err(format!(
130130
"Invalid output format: {format}"
131-
)))
131+
)));
132132
}
133133
})
134134
}

0 commit comments

Comments
 (0)