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

feat: Add support for custom exception codes #279

Merged
merged 14 commits into from
Sep 11, 2024

Conversation

CosminPerRam
Copy link
Contributor

@CosminPerRam CosminPerRam commented Sep 11, 2024

Would close #278

This PR is just for a better insight on my problem and suggested changes (although it compiles, tests run successfully and pre-commit is happy).

As in the context of the linked issue, this introduces breaking changes.

@uklotzde
Copy link
Member

uklotzde commented Sep 11, 2024

Thanks for contributing!

Some general remarks for discussion.

I have noticed that the naming of the types FunctionCode and Exception is inconsistent. Shouldn't Exception better be renamed to ExceptionCode to reflect this duality?

The different naming of the fallback variants Custom and Other respectively is inconsistent and confusing. That's what I initially stumbled upon. Not sure which term is more common in this context.

@CosminPerRam
Copy link
Contributor Author

The different naming of the fallback variants Custom and Other respectively is inconsistent and confusing.

Shouldn't Exception better be renamed to ExceptionCode to reflect this duality?

Yes, having consistency would be great to see, should this be done in this PR or open another one?

Not sure which term is more common in this context.

I'm not sure either, but personally I'd go with Custom, sounds nicer in this context, as in 'one might have custom implementations' or 'this device returns a custom code'.

@uklotzde
Copy link
Member

Other in the sense of /// None of the above. would make sense to me.

The documentation of both types should also mention, that encoding one of the predefined variants as Other is possible but not recommended. Unfortunately, this ambiguity cannot be resolved easily.

@uklotzde
Copy link
Member

We should address the naming of Custom/Other in this PR and defer the renaming of the type(s) to a follow-up PR.

Let's use Custom for now to avoid changing existing code.

src/frame/mod.rs Outdated Show resolved Hide resolved
@uklotzde uklotzde changed the title feat: add exception other feat: Add support for custom exception codes Sep 11, 2024
src/codec/mod.rs Outdated Show resolved Hide resolved
src/frame/mod.rs Outdated Show resolved Hide resolved
src/codec/mod.rs Outdated Show resolved Hide resolved
GatewayTargetDevice,
/// None of the above.
///
/// Although encoding one of the predefined values as this is possible, it is not recommended.
Copy link
Member

Choose a reason for hiding this comment

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

We could then add something like the following hint:

Instead of instantiating this variant directly prefer to use [`Self::new()`] to prevent such ambiguities.

CHANGELOG.md Outdated
@@ -3,6 +3,15 @@

# Changelog

## v0.15.0 (Unreleased)

- Added `Exception::Other`.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- Added `Exception::Other`.
- Added `Exception::Custom`.

src/codec/mod.rs Outdated
@@ -385,34 +385,17 @@ impl TryFrom<Bytes> for ExceptionResponse {
));
}
let function = fn_err_code - 0x80;
let exception = Exception::try_from(rdr.read_u8()?)?;
let exception = Exception::from(rdr.read_u8()?);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let exception = Exception::from(rdr.read_u8()?);
let exception = Exception::new(rdr.read_u8()?);

src/codec/mod.rs Outdated
}
};
Ok(ex)
impl From<u8> for Exception {
Copy link
Member

Choose a reason for hiding this comment

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

I suggest to delete this impl From.

As a user of this crate you probably do not want to convert arbitrary u8 values implicitly into a Modbus exception code with .into(). With Exception::new() we now have an explicit constructor to make such a conversion explicit.

I generally try to avoid From/TryFrom implementations between types of different abstraction levels. These should be explicitly converted with properly named methods.

Please then also update the changelog accordingly.

src/frame/mod.rs Outdated
GatewayTargetDevice,
/// None of the above.
///
/// Although encoding one of the predefined values as this is possible, it is not recommended,
Copy link
Member

Choose a reason for hiding this comment

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

Please split into two sentences for readability.

Copy link
Member

@uklotzde uklotzde left a comment

Choose a reason for hiding this comment

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

Just one remaining remark.

@uklotzde uklotzde added this to the v0.15.0 milestone Sep 11, 2024
@uklotzde uklotzde merged commit a5eb819 into slowtec:main Sep 11, 2024
10 checks passed
@uklotzde
Copy link
Member

Reporting a bug and with a follow-up PR that fixes it is nice. Thank you!

@mihairadulescu
Copy link

Nice!

@CosminPerRam CosminPerRam deleted the feat/exception_other branch September 11, 2024 21:20
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.

Cannot obtain custom Exception code
3 participants