Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust support #250

Merged
merged 173 commits into from
Sep 7, 2024
Merged

Rust support #250

merged 173 commits into from
Sep 7, 2024

Conversation

Agile86
Copy link
Contributor

@Agile86 Agile86 commented Mar 15, 2023

Hello everybody,

my name is Oleh Dolhov, my collegue name is Vitaly Reshetyuk,

we are from Stroz Friedberg (AON) providing this PR to bring Rust-language support to Kaitai.

Right now we can pass almost all tests, except (4) tests, that's using features like:

  1. parent rewrite

Rust doesn't support inheritance, so there is no common type.

  1. KStructUnit, tests like nav_parent_switch_cast.ksy, params_pass_array_struct.ksy, params_pass_struct.ksy.

  2. AnyType, test like process_coerce_switch.ksy.

Please, note, you need to clone tests and runtime/rust exactly rust_basic_support_v2 branch.

Would be great to get feedback.

Best regards,
Oleh & Vitaly.

Agile86 and others added 30 commits July 6, 2022 09:06
…i_struct_compiler into rust_tuning

� Conflicts:
�	shared/src/main/scala/io/kaitai/struct/languages/RustCompiler.scala
@Agile86 Agile86 mentioned this pull request Mar 16, 2023
@cmhulbert
Copy link

Just wondering is this is still an active effort? I'd love to have Rust support for for Kaitai, as I'm trying to migrate some of my projects to Rust using existing kaitai structs

@Agile86
Copy link
Contributor Author

Agile86 commented Nov 28, 2023

Thanks for your interest. Yes, we are waiting for approval from maintainers.

sample:
self.groups().value() # look for "value" method inside of class (type) "groups"
self.value()          # look for "value" in current class
@Agile86
Copy link
Contributor Author

Agile86 commented Feb 19, 2024

Updated PR with the latest changes from the master branch + small improvements.

generalmimon added a commit to Mingun/kaitai_struct_compiler that referenced this pull request Jul 10, 2024
Rust support is largely reworked in
kaitai-io#250 and
touching it here would just create conflicts.
generalmimon added a commit to kaitai-io/kaitai_struct_rust_runtime that referenced this pull request Sep 2, 2024
It is not used from the rest of the `src/lib.rs` file and it is not
referenced from
kaitai-io/kaitai_struct_compiler#250 either.
Besides, it is apparently a reproduction of `UnexpectedDataError`, which
was used in KS only before 0.9. Since 0.9, `ValidationNotEqualError` is
used instead (see
kaitai-io/kaitai_struct#280 (comment)).
In runtime libraries which existed before 0.9, `UnexpectedDataError` was
marked as deprecated and will be eventually removed, so it doesn't make
sense to add it to new runtime libraries.
generalmimon added a commit to kaitai-io/kaitai_struct_rust_runtime that referenced this pull request Sep 2, 2024
generalmimon added a commit to kaitai-io/kaitai_struct_rust_runtime that referenced this pull request Sep 2, 2024
At the time of writing,
kaitai-io/kaitai_struct_compiler#250 used
`KError::ValidationNotEqual` for all kinds of validations (see
https://github.com/kaitai-io/kaitai_struct_compiler/blob/23328785803ba93a8b3bfc33e1e0b998a34df01f/shared/src/main/scala/io/kaitai/struct/languages/RustCompiler.scala#L1199),
which doesn't make any sense.

This commit makes a step towards a solution consistent with the
established system of validation errors in KS. The enum variant
`KError::ValidationFailed` represents the failure of any validation (it
is an equivalent of the `ValidationFailedError` base class in other
languages). It contains a `ValidationFailedError` struct, which is
intended to hold any details associated with the specific failed
validation. It only contains the `kind` field at the moment, but the
plan is to extend it in the future with fields like `actual`, `src_path`
etc.

The `ValidationKind` enum has been marked with the [`non_exhaustive`
attribute](https://doc.rust-lang.org/reference/attributes/type_system.html#the-non_exhaustive-attribute),
which allows us to add more validation kinds in the future without
breaking compatibility with user application code.

Even though I anticipate the addition of fields to the
`ValidationFailedError` struct, it cannot be marked `non_exhaustive`
because we must be able to instantiate it from the KSC-generated code.
So I added at least a doc comment warning the users about the
possibility of extending the struct contents.
Comment on lines 53 to 54
outHeader.puts("#![allow(arithmetic_overflow)]")
outHeader.puts("#![allow(overflowing_literals)]")
Copy link
Member

@generalmimon generalmimon Sep 2, 2024

Choose a reason for hiding this comment

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

These two suppressions hide real bugs, so I'll remove them.

Copy link
Member

@generalmimon generalmimon left a comment

Choose a reason for hiding this comment

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

@Agile86 @revitalyr Thank you for your work, I'll now merge this.

I'm personally not very happy with the quality of the code (for example, too many mutable variables, too much string manipulation instead of modeling the necessary concepts in types in types and using pattern matching like in the majority of KSC code), but it's better than what we have now.

Hopefully we can improve it over time. Going through the code now, it actually seems that it isn't bad overall, there are just a number of weird places that need rewriting. So when I have time in the future, I'll try to refactor these weird places one by one and then it might be actually good.

@generalmimon generalmimon merged commit 049b464 into kaitai-io:master Sep 7, 2024
1 of 4 checks passed
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.

4 participants