tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 231Rust 8TOML 3CSS 1317 Sutichaykuna

⎇ 70139109655efbe34ab594b10f3db52fd40f08e7 src/error.rs Rust · 49 siq'ikuna · 1.9 KB Ñawpa willay Pi imata llamkarqan ch'uya

Use slotmap instead of bespoke arena, limiting to one parent only (#196) c3f6a1727 · Jon Kelley ·

1//! The Error types produced by Taffy.
2#[cfg(feature = "std")]
3use core::fmt::{Display, Formatter, Result};
4
5use crate::node::Node;
6
7/// The error Taffy generates on invalid operations
8pub type TaffyResult<T> = core::result::Result<T, TaffyError>;
9
10/// An error that occurs while trying to access or modify a [`Node`]'s children by index.
11#[derive(Debug)]
12pub enum TaffyError {
13    /// The parent [`Node`] does not have a child at `child_index`. It only has `child_count` children
14    ChildIndexOutOfBounds {
15        /// The parent node whose child was being looked up
16        parent: Node,
17        /// The index that was looked up
18        child_index: usize,
19        /// The total number of children the parent has
20        child_count: usize,
21    },
22    /// The parent [`Node`] was not found in the [`Taffy`](crate::Taffy) instance.
23    InvalidParentNode(Node),
24    /// The child [`Node`] was not found in the [`Taffy`](crate::Taffy) instance.
25    InvalidChildNode(Node),
26    /// The supplied [`Node`] was not found in the [`Taffy`](crate::Taffy) instance.
27    InvalidInputNode(Node),
28}
29
30#[cfg(feature = "std")]
31impl Display for TaffyError {
32    fn fmt(&self, f: &mut Formatter) -> Result {
33        match self {
34            TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count } => write!(
35                f,
36                "Index (is {}) should be < child_count ({}) for parent node {:?}",
37                child_index, child_count, parent
38            ),
39            TaffyError::InvalidParentNode(parent) => {
40                write!(f, "Parent Node {:?} is not in the Taffy instance", parent)
41            }
42            TaffyError::InvalidChildNode(child) => write!(f, "Child Node {:?} is not in the Taffy instance", child),
43            TaffyError::InvalidInputNode(node) => write!(f, "Supplied Node {:?} is not in the Taffy instance", node),
44        }
45    }
46}
47
48#[cfg(feature = "std")]
49impl std::error::Error for TaffyError {}