diff --git a/CHANGELOG.md b/CHANGELOG.md index d42f7f6..01b9513 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Add support for implicit and wildcard internal transitions +### Changed + +- [breaking] Renamed custom_guard_error flag to custom_error as it is not guard specific anymore + ## [0.7.0] - 2024-07-03 ### Added diff --git a/docs/dsl.md b/docs/dsl.md index 83aac51..09c6ef0 100644 --- a/docs/dsl.md +++ b/docs/dsl.md @@ -21,7 +21,7 @@ statemachine!{ // [Optional] Can be optionally specified to add a new `type Error` to the // generated `StateMachineContext` trait to allow guards to return a custom // error type instead of `()`. - custom_guard_error: false, + custom_error: false, // [Optional] A list of derive names for the generated `States` and `Events` // enumerations respectively. For example, to `#[derive(Debug)]`, these diff --git a/examples/guard_custom_error.rs b/examples/guard_custom_error.rs index b8e85a4..aac7a2a 100644 --- a/examples/guard_custom_error.rs +++ b/examples/guard_custom_error.rs @@ -27,7 +27,7 @@ statemachine! { State2(MyStateData) + Event2 [guard2] / action2 = State3, // ... }, - custom_guard_error: true, + custom_error: true, } /// Context diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index 457c136..3e663cf 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -203,7 +203,7 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream { }) .collect(); - let guard_error = if sm.custom_guard_error { + let custom_error = if sm.custom_error { quote! { Self::Error } } else { quote! { () } @@ -329,7 +329,7 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream { guard_list.extend(quote! { #[allow(missing_docs)] #[allow(clippy::result_unit_err)] - #is_async fn #guard <#all_lifetimes> (&self, #temporary_context #state_data #event_data) -> Result; + #is_async fn #guard <#all_lifetimes> (&self, #temporary_context #state_data #event_data) -> Result; }); }; Ok(()) @@ -352,10 +352,10 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream { .data_types .get(&transition.out_state.to_string()) { - quote! { Result<#output_data,#guard_error> } + quote! { Result<#output_data,#custom_error> } } else { // Empty return type - quote! { Result<(),#guard_error> } + quote! { Result<(),#custom_error> } }; let event_data = match sm.event_data.data_types.get(event) { @@ -524,7 +524,7 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream { // lifetimes that exists in #events_type_name but not in #states_type_name let event_unique_lifetimes = event_lifetimes - state_lifetimes; - let guard_error = if sm.custom_guard_error { + let custom_error = if sm.custom_error { quote! { /// The error type returned by guard or action functions. type Error: core::fmt::Debug; @@ -539,7 +539,7 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream { quote! {} }; - let error_type = if sm.custom_guard_error { + let error_type = if sm.custom_error { quote! { #error_type_name<::Error> } @@ -554,7 +554,7 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream { /// This trait outlines the guards and actions that need to be implemented for the state /// machine. pub trait #state_machine_context_type_name { - #guard_error + #custom_error #guard_list #action_list #entries_exits diff --git a/macros/src/parser/mod.rs b/macros/src/parser/mod.rs index e9b0f6c..c9c9b27 100644 --- a/macros/src/parser/mod.rs +++ b/macros/src/parser/mod.rs @@ -49,7 +49,7 @@ pub struct ParsedStateMachine { pub derive_states: Vec, pub derive_events: Vec, pub temporary_context_type: Option, - pub custom_guard_error: bool, + pub custom_error: bool, pub states: HashMap, pub starting_state: Ident, pub state_data: DataDefinitions, @@ -249,7 +249,7 @@ impl ParsedStateMachine { derive_states: sm.derive_states, derive_events: sm.derive_events, temporary_context_type: sm.temporary_context_type, - custom_guard_error: sm.custom_guard_error, + custom_error: sm.custom_error, states, starting_state, state_data, diff --git a/macros/src/parser/state_machine.rs b/macros/src/parser/state_machine.rs index 85f4402..2b011d0 100644 --- a/macros/src/parser/state_machine.rs +++ b/macros/src/parser/state_machine.rs @@ -4,7 +4,7 @@ use syn::{braced, bracketed, parse, spanned::Spanned, token, Ident, Token, Type} #[derive(Debug)] pub struct StateMachine { pub temporary_context_type: Option, - pub custom_guard_error: bool, + pub custom_error: bool, pub transitions: Vec, pub name: Option, pub derive_states: Vec, @@ -15,7 +15,7 @@ impl StateMachine { pub fn new() -> Self { StateMachine { temporary_context_type: None, - custom_guard_error: false, + custom_error: false, transitions: Vec::new(), name: None, derive_states: Vec::new(), @@ -72,11 +72,11 @@ impl parse::Parse for StateMachine { } } } - "custom_guard_error" => { + "custom_error" => { input.parse::()?; - let custom_guard_error: syn::LitBool = input.parse()?; - if custom_guard_error.value { - statemachine.custom_guard_error = true + let custom_error: syn::LitBool = input.parse()?; + if custom_error.value { + statemachine.custom_error = true } } "temporary_context" => { @@ -145,7 +145,7 @@ impl parse::Parse for StateMachine { "Unknown keyword {}. Support keywords: [\"name\", \ \"transitions\", \ \"temporary_context\", \ - \"custom_guard_error\", \ + \"custom_error\", \ \"derive_states\", \ \"derive_events\" ]",