---
source: framec/tests/rust_snapshots.rs
expression: "compile_fixture(\"10_actions\", \"rust\")"
---
#[allow(dead_code)]
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
#[allow(unused_variables)]
#[allow(unused_mut)]
#[allow(unused_imports)]
#[allow(clippy::assign_op_pattern)]
#[allow(clippy::clone_on_copy)]
#[allow(clippy::derivable_impls)]
#[allow(clippy::match_single_binding)]
#[allow(clippy::needless_return)]
#[allow(clippy::new_without_default)]
#[allow(clippy::single_match)]
mod _actions_framec {
    use super::*;
    extern crate alloc;
    use alloc::{vec, format};
    #[derive(Clone, Debug)]
    #[allow(dead_code, non_camel_case_types)]
    enum ActionsFrameEvent {
        Increment { n: i32 },
        GetTotal {  },
        FrameEnter { args: Vec<String> },
        FrameExit { args: Vec<String> },
    }

    #[derive(Clone)]
    #[allow(dead_code, non_camel_case_types)]
    enum ActionsFrameReturn {
        GetTotal(i32),
        _Lifecycle(alloc::rc::Rc<dyn core::any::Any>),
    }

    #[allow(dead_code)]
    impl ActionsFrameEvent {
        fn name(&self) -> &'static str {
            match self {
                ActionsFrameEvent::Increment { .. } => "increment",
                ActionsFrameEvent::GetTotal { .. } => "get_total",
                ActionsFrameEvent::FrameEnter { .. } => "$>",
                ActionsFrameEvent::FrameExit { .. } => "<$",
            }
        }
    }

    #[derive(Clone, Debug)]
    #[allow(dead_code, non_camel_case_types)]
    enum ActionsFrameValue {
        Int(i64),
        Float(f64),
        Bool(bool),
        Str(String),
        List(Vec<Self>),
        Dict(alloc::collections::BTreeMap<String, Self>),
    }

    #[allow(dead_code, non_camel_case_types)]
    struct ActionsFrameContext {
        event: alloc::rc::Rc<ActionsFrameEvent>,
        _return: Option<ActionsFrameReturn>,
        _data: alloc::collections::BTreeMap<String, ActionsFrameValue>,
        _transitioned: bool,
    }

    impl ActionsFrameContext {
        fn new(event: alloc::rc::Rc<ActionsFrameEvent>, default_return: Option<ActionsFrameReturn>) -> Self {
            Self {
                event,
                _return: default_return,
                _data: alloc::collections::BTreeMap::new(),
                _transitioned: false,
            }
        }
    }

    #[allow(dead_code, non_camel_case_types)]
    #[derive(Clone)]
    enum ActionsStateContext {
        Counting,
        Empty,
    }

    impl Default for ActionsStateContext {
        fn default() -> Self {
            ActionsStateContext::Counting
        }
    }

    #[allow(dead_code, non_camel_case_types)]
    #[derive(Clone)]
    struct ActionsCompartment {
        state: String,
        state_context: ActionsStateContext,
        enter_args: Vec<String>,
        exit_args: Vec<String>,
        forward_event: Option<ActionsFrameEvent>,
        parent_compartment: Option<Box<ActionsCompartment>>,
    }

    impl ActionsCompartment {
        fn new(state: &str) -> Self {
            let state_context = match state {
                "Counting" => ActionsStateContext::Counting,
                _ => ActionsStateContext::Empty,
            };
            Self {
                state: state.to_string(),
                state_context,
                enter_args: Vec::new(),
                exit_args: Vec::new(),
                forward_event: None,
                parent_compartment: None,
            }
        }
    }

    #[allow(dead_code)]
    pub struct Actions {
        _state_stack: Vec<ActionsCompartment>,
        __compartment: ActionsCompartment,
        __next_compartment: Option<ActionsCompartment>,
        _context_stack: Vec<ActionsFrameContext>,
        pub total: i32,
    }

    #[allow(non_snake_case)]
    impl Actions {
        pub fn new() -> Self {
            Self {
                _state_stack: Vec::new(),
                _context_stack: Vec::new(),
                total: 0,
                __compartment: ActionsCompartment::new("Counting"),
                __next_compartment: None,
            }
        }

        pub fn __create() -> Self {
            let mut c = Self::new();
            c.__compartment = c.__prepareEnter("Counting", vec![]);
            let __e = alloc::rc::Rc::new(ActionsFrameEvent::FrameEnter { args: c.__compartment.enter_args.clone() });
            let __ctx = ActionsFrameContext::new(alloc::rc::Rc::clone(&__e), None);
            c._context_stack.push(__ctx);
            c.__kernel(&__e);
            c._context_stack.pop();
            c
        }

        fn __hsm_chain(&mut self, leaf: &str) -> &'static [&'static str] {
            match leaf {
                "Counting" => &["Counting"],
                _ => &[],
            }
        }

        fn __prepareEnter(&mut self, leaf: &str, enter_args: Vec<String>) -> ActionsCompartment {
            let chain = self.__hsm_chain(leaf);
            let mut comp: Option<ActionsCompartment> = None;
            for name in chain.iter() {
                let mut new_comp = ActionsCompartment::new(name);
                new_comp.enter_args = enter_args.clone();
                if let Some(parent) = comp.take() {
                    new_comp.parent_compartment = Some(Box::new(parent));
                }
                comp = Some(new_comp);
            }
            comp.expect("chain must contain at least the leaf state")
        }

        fn __prepareExit(&mut self, exit_args: Vec<String>) {
            self.__compartment.exit_args = exit_args.clone();
            let mut cursor = self.__compartment.parent_compartment.as_deref_mut();
            while let Some(c) = cursor {
                c.exit_args = exit_args.clone();
                cursor = c.parent_compartment.as_deref_mut();
            }
        }

        fn __kernel(&mut self, __e: &alloc::rc::Rc<ActionsFrameEvent>) {
            // Route event to current state.
            self.__router(__e);
            // Drain any transitions queued by the handler.
            while self.__next_compartment.is_some() {
                let next_compartment = self.__next_compartment.take().expect("invariant: while-loop guard checked is_some()");
                // Exit the current (leaf) state.
                let exit_args = self.__compartment.exit_args.clone();
                let exit_event = alloc::rc::Rc::new(ActionsFrameEvent::FrameExit { args: exit_args });
                self.__router(&exit_event);
                // Switch to the new compartment.
                self.__compartment = next_compartment;
                // Three-branch forward-event handling (RFC-0025 Track B.1: forward
                // event is matched on enum variant; $> recognition is now a
                // structural match, not a string compare).
                match self.__compartment.forward_event.take() {
                    None => {
                        // No forwarded event — synthesize a fresh $>.
                        let enter_args = self.__compartment.enter_args.clone();
                        let enter_event = alloc::rc::Rc::new(ActionsFrameEvent::FrameEnter { args: enter_args });
                        self.__router(&enter_event);
                    }
                    Some(fwd) if matches!(fwd, ActionsFrameEvent::FrameEnter { .. }) => {
                        // Forwarded event IS $> — dispatch directly so the
                        // destination's $> handler receives the caller's payload.
                        let fwd_rc = alloc::rc::Rc::new(fwd);
                        self.__router(&fwd_rc);
                    }
                    Some(fwd) => {
                        // Forwarded event is not $> — initialize the destination
                        // with a fresh $>, then dispatch the forward.
                        let enter_args = self.__compartment.enter_args.clone();
                        let enter_event = alloc::rc::Rc::new(ActionsFrameEvent::FrameEnter { args: enter_args });
                        self.__router(&enter_event);
                        let fwd_rc = alloc::rc::Rc::new(fwd);
                        self.__router(&fwd_rc);
                    }
                }
                for ctx in self._context_stack.iter_mut() {
                    ctx._transitioned = true;
                }
            }
        }

        fn __router(&mut self, __e: &alloc::rc::Rc<ActionsFrameEvent>) {
            let __ev: &ActionsFrameEvent = __e;
            match self.__compartment.state.as_str() {
                "Counting" => self._state_Counting(__ev),
                _ => {}
            }
        }

        fn __transition(&mut self, next_compartment: ActionsCompartment) {
            self.__next_compartment = Some(next_compartment);
        }

        pub fn increment(&mut self, n: i32) {
            let __e = alloc::rc::Rc::new(ActionsFrameEvent::Increment { n: n.clone() });
            let mut __ctx = ActionsFrameContext::new(alloc::rc::Rc::clone(&__e), None);
            self._context_stack.push(__ctx);
            self.__kernel(&__e);
            self._context_stack.pop();
        }

        pub fn get_total(&mut self) -> i32 {
            let __e = alloc::rc::Rc::new(ActionsFrameEvent::GetTotal {});
            let mut __ctx = ActionsFrameContext::new(alloc::rc::Rc::clone(&__e), None);
            self._context_stack.push(__ctx);
            self.__kernel(&__e);
            let __ctx = self._context_stack.pop().expect("invariant: handler must have pushed a context before reading return");
            match __ctx._return {
                Some(ActionsFrameReturn::GetTotal(v)) => v,
                Some(ActionsFrameReturn::_Lifecycle(v)) => v.downcast_ref::<i32>().cloned().unwrap_or_default(),
                _ => Default::default(),
            }
        }

        fn _state_Counting(&mut self, __e: &ActionsFrameEvent) {
            match __e {
                ActionsFrameEvent::GetTotal { .. } => { self._s_Counting_hdl_user_get_total(__e); }
                ActionsFrameEvent::Increment { n, .. } => {
                    self._s_Counting_hdl_user_increment(__e, *n);
                }
                _ => {}
            }
        }

        fn _s_Counting_hdl_user_get_total(&mut self, __e: &ActionsFrameEvent) {
            let __return_val = ActionsFrameReturn::GetTotal(self.total);
             if let Some(ctx) = self._context_stack.last_mut() { ctx._return = Some(__return_val); }
        }

        fn _s_Counting_hdl_user_increment(&mut self, __e: &ActionsFrameEvent, n: i32) {
            self._scale(n);
        }

        fn _scale(&mut self, n: i32) {
                        self.total = self.total + n * 2;
        }
    }
}
pub use _actions_framec::*;
