Files
turtle/turtle-lib
dietrich 95b467a76f Remove Process-Wide Panic Hook in Headless SVG Export
Greptile flagged that
[`run_headless_svg_export`](file:///home/dietrich/Projekte/Source/turtlers/turtle-lib/src/export.rs#L80)
was setting and taking the global, process-wide panic hook without
thread coordination. In concurrent environments (such as multi-threaded
test runners or multi-threaded host applications), this caused race
conditions that could clobber custom hooks, restore stale hooks, or
capture another thread's panic hook.

[export.rs](file:///home/dietrich/Projekte/Source/turtlers/turtle-lib/src/export.rs)

1. **Removed Global Panic Hook Manipulation**:
   - Removed `PanicHookFn` and `PanicHookGuard`.
   - Eliminated calls to `std::panic::take_hook()` and
     `std::panic::set_hook()`.
   - Removed `AtomicBool` and `Arc` overhead.
2. **Inspect Panic Payload Directly in
   [`run_headless_svg_export`](file:///home/dietrich/Projekte/Source/turtlers/turtle-lib/src/export.rs#L80)**:
   - Evaluates user command closures inside `std::panic::catch_unwind`.
   - Downcasts caught panic payload to `&str` or `String` and checks for
     Macroquad assertion signatures (`THREAD_ID.is_some()` or
     `macroquad`).
   - Packages diagnostic guidance directly into
     `ExportError::Execution(...)` without emitting unsolicited messages
     to `eprintln!`.
3. **Banner in CLI Entrypoint
   [`handle_svg_export`](file:///home/dietrich/Projekte/Source/turtlers/turtle-lib/src/export.rs#L137)**:
   - When executed via `--export-svg`, if `run_headless_svg_export`
     fails due to Macroquad window/GUI functions, formats and displays
     the friendly "Headless SVG Export Note" banner to `eprintln!`.
4. **Unit Tests**:
   - Added `test_headless_svg_export_success`: checks complete headless
     SVG export output.
   - Added `test_headless_svg_export_macroquad_panic`: verifies proper
     detection and error message for simulated Macroquad panics.
   - Added `test_headless_svg_export_custom_panic`: verifies that
     non-Macroquad panics retain their original error message.

---

- **Default tests**:
  ```bash
  cargo test --package turtle-lib
  ```
  Result: 26 unit tests passed, 34 doc-tests passed (0 failures).

- **SVG feature tests**:
  ```bash
  cargo test --package turtle-lib --features svg
  ```
  Result: 32 unit tests passed, 34 doc-tests passed (0 failures).

- **Clippy**:
  ```bash
  cargo clippy --package turtle-lib --features svg -- -Wclippy::pedantic
  \
  -Aclippy::cast_precision_loss -Aclippy::cast_sign_loss
  -Aclippy::cast_possible_truncation
  ```
  Result: 0 warnings, clean.

- **SVG export example**:
  ```bash
  cargo run --package turtle-lib --example test_svg_export --features
  svg -- --export-svg /tmp/test_output.svg
  ```
  Result: Successfully generated valid SVG file `/tmp/test_output.svg`.
2026-09-20 13:53:15 +02:00
..
2025-10-24 17:29:40 +02:00

turtle-lib

The main turtle graphics library built on Macroquad with Lyon tessellation.

See the main README for complete documentation.