1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#![allow(clippy::module_name_repetitions)]
use std::fmt;

use bevy::{
    ecs::query::ReadOnlyWorldQuery,
    prelude::{Entity, Name, Query},
};
use bevy_mod_sysfail::FailureMode;
use thiserror::Error;

use crate::{direction::Axis, direction::Size, layout::Layout};

#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum Computed {
    ChildDefined(f32, Entity),
    Valid(f32),
}
impl Computed {
    pub(crate) fn with_child(&self, child_size: f32) -> f32 {
        match self {
            // TODO: margin
            Computed::ChildDefined(ratio, _) => *ratio * child_size,
            Computed::Valid(size) => *size,
        }
    }
}
impl From<f32> for Computed {
    fn from(value: f32) -> Self {
        Computed::Valid(value)
    }
}
impl fmt::Display for Computed {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Computed::ChildDefined(_, _) => fmt::Display::fmt("<child_size>", f),
            Computed::Valid(value) => fmt::Display::fmt(value, f),
        }
    }
}

impl From<Size<f32>> for Size<Computed> {
    fn from(Size { width, height }: Size<f32>) -> Self {
        Size { width: width.into(), height: height.into() }
    }
}

#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub enum Handle {
    Unnamed(Entity),
    Named(Name),
}
impl Handle {
    pub(crate) fn of_entity(entity: Entity, names: &Query<&Name>) -> Self {
        names
            .get(entity)
            .map_or(Handle::Unnamed(entity), |name| Handle::Named(name.clone()))
    }
    pub(crate) fn of(queries: &Layout<impl ReadOnlyWorldQuery>) -> Self {
        Self::of_entity(queries.this, queries.names)
    }
}
impl fmt::Display for Handle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Handle::Unnamed(entity) => write!(f, "<{entity:?}>"),
            Handle::Named(name) => write!(f, "{name}"),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
enum RelativeAxis {
    Main,
    Cross,
}

impl RelativeAxis {
    fn of(reference: Axis, axis: Axis) -> Self {
        match reference == axis {
            true => RelativeAxis::Main,
            false => RelativeAxis::Cross,
        }
    }
}

impl fmt::Display for RelativeAxis {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Main => f.write_str("main"),
            Self::Cross => f.write_str("cross"),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Relative {
    size: f32,
    axis: RelativeAxis,
    absolute: Axis,
}
impl Relative {
    pub(crate) fn of(reference: Axis, axis: Axis, size: f32) -> Self {
        Relative {
            size,
            axis: RelativeAxis::of(reference, axis),
            absolute: reference,
        }
    }
}

impl fmt::Display for Relative {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.size > 0.5 {
            let larger = self.size > 1.0;
            write!(
                f,
                "- children have a total relative size on the parent's {} \
                axis of {:0}% of the parent's {}.{}",
                self.axis,
                self.size * 100.0,
                self.absolute,
                if larger { " This is larger than the parent!" } else { "" },
            )?;
        }
        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, Error)]
pub(crate) enum Why {
    #[error("Both axes of a `Root` container must be `Rule::Fixed`! {this}'s {axis} is not!")]
    InvalidRoot { this: Handle, axis: Axis },
    #[error(
        "{0}'s `Node` is a `Container`, yet it has no children! Use `Node::Box` or `Node::Axis` \
        for terminal nodes!"
    )]
    ChildlessContainer(Handle),
    #[error(
        "Cyclic rule definition detected!\n\
        - {this} depends on PARENT {parent} on {axis}\n\
        - {parent} depends on CHILD {this} on {axis}\n\
        It's impossible to make sense of this circular dependency!   \
        Use different rules on {axis} for any container between {parent} and {this} \
        (included) to fix this issue."
    )]
    CyclicRule {
        this: Handle,
        parent: Handle,
        axis: Axis,
    },
    #[error(
        "Node {this}'s {axis} is overflowed by its children!\n\
        Notes:\n\
        - {this}'s inner size (excluding margins) is {size}\n\
        - There are {node_children_count} children of total {axis} {child_size}px.\n\
        - The largest child is {largest_child}\n\
        {child_relative_size}"
    )]
    ContainerOverflow {
        this: Handle,
        size: Size<f32>,
        largest_child: Handle,
        node_children_count: u32,
        axis: Axis,
        child_relative_size: Relative,
        child_size: f32,
    },
    #[error(
        "The margin of container {this} on axis {axis} has a negative value! ({margin}), \
        cuicui_layout doesn't support negative margins."
    )]
    NegativeMargin {
        this: Handle,
        axis: Axis,
        margin: f32,
    },
    #[error(
        "The margin of container {this} on axis {axis} is of {margin} pixels, \
        yet, {this} has a {axis} of {this_size} pixels! This would require \
        the content of {this} to have a negative size."
    )]
    TooMuchMargin {
        this: Handle,
        axis: Axis,
        margin: f32,
        this_size: f32,
    },
}

impl Why {
    pub(crate) fn bad_rule(
        axis: Axis,
        parent: Entity,
        queries: &Layout<impl ReadOnlyWorldQuery>,
    ) -> Self {
        Why::CyclicRule {
            this: Handle::of(queries),
            parent: Handle::of_entity(parent, queries.names),
            axis,
        }
    }

    pub(crate) fn invalid_root(axis: Axis, entity: Entity, names: &Query<&Name>) -> Self {
        Why::InvalidRoot { this: Handle::of_entity(entity, names), axis }
    }
}
/// An error caused by a bad layout.
#[derive(Debug, Error)]
#[error(transparent)]
pub struct ComputeLayoutError(#[from] Why);

/// Uniquely identifies an error
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub enum ErrorId {
    ChildlessContainer(Handle),
    CyclicRule(Handle),
    ContainerOverflow(Handle),
    NegativeMargin(Handle),
    InvalidRoot(Handle),
    TooMuchMargin(Handle),
}

impl FailureMode for ComputeLayoutError {
    fn log_level(&self) -> bevy_mod_sysfail::LogLevel {
        bevy_mod_sysfail::LogLevel::Error
    }

    type ID = ErrorId;

    fn identify(&self) -> Self::ID {
        match &self.0 {
            Why::ChildlessContainer(this) => ErrorId::ChildlessContainer(this.clone()),
            Why::CyclicRule { this, .. } => ErrorId::CyclicRule(this.clone()),
            Why::ContainerOverflow { this, .. } => ErrorId::ContainerOverflow(this.clone()),
            Why::NegativeMargin { this, .. } => ErrorId::NegativeMargin(this.clone()),
            Why::InvalidRoot { this, .. } => ErrorId::InvalidRoot(this.clone()),
            Why::TooMuchMargin { this, .. } => ErrorId::TooMuchMargin(this.clone()),
        }
    }
    fn display(&self) -> Option<String> {
        Some(self.to_string())
    }
}