Add Command Error Handling, Flags, and Shutdown State Filtering #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds error handling, command flags, and shutdown state filtering to Anchor.
Breaking change:
Transport::receive()now returnsResultthat must be handled. Shutdown filtering is feature-gated and non-breaking.Motivation
Previously,
#[klipper_command]functions couldn't return errors. Users had to either usepanic!(problematic across different embedded platforms) or shared state withMutex/Atomic(adds synchronization overhead to every main loop iteration). Returning errors from commands is more portable and matches Klipper'ssetjmpbehavior more closely.There was also no efficient way to filter commands during shutdown to match Klipper's firmware behavior where only certain commands are allowed.
Changes
Error Handling
Commands can return
Result<(), CustomError>. Errors are wrapped in a generatedKlipperCommandErrorenum and propagate throughTransport::receive():Command Flags
Commands can be marked with flags to control behavior. Currently supports
HF_IN_SHUTDOWN:Shutdown State Filtering (Feature-Gated)
When
shutdown-filteringis enabled, commands without theHF_IN_SHUTDOWNflag are filtered during shutdown. Requires implementingCheckShutdownon your context type. Uses local state for zero-cost checks in the happy path.Implementation
anchor_typescrate for sharedKlipperCommandFlagsbitflagsCommandstruct now stores flags parsed from#[klipper_command(flags = ...)]KlipperCommandErrorenum wraps all command error typesCheckShutdowntrait for efficient shutdown state checking (feature-gated)Breaking Changes
Transport::receive()now returnsResultthat must be handled. Shutdown filtering is feature-gated and optional.Migration
Update your main loop to handle
Transport::receive()errors:To use shutdown filtering, enable the feature and implement
CheckShutdownon your context type. SeeREADME.mdfor details.Testing
Tested with
testjig:get_configcommandclear_shutdown,config_reset,get_config, etc.Documentation
Updated
README.mdand inline docs with examples and usage instructions.