Skip to content

Conversation

@JuroOravec
Copy link
Contributor

Just refactor so that the next PR contains only code related to the Django template AST parser.

ValueError: If the HTML is malformed or cannot be parsed.
"""
...
from djc_core.djc_html_transformer import *
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Rust->Python bindings by maturin / PyO3 don't generate typings, so a __init__.pyi file is needed so that Mypy, Pyright, etc, are able to know what API the final package exposes.

To keep it clean, each Rust crate has it's own .pyi file, and then this __init__.pyi brings it all together.

/// A Result containing either:
/// - Ok((html, captured)): A tuple with the transformed HTML and captured attributes
/// - Err(error): An error if the HTML is malformed or cannot be parsed.
pub fn set_html_attributes(
Copy link
Contributor Author

@JuroOravec JuroOravec Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously the djc-html-transformer crate contained PyO3 code (which is used for defining the Python bindings).

To make sense of the code and keep it clean, I adopted following approach:

  1. Each crate (AKA Rust package) has lib.rs (which is like Python's __init__.py). These files do not define the main logic, but only the public API of the create. So the API that's to be used by other crates.
  2. The djc-core crate imports other crates
  3. And it is only this djc-core where we define the Python API using PyO3.

So it's hard to see in this diff (would be easier if you check the raw file), but this file now practically only re-exports transform() function as set_html_attributes().

And it is in djc_core/src/lib.rs where we import this set_html_attributes() and re-export it as Python API.

@@ -0,0 +1,406 @@
use quick_xml::events::{BytesStart, Event};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not new, this was previously in djc-html-transformer/src/lib.rs. Just removed definition of the Python API from here.

#[pyo3(
text_signature = "(html, root_attributes, all_attributes, *, check_end_names=False, watch_on_attribute=None)"
)]
pub fn set_html_attributes(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here is the Python definition of set_html_attributes() function.

To actually expose it to Python, this Python function is added to the module's scope on line 12:

    m.add_function(wrap_pyfunction!(set_html_attributes, m)?)?;

@JuroOravec
Copy link
Contributor Author

cc @EmilStenstrom, just plain refactor, but comments here go over how the Rust crates are laid out.

@JuroOravec JuroOravec merged commit 28472c0 into main Oct 23, 2025
14 checks passed
@JuroOravec JuroOravec deleted the jo-refactor-move-rust-python-api-to-djc-core branch October 23, 2025 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants