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

Merge main into workflow dev branch #14

Open
wants to merge 11 commits into
base: release-workflow
Choose a base branch
from
Open

Conversation

StrongFennecs
Copy link

No description provided.

StrongChris and others added 11 commits May 3, 2023 03:11
* Use `fs::read` instead of manual implementation

* Remove unneeded `File::try_clone`

Immutable references to `File` can also be written to.

* Remove unneeded `Cursor`

References to `&[u8]` implement the needed `Read` and `Write` traits.

* Use iterator methods to prepare dimble fields

* Remove unneeded variables and types

* Avoid creating Strings for two ASCII characters

* Decode directly to desired type instead of going through `Value`

* Reduce code duplication for converting integers

* Coalesce panic and print statements

* Use Iterator methods to build a new vector

* Remove unneeded variable

* Avoid checking the type twice

* Use `unwrap_or_else` instead of manual matching

* Extract duplicated calls to `set_item`

* Simplify header length and data parsing

* Use the header length constant in more places

Since we use it as a `usize` some places, and a `u64` others, I
changed the base type to a `u8`, which is large enough to represent
the 8 bytes needed. This allows us to losslessly convert to a those
two larger types.

* Merge import statements

* Demonstrate one way to create Rust errors and integrate with Python

While it's possible to create your error types by hand, I like to use
my library [SNAFU][snafu] to help with some of the boilerplate. There
are other popular libraries such as `thiserror` or `anyhow` as well.

I demonstrate a few facets of the crate here:

1. Creating opaque errors that do not expose internal details. Here,
   `ir_to_dimble::Error` wraps `ir_to_dimble::InnerError` and hides
   details except for the causal chain and error text. This is good
   for public APIs where you should be mindful of semver concerns.

2. Localized errors that compose into larger
   ones. `ir_to_dimble::SerialiseFieldsError` only deals with the ways
   that `serialise_dimble_fields` can fail and helps you avoid
   repetition and keep the scope a little tighter.

I also followed PyO3's example and implemented a conversion from the
Rust error to a `PyErr`, going through the existing
`DimbleError`. This is a very basic implementation; a better one would
expose the causal chain as a Python exception chain and potentially
even have a traceback including Rust code.

[snafu]: https://docs.rs/snafu/

* Remove unneeded ownership

* Remove unneeded explicit types

* DRY up repeated unwraps

* DRY up repeated code
* changed format of README

* fixed formatting readme

---------

Co-authored-by: wallarug <[email protected]>
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