Skip to content

Return errors from I2C transaction operations - #143

Open
Old-Ding wants to merge 2 commits into
rust-embedded:mainfrom
Old-Ding:codex/i2c-transaction-errors
Open

Return errors from I2C transaction operations#143
Old-Ding wants to merge 2 commits into
rust-embedded:mainfrom
Old-Ding:codex/i2c-transaction-errors

Conversation

@Old-Ding

@Old-Ding Old-Ding commented Jul 5, 2026

Copy link
Copy Markdown

Fixes #127.

Summary:

  • Stop unwrapping nested I2C operation results inside eh1::i2c::Mock::transaction.
  • Return the first mocked operation error to the caller.
  • Still consume the transaction_end expectation so the mock transaction boundary remains balanced when an operation returns an error.
  • Add regression coverage for both the returned operation error and the unhappy path where extra expectations appear before transaction_end.

Testing:

  • cargo test transaction_returns_operation_error --features eh1
  • cargo test transaction_error_rejects_trailing_expectations --features eh1
  • cargo fmt --check
  • cargo test --all-features
  • git diff --check HEAD~1..HEAD
  • git ls-files --eol src/eh1/i2c.rs

Generated-by: OpenAI Codex
Signed-off-by: aineoae86-sys <ai.neo.ae86@gmail.com>
@dbrgn

dbrgn commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

@Old-Ding If you're copy-pasting directly from AI output, please ensure at the very least that the description is readable and well-formatted...

@dbrgn dbrgn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Regarding the change itself: I was a bit on the fence, whether a TransactionEnd expectation should be part of the expectations (since the transaction is aborted, not completed). But on the wire a STOP happens as well, so TransactionEnd is probably correct.

Having more expectations between error and TransactionEnd would be wrong, and that will get caught by this crate. This is not yet tested, so an additional test would be nice (see inline comment).

Comment thread src/eh1/i2c.rs
}

#[test]
fn transaction_returns_operation_error() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This tests the happy path. Could you add a test for the unhappy path as well, where the user included additional assertions before transaction_end? This should cause the test to fail.

Probably something like this:

#[test]
#[should_panic(expected = "i2c::transaction_end unexpected mode")]
fn transaction_error_rejects_trailing_expectations() {
    let expectations = [
        Transaction::transaction_start(0x76),
        Transaction::write(0x76, vec![0x88]).with_error(ErrorKind::Other),
        Transaction::write(0x76, vec![0xAA]),
        Transaction::transaction_end(0x76),
    ];
    let mut i2c = Mock::new(&expectations);
    let bytes = [0x88];
    let more = [0xAA];
    let mut ops = [i2c::Operation::Write(&bytes), i2c::Operation::Write(&more)];
    let _ = i2c.transaction(0x76, &mut ops);
}

...but I did not test this in practice, so please validate that it works as intended.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed in dbc0ede: transaction_error_rejects_trailing_expectations adds the error-path case with an extra write expectation before transaction_end, and asserts that it panics with i2c::transaction_end unexpected mode.

I validated the current head with cargo test --all-features: 140 unit tests and 21 doc tests passed, including this regression test.

Signed-off-by: Old-Ding <ai.neo.ae86@gmail.com>
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.

Unwrap in I2C transaction

3 participants