---
source: framec/tests/rust_snapshots.rs
expression: "compile_fixture(\"07_forward\", \"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 _forward_framec {
    use super::*;
    extern crate alloc;
    use alloc::{vec, format};
    #[derive(Clone, Debug)]
    #[allow(dead_code, non_camel_case_types)]
    enum ForwardFrameEvent {
        Run {  },
        Finish {  },
        FrameEnter { args: Vec<String> },
        FrameExit { args: Vec<String> },
    }

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

    #[allow(dead_code)]
    impl ForwardFrameEvent {
        fn name(&self) -> &'static str {
            match self {
                ForwardFrameEvent::Run { .. } => "run",
                ForwardFrameEvent::Finish { .. } => "finish",
                ForwardFrameEvent::FrameEnter { .. } => "$>",
                ForwardFrameEvent::FrameExit { .. } => "<$",
            }
        }
    }

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

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

    #[allow(dead_code, non_camel_case_types)]
    #[derive(Clone)]
    enum ForwardStateContext {
        Start,
        Middle,
        End,
        Empty,
    }

    impl Default for ForwardStateContext {
        fn default() -> Self {
            ForwardStateContext::Start
        }
    }

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

    impl ForwardCompartment {
        fn new(state: &str) -> Self {
            let state_context = match state {
                "Start" => ForwardStateContext::Start,
                "Middle" => ForwardStateContext::Middle,
                "End" => ForwardStateContext::End,
                _ => ForwardStateContext::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 Forward {
        _state_stack: Vec<ForwardCompartment>,
        __compartment: ForwardCompartment,
        __next_compartment: Option<ForwardCompartment>,
        _context_stack: Vec<ForwardFrameContext>,
        pub starts: i32,
        pub middles: i32,
    }

    #[allow(non_snake_case)]
    impl Forward {
        pub fn new() -> Self {
            Self {
                _state_stack: Vec::new(),
                _context_stack: Vec::new(),
                starts: 0,
                middles: 0,
                __compartment: ForwardCompartment::new("Start"),
                __next_compartment: None,
            }
        }

        pub fn __create() -> Self {
            let mut c = Self::new();
            c.__compartment = c.__prepareEnter("Start", vec![]);
            let __e = alloc::rc::Rc::new(ForwardFrameEvent::FrameEnter { args: c.__compartment.enter_args.clone() });
            let __ctx = ForwardFrameContext::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 {
                "Start" => &["Start"],
                "Middle" => &["Middle"],
                "End" => &["End"],
                _ => &[],
            }
        }

        fn __prepareEnter(&mut self, leaf: &str, enter_args: Vec<String>) -> ForwardCompartment {
            let chain = self.__hsm_chain(leaf);
            let mut comp: Option<ForwardCompartment> = None;
            for name in chain.iter() {
                let mut new_comp = ForwardCompartment::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<ForwardFrameEvent>) {
            // 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(ForwardFrameEvent::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(ForwardFrameEvent::FrameEnter { args: enter_args });
                        self.__router(&enter_event);
                    }
                    Some(fwd) if matches!(fwd, ForwardFrameEvent::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(ForwardFrameEvent::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<ForwardFrameEvent>) {
            let __ev: &ForwardFrameEvent = __e;
            match self.__compartment.state.as_str() {
                "Start" => self._state_Start(__ev),
                "Middle" => self._state_Middle(__ev),
                "End" => self._state_End(__ev),
                _ => {}
            }
        }

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

        pub fn run(&mut self) {
            let __e = alloc::rc::Rc::new(ForwardFrameEvent::Run {});
            let mut __ctx = ForwardFrameContext::new(alloc::rc::Rc::clone(&__e), None);
            self._context_stack.push(__ctx);
            self.__kernel(&__e);
            self._context_stack.pop();
        }

        pub fn finish(&mut self) {
            let __e = alloc::rc::Rc::new(ForwardFrameEvent::Finish {});
            let mut __ctx = ForwardFrameContext::new(alloc::rc::Rc::clone(&__e), None);
            self._context_stack.push(__ctx);
            self.__kernel(&__e);
            self._context_stack.pop();
        }

        fn _state_Start(&mut self, __e: &ForwardFrameEvent) {
            match __e {
                ForwardFrameEvent::Run { .. } => { self._s_Start_hdl_user_run(__e); }
                _ => {}
            }
        }

        fn _state_Middle(&mut self, __e: &ForwardFrameEvent) {
            match __e {
                ForwardFrameEvent::Finish { .. } => { self._s_Middle_hdl_user_finish(__e); }
                ForwardFrameEvent::Run { .. } => { self._s_Middle_hdl_user_run(__e); }
                _ => {}
            }
        }

        fn _state_End(&mut self, __e: &ForwardFrameEvent) {
            match __e {
                ForwardFrameEvent::Finish { .. } => { self._s_End_hdl_user_finish(__e); }
                _ => {}
            }
        }

        fn _s_Start_hdl_user_run(&mut self, __e: &ForwardFrameEvent) {
            self.starts = self.starts + 1;
            let mut __compartment = self.__prepareEnter("Middle", Vec::new());
            __compartment.forward_event = Some(__e.clone());
            self.__transition(__compartment);
            return;
        }

        fn _s_Middle_hdl_user_finish(&mut self, __e: &ForwardFrameEvent) {
            let mut __compartment = self.__prepareEnter("End", vec![]);
            self.__transition(__compartment);
            return;
        }

        fn _s_Middle_hdl_user_run(&mut self, __e: &ForwardFrameEvent) {
            self.middles = self.middles + 1;
        }

        fn _s_End_hdl_user_finish(&mut self, __e: &ForwardFrameEvent) {

        }
    }
}
pub use _forward_framec::*;
