rust-io 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rust_io-0.1.0/PKG-INFO +227 -0
- rust_io-0.1.0/README.md +208 -0
- rust_io-0.1.0/files-io/Cargo.lock +3004 -0
- rust_io-0.1.0/files-io/Cargo.toml +27 -0
- rust_io-0.1.0/files-io/README.md +403 -0
- rust_io-0.1.0/files-io/pyproject.toml +15 -0
- rust_io-0.1.0/files-io/src/archive/mod.rs +2 -0
- rust_io-0.1.0/files-io/src/archive/tar_gz.rs +145 -0
- rust_io-0.1.0/files-io/src/archive/zip.rs +115 -0
- rust_io-0.1.0/files-io/src/backends/local.rs +143 -0
- rust_io-0.1.0/files-io/src/backends/mod.rs +40 -0
- rust_io-0.1.0/files-io/src/backends/s3.rs +307 -0
- rust_io-0.1.0/files-io/src/error.rs +86 -0
- rust_io-0.1.0/files-io/src/hash.rs +26 -0
- rust_io-0.1.0/files-io/src/lib.rs +5 -0
- rust_io-0.1.0/files-io/src/py/dedup.rs +39 -0
- rust_io-0.1.0/files-io/src/py/file.rs +250 -0
- rust_io-0.1.0/files-io/src/py/mod.rs +4 -0
- rust_io-0.1.0/files-io/src/py/parallel.rs +175 -0
- rust_io-0.1.0/files-io/src/py/utils.rs +19 -0
- rust_io-0.1.0/files-io/tests/archive_security.rs +197 -0
- rust_io-0.1.0/files-io/tests/py_utils.rs +27 -0
- rust_io-0.1.0/files-io/tests/test_files_io.py +150 -0
- rust_io-0.1.0/files-io/uv.lock +7 -0
- rust_io-0.1.0/net-io/Cargo.lock +2189 -0
- rust_io-0.1.0/net-io/Cargo.toml +24 -0
- rust_io-0.1.0/net-io/README.md +569 -0
- rust_io-0.1.0/net-io/src/client.rs +208 -0
- rust_io-0.1.0/net-io/src/error.rs +75 -0
- rust_io-0.1.0/net-io/src/lib.rs +7 -0
- rust_io-0.1.0/net-io/src/mineru.rs +546 -0
- rust_io-0.1.0/net-io/src/providers/crossref.rs +96 -0
- rust_io-0.1.0/net-io/src/providers/doaj.rs +113 -0
- rust_io-0.1.0/net-io/src/providers/europepmc.rs +128 -0
- rust_io-0.1.0/net-io/src/providers/jstage.rs +119 -0
- rust_io-0.1.0/net-io/src/providers/mod.rs +15 -0
- rust_io-0.1.0/net-io/src/providers/openalex.rs +71 -0
- rust_io-0.1.0/net-io/src/providers/pmc.rs +192 -0
- rust_io-0.1.0/net-io/src/providers/unpaywall.rs +62 -0
- rust_io-0.1.0/net-io/src/py.rs +687 -0
- rust_io-0.1.0/net-io/src/scraper.rs +218 -0
- rust_io-0.1.0/net-io/src/types.rs +123 -0
- rust_io-0.1.0/pyproject.toml +31 -0
- rust_io-0.1.0/rust-io/.github/workflows/CI.yml +186 -0
- rust_io-0.1.0/rust-io/.gitignore +96 -0
- rust_io-0.1.0/rust-io/Cargo.lock +3793 -0
- rust_io-0.1.0/rust-io/Cargo.toml +16 -0
- rust_io-0.1.0/rust-io/README.md +208 -0
- rust_io-0.1.0/rust-io/src/lib.rs +80 -0
- rust_io-0.1.0/rust-io/uv.lock +7 -0
rust_io-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rust-io
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Classifier: Programming Language :: Rust
|
|
5
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
9
|
+
Classifier: Operating System :: MacOS
|
|
10
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
11
|
+
Summary: High-performance Rust native extensions for ACMG Lingua: literature search, MinerU document parsing, file I/O, S3, archives, and SHA-256 dedup.
|
|
12
|
+
Author: ACMG Lingua Team
|
|
13
|
+
License-Expression: MIT
|
|
14
|
+
Requires-Python: >=3.12
|
|
15
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
16
|
+
Project-URL: Homepage, https://github.com/anthropics/acmg-lingua
|
|
17
|
+
Project-URL: Repository, https://github.com/anthropics/acmg-lingua
|
|
18
|
+
|
|
19
|
+
# rust-io
|
|
20
|
+
|
|
21
|
+
> PyO3 facade crate for ACMG Lingua's Rust native extensions. Registers two Python submodules — `rust_io.net` and `rust_io.files` — backed by the `net-io` and `files-io` rlib crates.
|
|
22
|
+
|
|
23
|
+
## Python Usage
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
import rust_io.net as net_io
|
|
27
|
+
import rust_io.files as files
|
|
28
|
+
|
|
29
|
+
# Literature acquisition
|
|
30
|
+
result = await net_io.fetch_one("crossref", "search", {"query": "CRISPR"})
|
|
31
|
+
|
|
32
|
+
# MinerU document parsing
|
|
33
|
+
task = await net_io.mineru_create_task("https://example.com/paper.pdf", token="...")
|
|
34
|
+
|
|
35
|
+
# File I/O
|
|
36
|
+
f = files.File("/tmp/data/report.txt")
|
|
37
|
+
f.write("patient variant report")
|
|
38
|
+
content = f.read(as_text=True)
|
|
39
|
+
|
|
40
|
+
# SHA-256 hashing and dedup
|
|
41
|
+
sha = files.compute_sha256("/tmp/data/report.txt")
|
|
42
|
+
dup_result = files.check_duplicate("/tmp/data/report.txt", [sha])
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Build & Install
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
cd backend
|
|
49
|
+
uv run maturin develop --release -m libs/rust-io/Cargo.toml
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Requires Python >= 3.8.
|
|
53
|
+
|
|
54
|
+
## Architecture
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
rust-io (cdylib + rlib)
|
|
58
|
+
│
|
|
59
|
+
├── src/lib.rs # #[pymodule] facade — registers two submodules
|
|
60
|
+
│
|
|
61
|
+
├── files-io (rlib dep) # File I/O, S3, archives, SHA-256 dedup
|
|
62
|
+
│ ├── src/py/file.rs # File #[pyclass]
|
|
63
|
+
│ ├── src/py/parallel.rs # batch_copy, batch_compress, batch_copy_async
|
|
64
|
+
│ ├── src/py/dedup.rs # check_duplicate, batch_hash
|
|
65
|
+
│ └── src/py/utils.rs # compute_sha256, write_file, validate_pdf_magic
|
|
66
|
+
│
|
|
67
|
+
└── net-io (rlib dep) # HTTP/web I/O (7 providers, scraper, MinerU API)
|
|
68
|
+
└── src/py.rs # fetch_one, fetch_multi, scrape_web, scrape_html,
|
|
69
|
+
# extract_pdf_links, mineru_* (8 functions)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
This crate contains no business logic. `src/lib.rs` only:
|
|
73
|
+
1. Creates a `PyModule` for each submodule (`"net"`, `"files"`)
|
|
74
|
+
2. Adds `#[pyfunction]`s and `#[pyclass]`es from sub-crates via `wrap_pyfunction!` / `add_class`
|
|
75
|
+
3. Registers each submodule in `sys.modules` via `register_submodule()` so `import rust_io.net` and `import rust_io.files` work as top-level imports
|
|
76
|
+
|
|
77
|
+
The `register_submodule` helper calls `parent.add_submodule(submodule)` and then sets `sys.modules["rust_io.<name>"]` to the submodule. This is necessary because PyO3 modules are nested by default; without sys.modules registration, `import rust_io.net` would fail.
|
|
78
|
+
|
|
79
|
+
## Exports
|
|
80
|
+
|
|
81
|
+
### `rust_io.net`
|
|
82
|
+
|
|
83
|
+
#### Literature Providers
|
|
84
|
+
|
|
85
|
+
| Function | Signature | Description |
|
|
86
|
+
|----------|-----------|-------------|
|
|
87
|
+
| `fetch_one` | `(provider, action, params, timeout_ms=None, max_retries=None, proxy=None) -> dict` | Single-provider search/download. Action must be `"search"` or `"download"`. |
|
|
88
|
+
| `fetch_multi` | `(providers, action, params, timeout_ms=None, max_retries=None, proxy=None) -> list[dict]` | Multi-provider parallel search via `join_all`. Failures captured per-provider, not propagated. |
|
|
89
|
+
| `scrape_web` | `(provider, action, params, timeout_ms=None, max_retries=None, proxy=None) -> dict` | Generic web scraping. Fetches HTML, extracts elements by CSS selector. Derives URL from `params.detail_link` or `params.query`. |
|
|
90
|
+
| `scrape_html` | `(html, css_selector) -> list[dict]` | CSS selector extraction from HTML. Returns `[{text, html, tag_name, attrs}, ...]`. |
|
|
91
|
+
| `extract_pdf_links` | `(html, base_url) -> list[str]` | Extract PDF URLs from `<a href>` and `<meta citation_pdf_url>`. Resolves relative URLs against `base_url`. |
|
|
92
|
+
|
|
93
|
+
#### MinerU Document Parsing
|
|
94
|
+
|
|
95
|
+
| Function | Signature | Description |
|
|
96
|
+
|----------|-----------|-------------|
|
|
97
|
+
| `mineru_create_task` | `(url, token, model_version=None, is_ocr=None, enable_formula=None, enable_table=None, language=None, data_id=None, page_ranges=None, no_cache=None, cache_tolerance=None, timeout_ms=None, proxy=None) -> dict` | Create a single document parsing task. POST to MinerU API v4. |
|
|
98
|
+
| `mineru_get_result` | `(task_id, token, timeout_ms=None, proxy=None) -> dict` | Get single task result by ID. |
|
|
99
|
+
| `mineru_batch_submit` | `(files, token, model_version=None, enable_formula=None, enable_table=None, language=None, no_cache=None, cache_tolerance=None, timeout_ms=None, proxy=None) -> dict` | Submit batch parsing tasks. `files` is `list[dict]` with `url`, optional `data_id`, `is_ocr`, `page_ranges`. |
|
|
100
|
+
| `mineru_batch_result` | `(batch_id, token, timeout_ms=None, proxy=None) -> dict` | Get batch results by batch ID. |
|
|
101
|
+
| `mineru_create_upload_url` | `(filename, token, content_type=None, model_version=None, is_ocr=None, enable_formula=None, enable_table=None, language=None, data_id=None, page_ranges=None, no_cache=None, cache_tolerance=None, timeout_ms=None, proxy=None) -> dict` | Get a pre-signed upload URL for a local file (single). POST to MinerU API v4 `/file-urls/batch`. |
|
|
102
|
+
| `mineru_create_batch_upload_urls` | `(files, token, model_version=None, enable_formula=None, enable_table=None, language=None, callback=None, seed=None, extra_formats=None, timeout_ms=None, proxy=None) -> dict` | Get pre-signed upload URLs for multiple local files. Uploaded files are auto-submitted by MinerU. |
|
|
103
|
+
| `mineru_upload_local_files` | `(file_paths, token, model_version=None, enable_formula=None, enable_table=None, language=None, data_ids=None, is_ocr=None, page_ranges=None, callback=None, seed=None, extra_formats=None, timeout_ms=None, proxy=None) -> dict` | Create upload URLs and upload local files. MinerU auto-submits after upload. |
|
|
104
|
+
| `mineru_upload_local_file` | `(upload_url, file_path, content_type=None, timeout_ms=None, proxy=None) -> dict` | Upload a single local file to a pre-signed URL. |
|
|
105
|
+
### `rust_io.files`
|
|
106
|
+
|
|
107
|
+
| Item | Type | Description |
|
|
108
|
+
|------|------|-------------|
|
|
109
|
+
| `File` | class | Primary file I/O class. Path prefix (`s3://` vs local) auto-selects backend. Supports read, write, metadata, copy, rename, remove, list_dir, compress, extract, content_hash, and async variants. |
|
|
110
|
+
| `batch_copy` | function | `(sources: list[str], destinations: list[str], access_key=None, secret_key=None, endpoint=None, region=None) -> dict` — sequential copy with mixed local/S3 support. |
|
|
111
|
+
| `batch_compress` | function | `(dir_paths: list[str], output_paths: list[str], format="zip") -> dict` — sequential directory compression. |
|
|
112
|
+
| `batch_copy_async` | function | `(sources, destinations, access_key=None, secret_key=None, endpoint=None, region=None) -> dict` — non-blocking variant of `batch_copy`. |
|
|
113
|
+
| `check_duplicate` | function | `(file_path: str, known_hashes: list[str]) -> dict` — SHA-256 hash a file and check against known hashes. Returns `{"hash": str, "is_duplicate": bool}`. |
|
|
114
|
+
| `batch_hash` | function | `(file_paths: list[str]) -> dict` — SHA-256 hash multiple files. Returns `{"hashes": {path: hash}, "errors": {path: message}}`. |
|
|
115
|
+
| `compute_sha256` | function | `(file_path: str) -> str` — legacy helper; thin wrapper over `hash::hash_file`. Raises `IOError`. |
|
|
116
|
+
| `write_file` | function | `(file_path: str, data: bytes) -> None` — legacy helper; thin wrapper over `std::fs::write`. Raises `IOError`. |
|
|
117
|
+
| `validate_pdf_magic` | function | `(data: bytes) -> bool` — returns `True` if data starts with `%PDF`. |
|
|
118
|
+
|
|
119
|
+
## Sub-crate Docs
|
|
120
|
+
|
|
121
|
+
- [net-io](../net-io/) — HTTP/web I/O providers + MinerU document parsing
|
|
122
|
+
- [files-io](../files-io/) — File I/O, S3, archives, SHA-256 dedup
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
## Concurrency Model
|
|
126
|
+
|
|
127
|
+
All async Python functions (`fetch_one`, `fetch_multi`, `scrape_web`, `mineru_*`, `batch_copy_async`, etc.) use `pyo3-async-runtimes` to bridge Rust `async fn` → Python coroutine. The underlying tokio runtime is managed automatically:
|
|
128
|
+
|
|
129
|
+
- **Provider I/O** (`net-io`): Each `HttpClient` is created per-call. `fetch_multi` launches all provider requests concurrently via `futures::join_all` — all providers execute in parallel on the same tokio runtime.
|
|
130
|
+
- **File I/O** (`files-io`): Synchronous `File` methods execute directly. Async variants (`*_async`) use `tokio::task::spawn_blocking` to offload blocking I/O without stalling the Python event loop. The S3 backend uses a shared `OnceLock<Runtime>` singleton for sync→async bridging.
|
|
131
|
+
- **GIL behavior**: Rust code inside `#[pyfunction]` runs with the GIL held during argument parsing, then releases it during async I/O. The GIL is re-acquired only to construct the return value via `pythonize`.
|
|
132
|
+
|
|
133
|
+
## Environment Variables
|
|
134
|
+
|
|
135
|
+
| Variable | Used by | Description |
|
|
136
|
+
|----------|---------|-------------|
|
|
137
|
+
| `UNPAYWALL_EMAIL` | `net-io` (Unpaywall provider) | Required for Unpaywall API authentication |
|
|
138
|
+
| `AWS_ACCESS_KEY_ID` | `files-io` (S3 backend) | AWS credential chain (optional if passed explicitly) |
|
|
139
|
+
| `AWS_SECRET_ACCESS_KEY` | `files-io` (S3 backend) | AWS credential chain (optional if passed explicitly) |
|
|
140
|
+
| `AWS_REGION` | `files-io` (S3 backend) | AWS region, default `us-east-1` |
|
|
141
|
+
|
|
142
|
+
S3 credential resolution uses the `aws-sdk-s3` default credential chain (env vars, `~/.aws/credentials`, IMDS). Explicit `access_key`/`secret_key`/`region` kwargs override the chain. MinerU tokens are passed per-call — they are never read from the environment.
|
|
143
|
+
|
|
144
|
+
## Error Reference
|
|
145
|
+
|
|
146
|
+
Both sub-crates map Rust error types to Python exceptions automatically via `From<Error> for PyErr`:
|
|
147
|
+
|
|
148
|
+
### `rust_io.net` (GatewayError)
|
|
149
|
+
|
|
150
|
+
| Rust variant | Python exception | Typical cause |
|
|
151
|
+
|-------------|-----------------|---------------|
|
|
152
|
+
| `Http(reqwest::Error)` | `ConnectionError` | Timeout, DNS failure, non-2xx response |
|
|
153
|
+
| `Json(serde_json::Error)` | `ValueError` | Malformed provider response |
|
|
154
|
+
| `Io(std::io::Error)` | `OSError` | File read failure (MinerU upload) |
|
|
155
|
+
| `Url(url::ParseError)` | `ValueError` | Malformed provider URL |
|
|
156
|
+
| `Provider { provider, message }` | `RuntimeError` | Unknown provider or unsupported action |
|
|
157
|
+
| `Other(String)` | `RuntimeError` | Catch-all for business logic errors |
|
|
158
|
+
|
|
159
|
+
### `rust_io.files` (FileError)
|
|
160
|
+
|
|
161
|
+
| Rust variant | Python exception | Typical cause |
|
|
162
|
+
|-------------|-----------------|---------------|
|
|
163
|
+
| `Io(std::io::Error)` | `IOError` | Permission denied, file not found |
|
|
164
|
+
| `S3(String)` | `ConnectionError` | S3 service unavailable, auth failure |
|
|
165
|
+
| `Path(String)` | `ValueError` | Path traversal attempt, invalid path |
|
|
166
|
+
| `Archive(String)` | `ValueError` | Malformed archive, extraction path error |
|
|
167
|
+
| `Zip(ZipError)` | `ValueError` | Corrupt ZIP file |
|
|
168
|
+
| `Hash(String)` | `RuntimeError` | SHA-256 computation failure |
|
|
169
|
+
| `TaskJoin(JoinError)` | `RuntimeError` | spawn_blocking task panic or cancel |
|
|
170
|
+
| `Other(String)` | `RuntimeError` | Catch-all for business logic errors |
|
|
171
|
+
|
|
172
|
+
## Development Workflow
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
# Build the Python extension (from backend/)
|
|
176
|
+
cd backend
|
|
177
|
+
uv run maturin develop --release -m libs/rust-io/Cargo.toml
|
|
178
|
+
|
|
179
|
+
# Run Rust unit tests per sub-crate
|
|
180
|
+
cargo test -p net-io
|
|
181
|
+
cargo test -p files-io
|
|
182
|
+
|
|
183
|
+
# Run Python integration tests
|
|
184
|
+
uv run pytest backend/tests/ -v
|
|
185
|
+
|
|
186
|
+
# Lint Rust code
|
|
187
|
+
cargo clippy --all-targets -- -D warnings
|
|
188
|
+
|
|
189
|
+
# Type-check Python
|
|
190
|
+
uv run ruff check
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
The `Cargo.lock` files are committed for reproducibility. After adding or updating dependencies, run `cargo check` from the crate directory (not a workspace root) to update individual lock files.
|
|
194
|
+
|
|
195
|
+
## Extension Guide
|
|
196
|
+
|
|
197
|
+
### Adding a new `rust_io.net` pyfunction
|
|
198
|
+
|
|
199
|
+
1. Implement the Rust function in `net-io/src/` (e.g. a new provider, new scraper utility)
|
|
200
|
+
2. Expose it as a `#[pyfunction]` in `net-io/src/py.rs`
|
|
201
|
+
3. Register it in `rust-io/src/lib.rs`:
|
|
202
|
+
```rust
|
|
203
|
+
net.add_function(wrap_pyfunction!(net_io::py::your_new_function, &net)?)?;
|
|
204
|
+
```
|
|
205
|
+
4. Rebuild and test
|
|
206
|
+
|
|
207
|
+
### Adding a new `rust_io.files` pyfunction
|
|
208
|
+
|
|
209
|
+
Same pattern — implement in `files-io/src/py/`, then register in `rust-io/src/lib.rs`:
|
|
210
|
+
```rust
|
|
211
|
+
files.add_function(wrap_pyfunction!(files_io::py::new_module::your_function, &files)?)?;
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Submodule registration internals
|
|
215
|
+
|
|
216
|
+
The `register_submodule` helper is required because PyO3 nests submodules by default. Without explicitly setting `sys.modules["rust_io.net"]`, Python's `import rust_io.net` would fail even though the module is attached to the parent. The helper calls both `parent.add_submodule()` and `sys.modules[full_name] = submodule`.
|
|
217
|
+
## Dependencies
|
|
218
|
+
|
|
219
|
+
| Crate | Version | Purpose |
|
|
220
|
+
|-------|---------|---------|
|
|
221
|
+
| pyo3 | 0.28.2 | Rust-to-Python bindings (`extension-module` feature) |
|
|
222
|
+
| pyo3-async-runtimes | 0.28 | async fn → Python coroutine (tokio) |
|
|
223
|
+
| files-io | path = "../files-io" | Sub-crate providing `rust_io.files` functions |
|
|
224
|
+
| net-io | path = "../net-io" | Sub-crate providing `rust_io.net` functions |
|
|
225
|
+
|
|
226
|
+
All business-logic dependencies are in the sub-crates. The facade only depends on `pyo3`, `pyo3-async-runtimes`, and the two sub-crates.
|
|
227
|
+
|
rust_io-0.1.0/README.md
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# rust-io
|
|
2
|
+
|
|
3
|
+
> PyO3 facade crate for ACMG Lingua's Rust native extensions. Registers two Python submodules — `rust_io.net` and `rust_io.files` — backed by the `net-io` and `files-io` rlib crates.
|
|
4
|
+
|
|
5
|
+
## Python Usage
|
|
6
|
+
|
|
7
|
+
```python
|
|
8
|
+
import rust_io.net as net_io
|
|
9
|
+
import rust_io.files as files
|
|
10
|
+
|
|
11
|
+
# Literature acquisition
|
|
12
|
+
result = await net_io.fetch_one("crossref", "search", {"query": "CRISPR"})
|
|
13
|
+
|
|
14
|
+
# MinerU document parsing
|
|
15
|
+
task = await net_io.mineru_create_task("https://example.com/paper.pdf", token="...")
|
|
16
|
+
|
|
17
|
+
# File I/O
|
|
18
|
+
f = files.File("/tmp/data/report.txt")
|
|
19
|
+
f.write("patient variant report")
|
|
20
|
+
content = f.read(as_text=True)
|
|
21
|
+
|
|
22
|
+
# SHA-256 hashing and dedup
|
|
23
|
+
sha = files.compute_sha256("/tmp/data/report.txt")
|
|
24
|
+
dup_result = files.check_duplicate("/tmp/data/report.txt", [sha])
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Build & Install
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
cd backend
|
|
31
|
+
uv run maturin develop --release -m libs/rust-io/Cargo.toml
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Requires Python >= 3.8.
|
|
35
|
+
|
|
36
|
+
## Architecture
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
rust-io (cdylib + rlib)
|
|
40
|
+
│
|
|
41
|
+
├── src/lib.rs # #[pymodule] facade — registers two submodules
|
|
42
|
+
│
|
|
43
|
+
├── files-io (rlib dep) # File I/O, S3, archives, SHA-256 dedup
|
|
44
|
+
│ ├── src/py/file.rs # File #[pyclass]
|
|
45
|
+
│ ├── src/py/parallel.rs # batch_copy, batch_compress, batch_copy_async
|
|
46
|
+
│ ├── src/py/dedup.rs # check_duplicate, batch_hash
|
|
47
|
+
│ └── src/py/utils.rs # compute_sha256, write_file, validate_pdf_magic
|
|
48
|
+
│
|
|
49
|
+
└── net-io (rlib dep) # HTTP/web I/O (7 providers, scraper, MinerU API)
|
|
50
|
+
└── src/py.rs # fetch_one, fetch_multi, scrape_web, scrape_html,
|
|
51
|
+
# extract_pdf_links, mineru_* (8 functions)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
This crate contains no business logic. `src/lib.rs` only:
|
|
55
|
+
1. Creates a `PyModule` for each submodule (`"net"`, `"files"`)
|
|
56
|
+
2. Adds `#[pyfunction]`s and `#[pyclass]`es from sub-crates via `wrap_pyfunction!` / `add_class`
|
|
57
|
+
3. Registers each submodule in `sys.modules` via `register_submodule()` so `import rust_io.net` and `import rust_io.files` work as top-level imports
|
|
58
|
+
|
|
59
|
+
The `register_submodule` helper calls `parent.add_submodule(submodule)` and then sets `sys.modules["rust_io.<name>"]` to the submodule. This is necessary because PyO3 modules are nested by default; without sys.modules registration, `import rust_io.net` would fail.
|
|
60
|
+
|
|
61
|
+
## Exports
|
|
62
|
+
|
|
63
|
+
### `rust_io.net`
|
|
64
|
+
|
|
65
|
+
#### Literature Providers
|
|
66
|
+
|
|
67
|
+
| Function | Signature | Description |
|
|
68
|
+
|----------|-----------|-------------|
|
|
69
|
+
| `fetch_one` | `(provider, action, params, timeout_ms=None, max_retries=None, proxy=None) -> dict` | Single-provider search/download. Action must be `"search"` or `"download"`. |
|
|
70
|
+
| `fetch_multi` | `(providers, action, params, timeout_ms=None, max_retries=None, proxy=None) -> list[dict]` | Multi-provider parallel search via `join_all`. Failures captured per-provider, not propagated. |
|
|
71
|
+
| `scrape_web` | `(provider, action, params, timeout_ms=None, max_retries=None, proxy=None) -> dict` | Generic web scraping. Fetches HTML, extracts elements by CSS selector. Derives URL from `params.detail_link` or `params.query`. |
|
|
72
|
+
| `scrape_html` | `(html, css_selector) -> list[dict]` | CSS selector extraction from HTML. Returns `[{text, html, tag_name, attrs}, ...]`. |
|
|
73
|
+
| `extract_pdf_links` | `(html, base_url) -> list[str]` | Extract PDF URLs from `<a href>` and `<meta citation_pdf_url>`. Resolves relative URLs against `base_url`. |
|
|
74
|
+
|
|
75
|
+
#### MinerU Document Parsing
|
|
76
|
+
|
|
77
|
+
| Function | Signature | Description |
|
|
78
|
+
|----------|-----------|-------------|
|
|
79
|
+
| `mineru_create_task` | `(url, token, model_version=None, is_ocr=None, enable_formula=None, enable_table=None, language=None, data_id=None, page_ranges=None, no_cache=None, cache_tolerance=None, timeout_ms=None, proxy=None) -> dict` | Create a single document parsing task. POST to MinerU API v4. |
|
|
80
|
+
| `mineru_get_result` | `(task_id, token, timeout_ms=None, proxy=None) -> dict` | Get single task result by ID. |
|
|
81
|
+
| `mineru_batch_submit` | `(files, token, model_version=None, enable_formula=None, enable_table=None, language=None, no_cache=None, cache_tolerance=None, timeout_ms=None, proxy=None) -> dict` | Submit batch parsing tasks. `files` is `list[dict]` with `url`, optional `data_id`, `is_ocr`, `page_ranges`. |
|
|
82
|
+
| `mineru_batch_result` | `(batch_id, token, timeout_ms=None, proxy=None) -> dict` | Get batch results by batch ID. |
|
|
83
|
+
| `mineru_create_upload_url` | `(filename, token, content_type=None, model_version=None, is_ocr=None, enable_formula=None, enable_table=None, language=None, data_id=None, page_ranges=None, no_cache=None, cache_tolerance=None, timeout_ms=None, proxy=None) -> dict` | Get a pre-signed upload URL for a local file (single). POST to MinerU API v4 `/file-urls/batch`. |
|
|
84
|
+
| `mineru_create_batch_upload_urls` | `(files, token, model_version=None, enable_formula=None, enable_table=None, language=None, callback=None, seed=None, extra_formats=None, timeout_ms=None, proxy=None) -> dict` | Get pre-signed upload URLs for multiple local files. Uploaded files are auto-submitted by MinerU. |
|
|
85
|
+
| `mineru_upload_local_files` | `(file_paths, token, model_version=None, enable_formula=None, enable_table=None, language=None, data_ids=None, is_ocr=None, page_ranges=None, callback=None, seed=None, extra_formats=None, timeout_ms=None, proxy=None) -> dict` | Create upload URLs and upload local files. MinerU auto-submits after upload. |
|
|
86
|
+
| `mineru_upload_local_file` | `(upload_url, file_path, content_type=None, timeout_ms=None, proxy=None) -> dict` | Upload a single local file to a pre-signed URL. |
|
|
87
|
+
### `rust_io.files`
|
|
88
|
+
|
|
89
|
+
| Item | Type | Description |
|
|
90
|
+
|------|------|-------------|
|
|
91
|
+
| `File` | class | Primary file I/O class. Path prefix (`s3://` vs local) auto-selects backend. Supports read, write, metadata, copy, rename, remove, list_dir, compress, extract, content_hash, and async variants. |
|
|
92
|
+
| `batch_copy` | function | `(sources: list[str], destinations: list[str], access_key=None, secret_key=None, endpoint=None, region=None) -> dict` — sequential copy with mixed local/S3 support. |
|
|
93
|
+
| `batch_compress` | function | `(dir_paths: list[str], output_paths: list[str], format="zip") -> dict` — sequential directory compression. |
|
|
94
|
+
| `batch_copy_async` | function | `(sources, destinations, access_key=None, secret_key=None, endpoint=None, region=None) -> dict` — non-blocking variant of `batch_copy`. |
|
|
95
|
+
| `check_duplicate` | function | `(file_path: str, known_hashes: list[str]) -> dict` — SHA-256 hash a file and check against known hashes. Returns `{"hash": str, "is_duplicate": bool}`. |
|
|
96
|
+
| `batch_hash` | function | `(file_paths: list[str]) -> dict` — SHA-256 hash multiple files. Returns `{"hashes": {path: hash}, "errors": {path: message}}`. |
|
|
97
|
+
| `compute_sha256` | function | `(file_path: str) -> str` — legacy helper; thin wrapper over `hash::hash_file`. Raises `IOError`. |
|
|
98
|
+
| `write_file` | function | `(file_path: str, data: bytes) -> None` — legacy helper; thin wrapper over `std::fs::write`. Raises `IOError`. |
|
|
99
|
+
| `validate_pdf_magic` | function | `(data: bytes) -> bool` — returns `True` if data starts with `%PDF`. |
|
|
100
|
+
|
|
101
|
+
## Sub-crate Docs
|
|
102
|
+
|
|
103
|
+
- [net-io](../net-io/) — HTTP/web I/O providers + MinerU document parsing
|
|
104
|
+
- [files-io](../files-io/) — File I/O, S3, archives, SHA-256 dedup
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
## Concurrency Model
|
|
108
|
+
|
|
109
|
+
All async Python functions (`fetch_one`, `fetch_multi`, `scrape_web`, `mineru_*`, `batch_copy_async`, etc.) use `pyo3-async-runtimes` to bridge Rust `async fn` → Python coroutine. The underlying tokio runtime is managed automatically:
|
|
110
|
+
|
|
111
|
+
- **Provider I/O** (`net-io`): Each `HttpClient` is created per-call. `fetch_multi` launches all provider requests concurrently via `futures::join_all` — all providers execute in parallel on the same tokio runtime.
|
|
112
|
+
- **File I/O** (`files-io`): Synchronous `File` methods execute directly. Async variants (`*_async`) use `tokio::task::spawn_blocking` to offload blocking I/O without stalling the Python event loop. The S3 backend uses a shared `OnceLock<Runtime>` singleton for sync→async bridging.
|
|
113
|
+
- **GIL behavior**: Rust code inside `#[pyfunction]` runs with the GIL held during argument parsing, then releases it during async I/O. The GIL is re-acquired only to construct the return value via `pythonize`.
|
|
114
|
+
|
|
115
|
+
## Environment Variables
|
|
116
|
+
|
|
117
|
+
| Variable | Used by | Description |
|
|
118
|
+
|----------|---------|-------------|
|
|
119
|
+
| `UNPAYWALL_EMAIL` | `net-io` (Unpaywall provider) | Required for Unpaywall API authentication |
|
|
120
|
+
| `AWS_ACCESS_KEY_ID` | `files-io` (S3 backend) | AWS credential chain (optional if passed explicitly) |
|
|
121
|
+
| `AWS_SECRET_ACCESS_KEY` | `files-io` (S3 backend) | AWS credential chain (optional if passed explicitly) |
|
|
122
|
+
| `AWS_REGION` | `files-io` (S3 backend) | AWS region, default `us-east-1` |
|
|
123
|
+
|
|
124
|
+
S3 credential resolution uses the `aws-sdk-s3` default credential chain (env vars, `~/.aws/credentials`, IMDS). Explicit `access_key`/`secret_key`/`region` kwargs override the chain. MinerU tokens are passed per-call — they are never read from the environment.
|
|
125
|
+
|
|
126
|
+
## Error Reference
|
|
127
|
+
|
|
128
|
+
Both sub-crates map Rust error types to Python exceptions automatically via `From<Error> for PyErr`:
|
|
129
|
+
|
|
130
|
+
### `rust_io.net` (GatewayError)
|
|
131
|
+
|
|
132
|
+
| Rust variant | Python exception | Typical cause |
|
|
133
|
+
|-------------|-----------------|---------------|
|
|
134
|
+
| `Http(reqwest::Error)` | `ConnectionError` | Timeout, DNS failure, non-2xx response |
|
|
135
|
+
| `Json(serde_json::Error)` | `ValueError` | Malformed provider response |
|
|
136
|
+
| `Io(std::io::Error)` | `OSError` | File read failure (MinerU upload) |
|
|
137
|
+
| `Url(url::ParseError)` | `ValueError` | Malformed provider URL |
|
|
138
|
+
| `Provider { provider, message }` | `RuntimeError` | Unknown provider or unsupported action |
|
|
139
|
+
| `Other(String)` | `RuntimeError` | Catch-all for business logic errors |
|
|
140
|
+
|
|
141
|
+
### `rust_io.files` (FileError)
|
|
142
|
+
|
|
143
|
+
| Rust variant | Python exception | Typical cause |
|
|
144
|
+
|-------------|-----------------|---------------|
|
|
145
|
+
| `Io(std::io::Error)` | `IOError` | Permission denied, file not found |
|
|
146
|
+
| `S3(String)` | `ConnectionError` | S3 service unavailable, auth failure |
|
|
147
|
+
| `Path(String)` | `ValueError` | Path traversal attempt, invalid path |
|
|
148
|
+
| `Archive(String)` | `ValueError` | Malformed archive, extraction path error |
|
|
149
|
+
| `Zip(ZipError)` | `ValueError` | Corrupt ZIP file |
|
|
150
|
+
| `Hash(String)` | `RuntimeError` | SHA-256 computation failure |
|
|
151
|
+
| `TaskJoin(JoinError)` | `RuntimeError` | spawn_blocking task panic or cancel |
|
|
152
|
+
| `Other(String)` | `RuntimeError` | Catch-all for business logic errors |
|
|
153
|
+
|
|
154
|
+
## Development Workflow
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
# Build the Python extension (from backend/)
|
|
158
|
+
cd backend
|
|
159
|
+
uv run maturin develop --release -m libs/rust-io/Cargo.toml
|
|
160
|
+
|
|
161
|
+
# Run Rust unit tests per sub-crate
|
|
162
|
+
cargo test -p net-io
|
|
163
|
+
cargo test -p files-io
|
|
164
|
+
|
|
165
|
+
# Run Python integration tests
|
|
166
|
+
uv run pytest backend/tests/ -v
|
|
167
|
+
|
|
168
|
+
# Lint Rust code
|
|
169
|
+
cargo clippy --all-targets -- -D warnings
|
|
170
|
+
|
|
171
|
+
# Type-check Python
|
|
172
|
+
uv run ruff check
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
The `Cargo.lock` files are committed for reproducibility. After adding or updating dependencies, run `cargo check` from the crate directory (not a workspace root) to update individual lock files.
|
|
176
|
+
|
|
177
|
+
## Extension Guide
|
|
178
|
+
|
|
179
|
+
### Adding a new `rust_io.net` pyfunction
|
|
180
|
+
|
|
181
|
+
1. Implement the Rust function in `net-io/src/` (e.g. a new provider, new scraper utility)
|
|
182
|
+
2. Expose it as a `#[pyfunction]` in `net-io/src/py.rs`
|
|
183
|
+
3. Register it in `rust-io/src/lib.rs`:
|
|
184
|
+
```rust
|
|
185
|
+
net.add_function(wrap_pyfunction!(net_io::py::your_new_function, &net)?)?;
|
|
186
|
+
```
|
|
187
|
+
4. Rebuild and test
|
|
188
|
+
|
|
189
|
+
### Adding a new `rust_io.files` pyfunction
|
|
190
|
+
|
|
191
|
+
Same pattern — implement in `files-io/src/py/`, then register in `rust-io/src/lib.rs`:
|
|
192
|
+
```rust
|
|
193
|
+
files.add_function(wrap_pyfunction!(files_io::py::new_module::your_function, &files)?)?;
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Submodule registration internals
|
|
197
|
+
|
|
198
|
+
The `register_submodule` helper is required because PyO3 nests submodules by default. Without explicitly setting `sys.modules["rust_io.net"]`, Python's `import rust_io.net` would fail even though the module is attached to the parent. The helper calls both `parent.add_submodule()` and `sys.modules[full_name] = submodule`.
|
|
199
|
+
## Dependencies
|
|
200
|
+
|
|
201
|
+
| Crate | Version | Purpose |
|
|
202
|
+
|-------|---------|---------|
|
|
203
|
+
| pyo3 | 0.28.2 | Rust-to-Python bindings (`extension-module` feature) |
|
|
204
|
+
| pyo3-async-runtimes | 0.28 | async fn → Python coroutine (tokio) |
|
|
205
|
+
| files-io | path = "../files-io" | Sub-crate providing `rust_io.files` functions |
|
|
206
|
+
| net-io | path = "../net-io" | Sub-crate providing `rust_io.net` functions |
|
|
207
|
+
|
|
208
|
+
All business-logic dependencies are in the sub-crates. The facade only depends on `pyo3`, `pyo3-async-runtimes`, and the two sub-crates.
|