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::{de::DeserializeOwned, Serialize};
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        animator::*,
38        application::*,
39        implement_message_data, implement_props_data,
40        interactive::default_interactions_engine::*,
41        interactive::*,
42        layout::default_layout_engine::*,
43        layout::*,
44        make_widget,
45        messenger::*,
46        post_hooks, pre_hooks,
47        props::*,
48        renderer::*,
49        signals::*,
50        state::*,
51        unpack_named_slots,
52        view_model::*,
53        widget,
54        widget::*,
55        widget::{
56            component::*,
57            component::{
58                containers::{
59                    anchor_box::*, area_box::*, content_box::*, context_box::*, flex_box::*,
60                    grid_box::*, hidden_box::*, horizontal_box::*, portal_box::*, scroll_box::*,
61                    size_box::*, switch_box::*, tabs_box::*, tooltip_box::*, variant_box::*,
62                    vertical_box::*, wrap_box::*,
63                },
64                image_box::*,
65                interactive::*,
66                interactive::{
67                    button::*, input_field::*, navigation::*, options_view::*, scroll_view::*,
68                    slider_view::*,
69                },
70                space_box::*,
71                text_box::*,
72            },
73            context::*,
74            node::*,
75            unit::*,
76            unit::{area::*, content::*, flex::*, grid::*, image::*, portal::*, size::*, text::*},
77            utils::*,
78        },
79        Integer, LogKind, Logger, MessageData, Prefab, PrefabError, PrintLogger, PropsData, Scalar,
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}