bevy_lunex_core/code/
types.rs

1use bevy::{utils::thiserror::Error, ecs::component::Component};
2use pathio::PathioError;
3use crate::Container;
4
5#[derive(Clone, Component, Debug, Error, PartialEq)]
6pub enum LunexError {
7    /// Error that happens when merging branches. Two branches have the same name.
8    #[error("Container from merging branch was not dropped before merging")]
9    ContainerConflict,
10
11    /// Error that happens when merging branches. Two branches have the same name.
12    #[error("Duplicate name conflict for '{0:}' when trying to merge directory")]
13    DuplicateName (String),
14
15    /// Error that happens when merging branches. Two branches have the same name.
16    #[error("Name '{0:}' is already in use")]
17    NameInUse (String),
18
19    /// Error that happens when path provided is not allowed.
20    #[error("Path '{0:}' is not allowed")]
21    InvalidPath (String),
22
23    /// Error that happens when you try to locate a branch that doesn't exist.
24    #[error("Unable to locate '{0:}' branch")]
25    NoBranch (String),
26
27
28
29
30    /// Error that happens when [`crate::Widget`] fails to locate itself in [`crate::UiTree`].
31    #[error("could not find '{path:}': {cause:}")]
32    FetchError {
33        path: String,
34        cause: Box<LunexError>,
35    },
36}
37impl From<PathioError> for LunexError {
38    fn from(value: PathioError) -> Self {
39        match value {
40            PathioError::FileConflict => LunexError::ContainerConflict,
41            PathioError::DuplicateName (v) => LunexError::DuplicateName (v),
42            PathioError::NameInUse (v) => LunexError::NameInUse (v),
43            PathioError::NoDirectory (v) => LunexError::NoBranch (v),
44            PathioError::NoFile (_) => panic!("API should NOT ALLOW you to get this error"),
45            PathioError::InvalidPath (v) => LunexError::InvalidPath (v),
46        }
47    }
48}
49
50
51/// # DataWrap
52/// Wrapping struct that automates working with the appended data on the branch.
53/// Holds custom widget data (T) and [`Container`] data.
54#[derive(Clone, Component, Debug, Default, PartialEq)]
55pub struct DataWrap<T:Default> {
56    container: Container,
57    data: Option<T>,
58}
59impl <T: Default> DataWrap<T> {
60    pub fn new(container: Container) -> Self {
61        DataWrap {
62            container,
63            data: None,
64        }
65    }
66    /// Borrow a container wrapped in the struct.
67    pub fn container(&self) -> &Container {
68        &self.container
69    }
70    /// Mutably borrow a container wrapped in the struct.
71    pub fn container_mut(&mut self) -> &mut Container {
72        &mut self.container
73    }
74    /// Borrow a data(T) wrapped in the struct, still requires mut access
75    /// because the data struct is created using default method if it doesn't exist.
76    pub fn data(&mut self) -> &T {
77        if let None = &self.data {
78            self.data = Some(T::default());
79        }
80        match &self.data {
81            Some(t) => t,
82            None => unreachable!()
83        }
84    }
85    /// Mutably borrow a data(T) wrapped in the struct, struct is created using default method if it doesn't exist. 
86    pub fn data_mut(&mut self) -> &mut T {
87        if let None = &self.data {
88            self.data = Some(T::default());
89        }
90        match &mut self.data {
91            Some(t) => t,
92            None => unreachable!()
93        }
94    }
95}
96
97#[derive(Clone, Component, Debug, Default, PartialEq)]
98pub struct Size {
99    pub width: f32,
100    pub height: f32,
101}
102
103/// # Modifier
104/// A special enum dictating if *(T)* is supposed to be
105/// **added** to a specific property or **override** it instead
106#[derive(Clone, Copy, Component, Debug, PartialEq)]
107pub enum Modifier<T> {
108    Add (T),
109    Set (T),
110}