---
source: framec/tests/rust_snapshots.rs
expression: "compile_fixture(\"08_lifecycle\", \"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 _lifecycle_framec {
    use super::*;
    extern crate alloc;
    use alloc::{vec, format};
    #[derive(Clone, Debug)]
    #[allow(dead_code, non_camel_case_types)]
    enum LifecycleFrameEvent {
        Start { label: String },
        Stop {  },
        FrameEnter { args: Vec<String> },
        FrameExit { args: Vec<String> },
    }

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

    #[allow(dead_code)]
    impl LifecycleFrameEvent {
        fn name(&self) -> &'static str {
            match self {
                LifecycleFrameEvent::Start { .. } => "start",
                LifecycleFrameEvent::Stop { .. } => "stop",
                LifecycleFrameEvent::FrameEnter { .. } => "$>",
                LifecycleFrameEvent::FrameExit { .. } => "<$",
            }
        }
    }

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

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

    #[allow(dead_code, non_camel_case_types)]
    #[derive(Clone)]
    enum LifecycleStateContext {
        Idle,
        Running,
        Empty,
    }

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

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

    impl LifecycleCompartment {
        fn new(state: &str) -> Self {
            let state_context = match state {
                "Idle" => LifecycleStateContext::Idle,
                "Running" => LifecycleStateContext::Running,
                _ => LifecycleStateContext::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 Lifecycle {
        _state_stack: Vec<LifecycleCompartment>,
        __compartment: LifecycleCompartment,
        __next_compartment: Option<LifecycleCompartment>,
        _context_stack: Vec<LifecycleFrameContext>,
        pub entered: i32,
        pub exited: i32,
        pub tag: String,
    }

    #[allow(non_snake_case)]
    impl Lifecycle {
        pub fn new() -> Self {
            Self {
                _state_stack: Vec::new(),
                _context_stack: Vec::new(),
                entered: 0,
                exited: 0,
                tag: String::from(""),
                __compartment: LifecycleCompartment::new("Idle"),
                __next_compartment: None,
            }
        }

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

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

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

        pub fn start(&mut self, label: String) {
            let __e = alloc::rc::Rc::new(LifecycleFrameEvent::Start { label: label.clone() });
            let mut __ctx = LifecycleFrameContext::new(alloc::rc::Rc::clone(&__e), None);
            self._context_stack.push(__ctx);
            self.__kernel(&__e);
            self._context_stack.pop();
        }

        pub fn stop(&mut self) {
            let __e = alloc::rc::Rc::new(LifecycleFrameEvent::Stop {});
            let mut __ctx = LifecycleFrameContext::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: &LifecycleFrameEvent) {
            match __e {
                LifecycleFrameEvent::Start { label, .. } => {
                    self._s_Idle_hdl_user_start(__e, label.clone());
                }
                _ => {}
            }
        }

        fn _state_Running(&mut self, __e: &LifecycleFrameEvent) {
            match __e {
                LifecycleFrameEvent::FrameExit { .. } => { self._s_Running_hdl_frame_exit(__e); }
                LifecycleFrameEvent::FrameEnter { args } => {
                    let label: String = args.get(0).cloned().unwrap_or_default();
                    self._s_Running_hdl_frame_enter(__e, label);
                }
                LifecycleFrameEvent::Stop { .. } => { self._s_Running_hdl_user_stop(__e); }
                _ => {}
            }
        }

        fn _s_Idle_hdl_user_start(&mut self, __e: &LifecycleFrameEvent, label: String) {
            (label);
            let mut __compartment = self.__prepareEnter("Running", vec![]);
            self.__transition(__compartment);
            return;
        }

        fn _s_Running_hdl_frame_exit(&mut self, __e: &LifecycleFrameEvent) {
            self.exited = self.exited + 1;
        }

        fn _s_Running_hdl_frame_enter(&mut self, __e: &LifecycleFrameEvent, label: String) {
            self.entered = self.entered + 1;
            self.tag = label;
        }

        fn _s_Running_hdl_user_stop(&mut self, __e: &LifecycleFrameEvent) {
            let mut __compartment = self.__prepareEnter("Idle", vec![]);
            self.__transition(__compartment);
            return;
        }
    }
}
pub use _lifecycle_framec::*;
