oxide_mvu/lib.rs
1#![cfg_attr(feature = "no_std", no_std)]
2
3//! A lightweight Model-View-Update (MVU) runtime for Rust with `no_std` support.
4//!
5//! Implements the MVU pattern for building predictable, testable applications with
6//! unidirectional data flow and controlled side effects.
7//!
8//! ## Example
9//!
10//! ```rust
11//! use oxide_mvu::{Emitter, Effect, MvuLogic, MvuRuntime, Renderer};
12//!
13//! #[derive(Clone)]
14//! enum Event { AccumulateClicked }
15//!
16//! #[derive(Clone)]
17//! struct Model { count: i32 }
18//!
19//! struct Props { count: i32, on_accumulate_click: Box<dyn Fn()> }
20//!
21//! struct MyLogic;
22//!
23//! impl MvuLogic<Event, Model, Props> for MyLogic {
24//! fn init(&self, model: Model) -> (Model, Effect<Event>) {
25//! (model, Effect::none())
26//! }
27//!
28//! fn update(&self, event: Event, model: &Model) -> (Model, Effect<Event>) {
29//! match event {
30//! Event::AccumulateClicked => {
31//! let new_model = Model {
32//! count: model.count + 1,
33//! ..model.clone()
34//! };
35//! (new_model, Effect::none())
36//! }
37//! }
38//! }
39//!
40//! fn view(&self, model: &Model, emitter: &Emitter<Event>) -> Props {
41//! let emitter = emitter.clone();
42//! Props {
43//! count: model.count,
44//! on_accumulate_click: Box::new(move || emitter.emit(Event::AccumulateClicked))
45//! }
46//! }
47//! }
48//!
49//! struct MyRenderer;
50//! impl Renderer<Props> for MyRenderer {
51//! fn render(&mut self, _props: Props) {}
52//! }
53//!
54//! // Create a spawner for your async runtime
55//! let spawner = Box::new(|_fut| {
56//! // Spawn the future on your chosen runtime
57//! // e.g., tokio::spawn(fut); or async_std::task::spawn(fut);
58//! });
59//!
60//! let runtime = MvuRuntime::new(
61//! Model { count: 0 },
62//! Box::new(MyLogic),
63//! Box::new(MyRenderer),
64//! spawner
65//! );
66//! runtime.run();
67//! ```
68
69#[cfg(feature = "no_std")]
70extern crate alloc;
71
72// Module declarations
73mod effect;
74mod emitter;
75mod logic;
76mod renderer;
77mod runtime;
78
79// Public re-exports
80pub use effect::{Effect, Spawner};
81pub use emitter::Emitter;
82pub use logic::MvuLogic;
83pub use renderer::Renderer;
84pub use runtime::MvuRuntime;
85
86// Test utilities (only available with 'testing' feature or during tests)
87#[cfg(any(test, feature = "testing"))]
88pub use renderer::TestRenderer;
89#[cfg(any(test, feature = "testing"))]
90pub use runtime::{create_test_spawner, TestMvuDriver, TestMvuRuntime};