Finite state machines in rust; bendns fork to add types.
Diffstat (limited to 'rust-fsm-dsl/src/lib.rs')
-rw-r--r--rust-fsm-dsl/src/lib.rs25
1 files changed, 21 insertions, 4 deletions
diff --git a/rust-fsm-dsl/src/lib.rs b/rust-fsm-dsl/src/lib.rs
index 094e3b2..47e55f1 100644
--- a/rust-fsm-dsl/src/lib.rs
+++ b/rust-fsm-dsl/src/lib.rs
@@ -164,7 +164,7 @@ pub fn state_machine(tokens: TokenStream) -> TokenStream {
.parse()
.unwrap();
let input_generics = input_name.g();
- let input_impl = variant::tokenize(&inputs, |x| {
+ let input_impl = variant::tokenize(&inputs, |x, matcher| {
let attrs = attrs_to_token_stream(input_attrs);
input_name.tokenize(|f| {
quote! {
@@ -172,11 +172,17 @@ pub fn state_machine(tokens: TokenStream) -> TokenStream {
#input_visibility enum #f #input_generics {
#(#x),*
}
+
+ impl #input_generics #f #input_generics {
+ #input_visibility fn name(&self) -> &'static str {
+ match self { #(Self::#matcher => stringify!(#x)),* }
+ }
+ }
}
})
});
let input_name = input_name.path();
- let state_impl = variant::tokenize(&states, |x| {
+ let state_impl = variant::tokenize(&states, |x, matcher| {
let attrs = attrs_to_token_stream(state_attrs.clone());
state_name.tokenize(|f| {
quote! {
@@ -184,12 +190,18 @@ pub fn state_machine(tokens: TokenStream) -> TokenStream {
#state_visibility enum #f {
#(#x),*
}
+
+ impl #f {
+ #state_visibility fn name(&self) -> &'static str {
+ match self { #(Self::#matcher => stringify!(#x)),* }
+ }
+ }
}
})
});
let state_name = state_name.path();
let output_generics = output_name.g();
- let output_impl = variant::tokenize(&outputs, |outputs| {
+ let output_impl = variant::tokenize(&outputs, |outputs, matcher| {
let attrs = attrs_to_token_stream(output_attrs);
output_name.tokenize(|output_name| {
quote! {
@@ -197,6 +209,12 @@ pub fn state_machine(tokens: TokenStream) -> TokenStream {
#output_visibility enum #output_name #output_generics {
#(#outputs),*
}
+
+ impl #output_generics #output_name #output_generics {
+ #output_visibility fn name(&self) -> &'static str {
+ match self { #(Self::#matcher => stringify!(#outputs)),* }
+ }
+ }
}
})
});
@@ -231,7 +249,6 @@ pub fn state_machine(tokens: TokenStream) -> TokenStream {
(state, input) => ::core::result::Result::Err(::rust_fsm::TransitionImpossibleError { state, input, }),
}
}
-
}
};