---
source: framec/tests/rust_snapshots.rs
expression: "compile_fixture(\"04_state_args\", \"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 _state_args_framec {
    use super::*;
    extern crate alloc;
    use alloc::{vec, format};
    #[derive(Clone, Debug)]
    #[allow(dead_code, non_camel_case_types)]
    enum StateArgsFrameEvent {
        Load { initial: i32 },
        Adjust { delta: i32 },
        Peek {  },
        FrameEnter {},
        FrameExit {},
    }

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

    #[allow(dead_code)]
    impl StateArgsFrameEvent {
        fn name(&self) -> &'static str {
            match self {
                StateArgsFrameEvent::Load { .. } => "load",
                StateArgsFrameEvent::Adjust { .. } => "adjust",
                StateArgsFrameEvent::Peek { .. } => "peek",
                StateArgsFrameEvent::FrameEnter { .. } => "$>",
                StateArgsFrameEvent::FrameExit { .. } => "<$",
            }
        }
    }

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

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

    #[derive(Clone)]
    struct HoldingContext {
        value: i32,
    }

    impl Default for HoldingContext {
        fn default() -> Self {
            Self {
                value: 0,
            }
        }
    }

    #[allow(dead_code, non_camel_case_types)]
    #[derive(Clone)]
    enum StateArgsStateContext {
        Idle,
        Holding(HoldingContext),
        __NoContext,
    }

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

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

    impl StateArgsCompartment {
        fn new(state: &str) -> Self {
            let state_context = match state {
                "Idle" => StateArgsStateContext::Idle,
                "Holding" => StateArgsStateContext::Holding(HoldingContext::default()),
                _ => StateArgsStateContext::__NoContext,
            };
            Self {
                state: state.to_string(),
                state_context,
                forward_event: None,
                parent_compartment: None,
            }
        }
    }

    #[allow(dead_code)]
    pub struct StateArgs {
        _state_stack: Vec<StateArgsCompartment>,
        __compartment: StateArgsCompartment,
        __next_compartment: Option<StateArgsCompartment>,
        _context_stack: Vec<StateArgsFrameContext>,
    }

    #[allow(non_snake_case)]
    impl StateArgs {
        pub fn new() -> Self {
            Self {
                _state_stack: Vec::new(),
                _context_stack: Vec::new(),
                __compartment: StateArgsCompartment::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(StateArgsFrameEvent::FrameEnter {});
            let __ctx = StateArgsFrameContext::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"],
                "Holding" => &["Holding"],
                _ => &[],
            }
        }

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

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

        pub fn load(&mut self, initial: i32) {
            let __e = alloc::rc::Rc::new(StateArgsFrameEvent::Load { initial: initial.clone() });
            let mut __ctx = StateArgsFrameContext::new(alloc::rc::Rc::clone(&__e), None);
            self._context_stack.push(__ctx);
            self.__kernel(&__e);
            self._context_stack.pop();
        }

        pub fn adjust(&mut self, delta: i32) {
            let __e = alloc::rc::Rc::new(StateArgsFrameEvent::Adjust { delta: delta.clone() });
            let mut __ctx = StateArgsFrameContext::new(alloc::rc::Rc::clone(&__e), None);
            self._context_stack.push(__ctx);
            self.__kernel(&__e);
            self._context_stack.pop();
        }

        pub fn peek(&mut self) -> i32 {
            let __e = alloc::rc::Rc::new(StateArgsFrameEvent::Peek {});
            let mut __ctx = StateArgsFrameContext::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(StateArgsFrameReturn::Peek(v)) => v,
                Some(StateArgsFrameReturn::_Lifecycle(v)) => v.downcast_ref::<i32>().cloned().unwrap_or_default(),
                _ => Default::default(),
            }
        }

        fn _state_Idle(&mut self, __e: &StateArgsFrameEvent) {
            match __e {
                StateArgsFrameEvent::Load { initial, .. } => {
                    self._s_Idle_hdl_user_load(__e, *initial);
                }
                _ => {}
            }
        }

        fn _state_Holding(&mut self, __e: &StateArgsFrameEvent) {
            match __e {
                StateArgsFrameEvent::Adjust { delta, .. } => {
                    self._s_Holding_hdl_user_adjust(__e, *delta);
                }
                StateArgsFrameEvent::Peek { .. } => { self._s_Holding_hdl_user_peek(__e); }
                _ => {}
            }
        }

        fn _s_Idle_hdl_user_load(&mut self, __e: &StateArgsFrameEvent, initial: i32) {
            let mut __compartment = self.__prepareEnter("Holding");
            {
                if let StateArgsStateContext::Holding(ref mut ctx) = __compartment.state_context {
                    ctx.value = initial;
                }
            }
            self.__transition(__compartment);
            return;
        }

        fn _s_Holding_hdl_user_adjust(&mut self, __e: &StateArgsFrameEvent, delta: i32) {
            let value = {
                let mut __sc = &self.__compartment;
                while __sc.state != "Holding" {
                    match __sc.parent_compartment.as_deref() {
                        Some(p) => __sc = p,
                        None => break,
                    }
                }
                match &__sc.state_context {
                    StateArgsStateContext::Holding(ctx) => ctx.value.clone(),
                    _ => Default::default(),
                }
            };
            let mut __compartment = self.__prepareEnter("Holding");
            {
                if let StateArgsStateContext::Holding(ref mut ctx) = __compartment.state_context {
                    ctx.value = value + delta;
                }
            }
            self.__transition(__compartment);
            return;
        }

        fn _s_Holding_hdl_user_peek(&mut self, __e: &StateArgsFrameEvent) {
            let value = {
                let mut __sc = &self.__compartment;
                while __sc.state != "Holding" {
                    match __sc.parent_compartment.as_deref() {
                        Some(p) => __sc = p,
                        None => break,
                    }
                }
                match &__sc.state_context {
                    StateArgsStateContext::Holding(ctx) => ctx.value.clone(),
                    _ => Default::default(),
                }
            };
            let __return_val = StateArgsFrameReturn::Peek(value);
             if let Some(ctx) = self._context_stack.last_mut() { ctx._return = Some(__return_val); }
        }
    }
}
pub use _state_args_framec::*;
