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