---
source: framec/tests/rust_snapshots.rs
expression: "compile_fixture(\"11_consts\", \"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 _consts_framec {
    use super::*;
    extern crate alloc;
    use alloc::{vec, format};
    #[derive(Clone, Debug)]
    #[allow(dead_code, non_camel_case_types)]
    enum ConstsFrameEvent {
        Tick {  },
        GetCount {  },
        FrameEnter {},
        FrameExit {},
    }

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

    #[allow(dead_code)]
    impl ConstsFrameEvent {
        fn name(&self) -> &'static str {
            match self {
                ConstsFrameEvent::Tick { .. } => "tick",
                ConstsFrameEvent::GetCount { .. } => "get_count",
                ConstsFrameEvent::FrameEnter { .. } => "$>",
                ConstsFrameEvent::FrameExit { .. } => "<$",
            }
        }
    }

    #[derive(Clone, Debug)]
    #[allow(dead_code, non_camel_case_types)]
    enum ConstsFrameValue {
        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 ConstsFrameContext {
        event: alloc::rc::Rc<ConstsFrameEvent>,
        _return: Option<ConstsFrameReturn>,
        _data: alloc::collections::BTreeMap<String, ConstsFrameValue>,
        _transitioned: bool,
    }

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

    #[allow(dead_code, non_camel_case_types)]
    #[derive(Clone)]
    enum ConstsStateContext {
        Running,
        __NoContext,
    }

    impl Default for ConstsStateContext {
        fn default() -> Self {
            ConstsStateContext::Running
        }
    }

    #[allow(dead_code, non_camel_case_types)]
    #[derive(Clone)]
    struct ConstsCompartment {
        state: String,
        state_context: ConstsStateContext,
        forward_event: Option<ConstsFrameEvent>,
        parent_compartment: Option<Box<ConstsCompartment>>,
    }

    impl ConstsCompartment {
        fn new(state: &str) -> Self {
            let state_context = match state {
                "Running" => ConstsStateContext::Running,
                _ => ConstsStateContext::__NoContext,
            };
            Self {
                state: state.to_string(),
                state_context,
                forward_event: None,
                parent_compartment: None,
            }
        }
    }

    #[allow(dead_code)]
    pub struct Consts {
        _state_stack: Vec<ConstsCompartment>,
        __compartment: ConstsCompartment,
        __next_compartment: Option<ConstsCompartment>,
        _context_stack: Vec<ConstsFrameContext>,
        pub step: i32,
        pub limit: i32,
        pub count: i32,
    }

    #[allow(non_snake_case)]
    impl Consts {
        pub fn new() -> Self {
            Self {
                _state_stack: Vec::new(),
                _context_stack: Vec::new(),
                count: 0,
                __compartment: ConstsCompartment::new("Running"),
                __next_compartment: None,
                step: <i32 as Default>::default(),
                limit: <i32 as Default>::default(),
            }
        }

        pub fn __create(step: i32, limit: i32) -> Self {
            let mut c = Self::new();
            c.step = step;
            c.limit = limit;
            c.__compartment = c.__prepareEnter("Running");
            let __e = alloc::rc::Rc::new(ConstsFrameEvent::FrameEnter {});
            let __ctx = ConstsFrameContext::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 {
                "Running" => &["Running"],
                _ => &[],
            }
        }

        fn __prepareEnter(&mut self, leaf: &str) -> ConstsCompartment {
            let chain = self.__hsm_chain(leaf);
            let mut comp: Option<ConstsCompartment> = None;
            for name in chain.iter() {
                let mut new_comp = ConstsCompartment::new(name);
                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 __kernel(&mut self, __e: &alloc::rc::Rc<ConstsFrameEvent>) {
            // 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. RFC-0025.1: exit args live in the
                // source state's typed ctx (written at the transition site), so the
                // synthesized `<$` event carries no payload.
                let exit_event = alloc::rc::Rc::new(ConstsFrameEvent::FrameExit {});
                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 $>. RFC-0025.1:
                        // enter args live in the destination's typed ctx.
                        let enter_event = alloc::rc::Rc::new(ConstsFrameEvent::FrameEnter {});
                        self.__router(&enter_event);
                    }
                    Some(fwd) if matches!(fwd, ConstsFrameEvent::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_event = alloc::rc::Rc::new(ConstsFrameEvent::FrameEnter {});
                        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<ConstsFrameEvent>) {
            let __ev: &ConstsFrameEvent = __e;
            match self.__compartment.state.as_str() {
                "Running" => self._state_Running(__ev),
                _ => {}
            }
        }

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

        pub fn tick(&mut self) {
            let __e = alloc::rc::Rc::new(ConstsFrameEvent::Tick {});
            let mut __ctx = ConstsFrameContext::new(alloc::rc::Rc::clone(&__e), None);
            self._context_stack.push(__ctx);
            self.__kernel(&__e);
            self._context_stack.pop();
        }

        pub fn get_count(&mut self) -> i32 {
            let __e = alloc::rc::Rc::new(ConstsFrameEvent::GetCount {});
            let mut __ctx = ConstsFrameContext::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(ConstsFrameReturn::GetCount(v)) => v,
                Some(ConstsFrameReturn::_Lifecycle(v)) => v.downcast_ref::<i32>().cloned().unwrap_or_default(),
                _ => Default::default(),
            }
        }

        fn _state_Running(&mut self, __e: &ConstsFrameEvent) {
            match __e {
                ConstsFrameEvent::GetCount { .. } => { self._s_Running_hdl_user_get_count(__e); }
                ConstsFrameEvent::Tick { .. } => { self._s_Running_hdl_user_tick(__e); }
                _ => {}
            }
        }

        fn _s_Running_hdl_user_get_count(&mut self, __e: &ConstsFrameEvent) {
            let __return_val = ConstsFrameReturn::GetCount(self.count);
             if let Some(ctx) = self._context_stack.last_mut() { ctx._return = Some(__return_val); }
        }

        fn _s_Running_hdl_user_tick(&mut self, __e: &ConstsFrameEvent) {
            self.count = self.count + self.step;
            if self.count >= self.limit {
                self.count = 0;
            }
        }
    }
}
pub use _consts_framec::*;
