Metadata-Version: 2.4
Name: nyxa-speedups
Version: 0.0.1
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: Implementation :: CPython
License-File: LICENSE
Summary: Native accelerators for the Nyxa framework (bundled with nyxa by default)
Author-email: Al-Amin Islam Nerob <alamin@aincoder.com>
License-Expression: MIT
Requires-Python: >=3.13
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Repository, https://github.com/NyxaDev/Nyxa

# nyxa-speedups

Rust accelerators for the Nyxa framework — drop-in native versions of
the hot paths (routing, headers, query/cookie parsing, multipart,
cookie signing, URL building, form validation, pagination JSON).
Bundled with `nyxa` by default; every helper has a pure-Python
reference in `nyxa._speedups` with identical results.

## Features

- **`Matcher`** — route-tree matcher built by `RouteTree`:
  `Matcher()`, `add_node() -> id`, `add_static(parent, segment,
  child)`, `add_param(parent, name, pattern, child)` (compiled regex
  or `None`), `set_wildcard(parent, child)`, `add_handler(node,
  methods, route)`, `find(method, path) -> (route, params) | None`.
- **HTTP parsing** — `parse_query(qs)`, `parse_query_multi(qs)`,
  `parse_cookies(header)`, `scan_headers(...)`, `encode_headers(...)`,
  `parse_multipart(body, boundary, max_fields, max_files,
  max_part_size=None)` (bounded).
- **`HeaderIndex`** — `header_index(raw)` builds a byte-exact index
  over ASGI `[(bytes, bytes)]` pairs: `idx.get(name)`, `idx.items()`;
  plus `flatten_headers(headers)` and `normalize_headers(...)`.
- **Signed cookies** — `sign_cookie(value, key, timestamp=False)`,
  `verify_cookie(value, key, max_age=None)` — the same format the
  session middleware uses.
- **URLs** — `url_build(...)` for fast path construction.
- **Validation** — `validate_plan(plan, data)` evaluates compiled
  `nyxa-forms` rule plans natively; `NATIVE_RULE_IDS` maps rule names
  to the ids the native engine covers.
- **Pagination JSON** — `paginate_to_json(rows, meta)` encodes
  `{"data": rows, "meta": meta}` when every cell is JSON-safe.
- **Graceful fallback** — each helper returns `None` for inputs it
  doesn't cover (non-ASCII, quoted cookies, unusual items) and the
  framework transparently falls back to the Python reference —
  results are always identical.
- **Kill switch** — `NYXA_NO_SPEEDUPS=1` forces the Python
  implementations.

## Install

```bash
pip install nyxa    # nyxa-speedups (maturin wheel) is bundled by default
# standalone: pip install nyxa-speedups
```

No configuration — the framework probes the module at import and
uses it where available.

## Public API

Module `nyxa_speedups` (native extension):

- **`Matcher`** — the route-tree pyclass (see Features).
- **`HeaderIndex`** — header-pair index: `get(name) -> str | None`,
  `items() -> [(name, value)]`.
- **`header_index(raw)`** — build a `HeaderIndex` or `None`.
- **`parse_query(qs)`** — `dict[str, str]`, last value wins.
- **`parse_query_multi(qs)`** — repeated keys collapse to lists.
- **`parse_cookies(header)`** — `Cookie` header → `dict[str, str]`.
- **`scan_headers(...)`** / **`encode_headers(...)`** — header
  scanning/serialization used by request/response paths.
- **`parse_multipart(body, boundary, max_fields, max_files,
  max_part_size=None)`** — bounded multipart parse.
- **`flatten_headers(headers)`** — `str | list` values → wire pairs.
- **`normalize_headers(...)`** — header map normalization.
- **`sign_cookie(value, key, timestamp=False)`** → signed string.
- **`verify_cookie(value, key, max_age=None)`** → payload or `None`.
- **`url_build(...)`** — fast URL construction.
- **`validate_plan(plan, data)`** — native rule-plan evaluation or
  `None` to defer to Python.
- **`paginate_to_json(rows, meta)`** — JSON bytes or `None`.
- **`NATIVE_RULE_IDS`** — `dict[str, int]` of rules the native
  validator covers.

All functions return `None` on uncovered input — callers fall back to
Python for full semantics.

## Configuration

`NYXA_NO_SPEEDUPS=1` disables all acceleration.

## License

MIT

