---
source: framec/tests/rust_snapshots.rs
expression: "compile_fixture(\"05_pushpop\", \"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 _push_pop_framec {
    use super::*;
    extern crate alloc;
    use alloc::{vec, format};
    #[derive(Clone, Debug)]
    #[allow(dead_code, non_camel_case_types)]
    enum PushPopFrameEvent {
        Ping {  },
        Nest {  },
        Unnest {  },
        Leaf {  },
        FrameEnter {},
        FrameExit {},
    }

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

    #[allow(dead_code)]
    impl PushPopFrameEvent {
        fn name(&self) -> &'static str {
            match self {
                PushPopFrameEvent::Ping { .. } => "ping",
                PushPopFrameEvent::Nest { .. } => "nest",
                PushPopFrameEvent::Unnest { .. } => "unnest",
                PushPopFrameEvent::Leaf { .. } => "leaf",
                PushPopFrameEvent::FrameEnter { .. } => "$>",
                PushPopFrameEvent::FrameExit { .. } => "<$",
            }
        }
    }

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

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

    #[allow(dead_code, non_camel_case_types)]
    #[derive(Clone)]
    enum PushPopStateContext {
        Idle,
        Nested,
        __NoContext,
    }

    impl Default for PushPopStateContext {
        fn default() -> Self {
            PushPopStateContext::Idle
        }
    }

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

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

    #[allow(dead_code)]
    pub struct PushPop {
        _state_stack: Vec<PushPopCompartment>,
        __compartment: PushPopCompartment,
        __next_compartment: Option<PushPopCompartment>,
        _context_stack: Vec<PushPopFrameContext>,
        pub pings: i32,
        pub leaves: i32,
    }

    #[allow(non_snake_case)]
    impl PushPop {
        pub fn new() -> Self {
            Self {
                _state_stack: Vec::new(),
                _context_stack: Vec::new(),
                pings: 0,
                leaves: 0,
                __compartment: PushPopCompartment::new("Idle"),
                __next_compartment: None,
            }
        }

        pub fn __create() -> Self {
            let mut c = Self::new();
            c.__compartment = c.__prepareEnter("Idle");
            let __e = alloc::rc::Rc::new(PushPopFrameEvent::FrameEnter {});
            let __ctx = PushPopFrameContext::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 {
                "Idle" => &["Idle"],
                "Nested" => &["Nested"],
                _ => &[],
            }
        }

        fn __prepareEnter(&mut self, leaf: &str) -> PushPopCompartment {
            let chain = self.__hsm_chain(leaf);
            let mut comp: Option<PushPopCompartment> = None;
            for name in chain.iter() {
                let mut new_comp = PushPopCompartment::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<PushPopFrameEvent>) {
            // 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(PushPopFrameEvent::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(PushPopFrameEvent::FrameEnter {});
                        self.__router(&enter_event);
                    }
                    Some(fwd) if matches!(fwd, PushPopFrameEvent::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(PushPopFrameEvent::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<PushPopFrameEvent>) {
            let __ev: &PushPopFrameEvent = __e;
            match self.__compartment.state.as_str() {
                "Idle" => self._state_Idle(__ev),
                "Nested" => self._state_Nested(__ev),
                _ => {}
            }
        }

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

        pub fn ping(&mut self) {
            let __e = alloc::rc::Rc::new(PushPopFrameEvent::Ping {});
            let mut __ctx = PushPopFrameContext::new(alloc::rc::Rc::clone(&__e), None);
            self._context_stack.push(__ctx);
            self.__kernel(&__e);
            self._context_stack.pop();
        }

        pub fn nest(&mut self) {
            let __e = alloc::rc::Rc::new(PushPopFrameEvent::Nest {});
            let mut __ctx = PushPopFrameContext::new(alloc::rc::Rc::clone(&__e), None);
            self._context_stack.push(__ctx);
            self.__kernel(&__e);
            self._context_stack.pop();
        }

        pub fn unnest(&mut self) {
            let __e = alloc::rc::Rc::new(PushPopFrameEvent::Unnest {});
            let mut __ctx = PushPopFrameContext::new(alloc::rc::Rc::clone(&__e), None);
            self._context_stack.push(__ctx);
            self.__kernel(&__e);
            self._context_stack.pop();
        }

        pub fn leaf(&mut self) {
            let __e = alloc::rc::Rc::new(PushPopFrameEvent::Leaf {});
            let mut __ctx = PushPopFrameContext::new(alloc::rc::Rc::clone(&__e), None);
            self._context_stack.push(__ctx);
            self.__kernel(&__e);
            self._context_stack.pop();
        }

        fn _state_Idle(&mut self, __e: &PushPopFrameEvent) {
            match __e {
                PushPopFrameEvent::Nest { .. } => { self._s_Idle_hdl_user_nest(__e); }
                PushPopFrameEvent::Ping { .. } => { self._s_Idle_hdl_user_ping(__e); }
                _ => {}
            }
        }

        fn _state_Nested(&mut self, __e: &PushPopFrameEvent) {
            match __e {
                PushPopFrameEvent::Leaf { .. } => { self._s_Nested_hdl_user_leaf(__e); }
                PushPopFrameEvent::Unnest { .. } => { self._s_Nested_hdl_user_unnest(__e); }
                _ => {}
            }
        }

        fn _s_Idle_hdl_user_nest(&mut self, __e: &PushPopFrameEvent) {
            self._state_stack.push(self.__compartment.clone());
            let mut __compartment = self.__prepareEnter("Nested");
            self.__transition(__compartment);
            return;
        }

        fn _s_Idle_hdl_user_ping(&mut self, __e: &PushPopFrameEvent) {
            self.pings = self.pings + 1;
        }

        fn _s_Nested_hdl_user_leaf(&mut self, __e: &PushPopFrameEvent) {
            self.leaves = self.leaves + 1;
        }

        fn _s_Nested_hdl_user_unnest(&mut self, __e: &PushPopFrameEvent) {
            let mut __popped = self._state_stack.pop().expect("invariant: pop$ must follow push$");
            self.__transition(__popped);
            return;
        }
    }
}
pub use _push_pop_framec::*;
