Skip to content

Commit

Permalink
renamed custom_guard_error flag to custom_error as it is not guard sp…
Browse files Browse the repository at this point in the history
…ecific anymore
  • Loading branch information
Dmitriy Kumshayev committed Jul 13, 2024
1 parent 0939b6c commit 2c64922
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/dsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/guard_custom_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ statemachine! {
State2(MyStateData) + Event2 [guard2] / action2 = State3,
// ...
},
custom_guard_error: true,
custom_error: true,
}

/// Context
Expand Down
14 changes: 7 additions & 7 deletions macros/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! { () }
Expand Down Expand Up @@ -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<bool,#guard_error>;
#is_async fn #guard <#all_lifetimes> (&self, #temporary_context #state_data #event_data) -> Result<bool,#custom_error>;
});
};
Ok(())
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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<<T as #state_machine_context_type_name>::Error>
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions macros/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct ParsedStateMachine {
pub derive_states: Vec<Ident>,
pub derive_events: Vec<Ident>,
pub temporary_context_type: Option<Type>,
pub custom_guard_error: bool,
pub custom_error: bool,
pub states: HashMap<String, Ident>,
pub starting_state: Ident,
pub state_data: DataDefinitions,
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions macros/src/parser/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type>,
pub custom_guard_error: bool,
pub custom_error: bool,
pub transitions: Vec<StateTransition>,
pub name: Option<Ident>,
pub derive_states: Vec<Ident>,
Expand All @@ -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(),
Expand Down Expand Up @@ -72,11 +72,11 @@ impl parse::Parse for StateMachine {
}
}
}
"custom_guard_error" => {
"custom_error" => {
input.parse::<Token![:]>()?;
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" => {
Expand Down Expand Up @@ -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\"
]",
Expand Down

0 comments on commit 2c64922

Please sign in to comment.