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 intuicio_data::{
27    lifetime::*,
28    managed::{gc::*, value::*, *},
29    type_hash::*,
30};
31pub use raui_derive::*;
32use serde::{Serialize, de::DeserializeOwned};
33#[doc(inline)]
34pub use serde_json::{Number as PrefabNumber, Value as PrefabValue};
35
36/// An error that can occur while processing a [`Prefab`]
37#[derive(Debug, Clone)]
38pub enum PrefabError {
39    CouldNotSerialize(String),
40    CouldNotDeserialize(String),
41}
42
43/// The [`Prefab`] trait is implemented for types that are able to translate to and from
44/// [`PrefabValue`]'s
45///
46/// [`PrefabValue`]'s can then, in turn, be serialized or deserialized for persistance, transfer, or
47/// other purposes.
48pub trait Prefab: Serialize + DeserializeOwned {
49    fn from_prefab(data: PrefabValue) -> Result<Self, PrefabError> {
50        match serde_json::from_value(data) {
51            Ok(result) => Ok(result),
52            Err(error) => Err(PrefabError::CouldNotDeserialize(error.to_string())),
53        }
54    }
55
56    fn to_prefab(&self) -> Result<PrefabValue, PrefabError> {
57        match serde_json::to_value(self) {
58            Ok(result) => Ok(result),
59            Err(error) => Err(PrefabError::CouldNotSerialize(error.to_string())),
60        }
61    }
62}
63
64#[derive(Debug, Copy, Clone, PartialEq, Eq)]
65pub enum LogKind {
66    Info,
67    Warning,
68    Error,
69}
70
71/// Common logging interface that custom log engines should follow to enable their reusability
72/// across different modules that will log messages to text output targets.
73/// Objects that implement this trait should be considered text output targets, for example text
74/// streams, terminal, network-based loggers, even application screen.
75pub trait Logger {
76    /// Log message to this type of text output target.
77    ///
78    /// # Arguments
79    /// * `kind` - Kind of log message.
80    /// * `message` - Message string slice.
81    fn log(&mut self, _kind: LogKind, _message: &str) {}
82}
83
84impl Logger for () {}
85
86/// Prints log messages to terminal via println! macro.
87pub struct PrintLogger;
88
89impl Logger for PrintLogger {
90    fn log(&mut self, kind: LogKind, message: &str) {
91        println!("{kind:?} | {message}");
92    }
93}