raui_core/
lib.rs

1//! RAUI core types and components
2//!
3//! The things that most users will be interested in here are the [components][widget::component].
4//! Those have more documentation on how to use widgets, components, etc. in your app.
5
6pub mod application;
7#[macro_use]
8pub mod messenger;
9#[macro_use]
10pub mod props;
11pub mod renderer;
12pub mod state;
13#[macro_use]
14pub mod widget;
15pub mod animator;
16pub mod interactive;
17pub mod layout;
18pub mod signals;
19pub mod tester;
20pub mod view_model;
21
22pub type Scalar = f32;
23pub type Integer = i32;
24pub type UnsignedInteger = u32;
25
26pub use raui_derive::*;
27use serde::{Serialize, de::DeserializeOwned};
28
29#[doc(inline)]
30pub use serde_json::{Number as PrefabNumber, Value as PrefabValue};
31
32pub use intuicio_data::{lifetime::*, managed::*, type_hash::*};
33
34#[doc(hidden)]
35pub mod prelude {
36    pub use crate::{
37        Integer, LogKind, Logger, MessageData, Prefab, PrefabError, PrintLogger, PropsData, Scalar,
38        animator::*,
39        application::*,
40        implement_message_data, implement_props_data,
41        interactive::default_interactions_engine::*,
42        interactive::*,
43        layout::default_layout_engine::*,
44        layout::*,
45        make_widget,
46        messenger::*,
47        post_hooks, pre_hooks,
48        props::*,
49        renderer::*,
50        signals::*,
51        state::*,
52        unpack_named_slots,
53        view_model::*,
54        widget,
55        widget::*,
56        widget::{
57            component::*,
58            component::{
59                containers::{
60                    anchor_box::*, area_box::*, content_box::*, context_box::*, flex_box::*,
61                    grid_box::*, hidden_box::*, horizontal_box::*, portal_box::*, scroll_box::*,
62                    size_box::*, switch_box::*, tabs_box::*, tooltip_box::*, variant_box::*,
63                    vertical_box::*, wrap_box::*,
64                },
65                image_box::*,
66                interactive::*,
67                interactive::{
68                    button::*, input_field::*, navigation::*, options_view::*, scroll_view::*,
69                    slider_view::*,
70                },
71                space_box::*,
72                text_box::*,
73            },
74            context::*,
75            node::*,
76            unit::*,
77            unit::{area::*, content::*, flex::*, grid::*, image::*, portal::*, size::*, text::*},
78            utils::*,
79        },
80    };
81    pub use intuicio_data::{lifetime::*, managed::*, type_hash::*};
82}
83
84/// An error that can occur while processing a [`Prefab`]
85#[derive(Debug, Clone)]
86pub enum PrefabError {
87    CouldNotSerialize(String),
88    CouldNotDeserialize(String),
89}
90
91/// The [`Prefab`] trait is implemented for types that are able to translate to and from
92/// [`PrefabValue`]'s
93///
94/// [`PrefabValue`]'s can then, in turn, be serialized or deserialized for persistance, transfer, or
95/// other purposes.
96pub trait Prefab: Serialize + DeserializeOwned {
97    fn from_prefab(data: PrefabValue) -> Result<Self, PrefabError> {
98        match serde_json::from_value(data) {
99            Ok(result) => Ok(result),
100            Err(error) => Err(PrefabError::CouldNotDeserialize(error.to_string())),
101        }
102    }
103
104    fn to_prefab(&self) -> Result<PrefabValue, PrefabError> {
105        match serde_json::to_value(self) {
106            Ok(result) => Ok(result),
107            Err(error) => Err(PrefabError::CouldNotSerialize(error.to_string())),
108        }
109    }
110}
111
112#[derive(Debug, Copy, Clone, PartialEq, Eq)]
113pub enum LogKind {
114    Info,
115    Warning,
116    Error,
117}
118
119/// Common logging interface that custom log engines should follow to enable their reusability
120/// across different modules that will log messages to text output targets.
121/// Objects that implement this trait should be considered text output targets, for example text
122/// streams, terminal, network-based loggers, even application screen.
123pub trait Logger {
124    /// Log message to this type of text output target.
125    ///
126    /// # Arguments
127    /// * `kind` - Kind of log message.
128    /// * `message` - Message string slice.
129    fn log(&mut self, _kind: LogKind, _message: &str) {}
130}
131
132impl Logger for () {}
133
134/// Prints log messages to terminal via println! macro.
135pub struct PrintLogger;
136
137impl Logger for PrintLogger {
138    fn log(&mut self, kind: LogKind, message: &str) {
139        println!("{:?} | {}", kind, message);
140    }
141}