Skip to content

Commit

Permalink
fix: rewrite SerdeIntoPyObject struct as function (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
monosans authored Nov 17, 2024
1 parent 6cb9b7e commit f3a595a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 81 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

## [0.6.1] - 2024-11-16

- Bump `pyo3` to `v0.23.0`.
- Refactor the `serde_json::Value` to Python conversion code.
- Bump `pyo3` from `v0.22.6` to `v0.23.1`.

## [0.6.0] - 2024-11-03

Expand Down
6 changes: 3 additions & 3 deletions src/class_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ impl Markdown {
markdown: &str,
merge_text: bool,
) -> PyResult<Bound<'py, PyAny>> {
py.allow_threads(move || {
let v = py.allow_threads(move || {
crate::common::parse_events(markdown, self.0, merge_text)
})?
.into_pyobject(py)
})?;
crate::common::serde_into_py(py, &v)
}

/// Examples:
Expand Down
60 changes: 53 additions & 7 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pyo3::prelude::*;
use pyo3::{prelude::*, BoundObject};

pub(crate) fn build_options(options: Option<u32>) -> pulldown_cmark::Options {
match options {
Expand All @@ -18,17 +18,63 @@ pub(crate) fn parse_events(
markdown: &str,
options: pulldown_cmark::Options,
merge_text: bool,
) -> PyResult<crate::serde_into_pyobject::SerdeIntoPyObject> {
) -> PyResult<serde_json::Value> {
let parser = pulldown_cmark::Parser::new_ext(markdown, options);
let iterator: Box<dyn Iterator<Item = pulldown_cmark::Event>> =
if merge_text {
Box::new(pulldown_cmark::TextMergeStream::new(parser))
} else {
Box::new(parser)
};
Ok(crate::serde_into_pyobject::SerdeIntoPyObject(
serde_json::to_value(iterator.collect::<Vec<_>>()).map_err(
move |err| pyo3::exceptions::PyValueError::new_err(err.to_string()),
)?,
))
serde_json::to_value(iterator.collect::<Vec<_>>()).map_err(move |err| {
pyo3::exceptions::PyValueError::new_err(err.to_string())
})
}

pub(crate) fn serde_into_py<'py>(
py: Python<'py>,
v: &serde_json::Value,
) -> PyResult<Bound<'py, PyAny>> {
match v {
serde_json::Value::Null => Ok(py.None().into_bound(py).into_any()),
serde_json::Value::Bool(b) => {
Ok(b.into_pyobject(py)?.into_bound().into_any())
}
serde_json::Value::Number(n) => {
if let Some(i) = n.as_i64() {
Ok(i.into_pyobject(py)?.into_any())
} else if let Some(u) = n.as_u64() {
Ok(u.into_pyobject(py)?.into_any())
} else if let Some(f) = n.as_f64() {
Ok(f.into_pyobject(py)?.into_any())
} else {
Err(pyo3::exceptions::PyValueError::new_err("Invalid number"))
}
}
serde_json::Value::String(s) => Ok(s.into_pyobject(py)?.into_any()),
serde_json::Value::Array(arr) => Ok(pyo3::types::PyTuple::new(
py,
arr.iter()
.map(move |v| serde_into_py(py, v))
.collect::<Result<Vec<_>, _>>()?,
)?
.into_any()),
serde_json::Value::Object(obj) => Ok(
// Return tuple instead of a dict if dict contains one key
if obj.len() == 1 {
let (k, v) = obj.iter().next().unwrap();
pyo3::types::PyTuple::new(
py,
&[k.into_pyobject(py)?.into_any(), serde_into_py(py, v)?],
)?
.into_any()
} else {
let py_dict = pyo3::types::PyDict::new(py);
for (k, v) in obj {
py_dict.set_item(k, serde_into_py(py, v)?)?;
}
py_dict.into_any()
},
),
}
}
6 changes: 3 additions & 3 deletions src/function_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ pub(crate) fn events<'py>(
options: Option<u32>,
merge_text: bool,
) -> PyResult<Bound<'py, PyAny>> {
py.allow_threads(move || {
let v = py.allow_threads(move || {
crate::common::parse_events(
markdown,
crate::common::build_options(options),
merge_text,
)
})?
.into_pyobject(py)
})?;
crate::common::serde_into_py(py, &v)
}

/// Examples:
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod class_api;
mod common;
mod function_api;
mod serde_into_pyobject;
use pyo3::prelude::*;

#[pymodule]
Expand Down
65 changes: 0 additions & 65 deletions src/serde_into_pyobject.rs

This file was deleted.

0 comments on commit f3a595a

Please sign in to comment.