|
| 1 | +### Feature Implementation Writeup: HurlParser Wrapper for API Dash |
| 2 | + |
| 3 | +I initially started with the approach of writing my own parser using `petitparser` and creating a custom implementation. However, after about a week, the maintainer recommended against this approach. They highlighted that building a custom parser would lack future-proofing if `hurl.dev` were to implement changes. While I abandoned this route, the code is still available in a branch. My custom parser had progressed to the point where requests and assertions were working, though it was far from complete. |
| 4 | + |
| 5 | +#### Transition to Hurl Parser Wrapper Using Flutter Rust Bridge |
| 6 | + |
| 7 | +Following the maintainer's suggestion, I began developing a wrapper for the Hurl parser using `flutter_rust_bridge`. Since the documentation for the library can be sparse, I’ve documented my steps for clarity: |
| 8 | + |
| 9 | +1. **Creating the Library:** |
| 10 | + |
| 11 | + - I ran the command: |
| 12 | + |
| 13 | + `flutter_rust_bridge_codegen create hurl --template plugin` |
| 14 | + |
| 15 | + - This initialized the project. |
| 16 | +2. **Adding the Parse Function:** |
| 17 | + |
| 18 | + - I added the library code and the `parse_hurl` function in `rust/src/api/simple.rs`. |
| 19 | +3. **Generating the Flutter Side Code:** |
| 20 | + |
| 21 | + - I ran: |
| 22 | + |
| 23 | + |
| 24 | + `flutter_rust_bridge_codegen generate` |
| 25 | + |
| 26 | + - This generated all the necessary code for Flutter and all the targeted platforms. |
| 27 | + |
| 28 | +`flutter_rust_bridge` uses a tool called `cargokit` to manage dependencies, acting as a glue layer between Flutter and Rust. Unfortunately, `cargokit` is still experimental, with little documentation available. The [blog post by Matej Knopp](https://matejknopp.com/post/flutter_plugin_in_rust_with_no_prebuilt_binaries/) provided valuable insights. One crucial takeaway was avoiding the Homebrew installation of Rust and instead using `rustup`. This resolved platform compilation issues for me, though the project is still not compiling entirely. |
| 29 | + |
| 30 | +#### Implementing the Wrapper |
| 31 | + |
| 32 | +I postponed multi-platform compilation issues to focus on writing the wrapper code. By reviewing the `hurl.dev` documentation and examples, I identified the required structure: |
| 33 | + |
| 34 | +- `HurlFile` → List of `Entries` |
| 35 | +- `Entries` → `Request` and `Response` |
| 36 | + |
| 37 | +Based on this, I created models in Dart using the `freezed` package. The goal was to convert the JSON output from the Hurl parser into Dart data models. I am confident this part turned out well. |
| 38 | + |
| 39 | +After writing tests for Hurl parsing and model generation, I shifted focus to building the project. |
| 40 | + |
| 41 | +#### Compilation Challenges |
| 42 | + |
| 43 | +During compilation, I encountered an issue, which I reported on the [Hurl repository](https://github.com/Orange-OpenSource/hurl/issues/3603). The Hurl parser depends on the `libxml2` crate for XML parsing. To resolve this, I had to add: |
| 44 | + |
| 45 | +`OTHER_LDFLAGS => -force_load ${BUILT_PRODUCTS_DIR}/libhurl.a -lxml2` |
| 46 | + |
| 47 | +This fixed the macOS build. |
| 48 | + |
| 49 | +However, testing on iOS and Android proved problematic, as I didn’t have access to Windows or Linux systems. For iOS, adding similar flags didn’t work. I tried various fixes, such as modifying the header search paths, linking frameworks, and changing build phases in Xcode, but none succeeded. |
| 50 | + |
| 51 | +#### Investigation into `libxml2` |
| 52 | + |
| 53 | +I learned that `libxml2` is not a system library but a crate that wraps platform-specific libraries. Unfortunately, `libxml2` does not support Android or iOS, causing persistent errors. After consulting the maintainers of Hurl and `flutter_rust_bridge`, I received the following suggestion from the Hurl maintainer: |
| 54 | + |
| 55 | +> _"I'm not an expert in iOS/Android, but what I would do is try to build something smaller, maybe just calling libxml through a Flutter component, then trying to build with libxml but with the Rust crate wrapper, etc."_ ([GitHub comment](https://github.com/Orange-OpenSource/hurl/issues/3603#issuecomment-2611159759)) |
| 56 | +
|
| 57 | +#### Current Status and Potential Solutions |
| 58 | + |
| 59 | +At this point, I’ve committed all my code to the repository in a fork. Here are some potential solutions I believe might work: |
| 60 | + |
| 61 | +1. **Use Dart Native FFIs**: Currently under an experimental flag, as suggested by the `flutter_rust_bridge` maintainer. |
| 62 | +2. **Revert to an Older Version of Hurlfmt**: Use a version of the library from before it transitioned to `libxml` for performance improvements. |
| 63 | +3. **Revisit My Custom Parser**: Complete the parser I started building with `petitparser`. |
| 64 | + |
| 65 | +If anyone knows a solution to these challenges or has suggestions, I’d appreciate the help! |
0 commit comments