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 [`Node`] was not found in the [`Taffy`](crate::Taffy) instance
8#[derive(Debug)]
9pub struct InvalidNode(pub Node);
10
11#[cfg(feature = "std")]
12impl Display for InvalidNode {
13 fn fmt(&self, f: &mut Formatter) -> Result {
14 write!(f, "Node {:?} is not in the Taffy instance", self.0)
15 }
16}
17
18#[cfg(feature = "std")]
19impl std::error::Error for InvalidNode {}
20
21/// An error that occurs while trying to access or modify a [`Node`]'s children by index.
22#[derive(Debug)]
23pub enum InvalidChild {
24 /// The parent [`Node`] does not have a child at `child_index`. It only has `child_count` children
25 ChildIndexOutOfBounds {
26 /// The parent node whose child was being looked up
27 parent: Node,
28 /// The index that was looked up
29 child_index: usize,
30 /// The total number of children the parent has
31 child_count: usize,
32 },
33 /// The parent [`Node`] was not found in the [`Taffy`](crate::Taffy) instance.
34 InvalidParentNode(Node),
35 /// The child [`Node`] was not found in the [`Taffy`](crate::Taffy) instance.
36 InvalidChildNode(Node),
37}
38
39#[cfg(feature = "std")]
40impl Display for InvalidChild {
41 fn fmt(&self, f: &mut Formatter) -> Result {
42 match self {
43 InvalidChild::ChildIndexOutOfBounds { parent, child_index, child_count } => write!(
44 f,
45 "Index (is {}) should be < child_count ({}) for parent node {:?}",
46 child_index, child_count, parent
47 ),
48 InvalidChild::InvalidParentNode(parent) => {
49 write!(f, "Parent Node {:?} is not in the Taffy instance", parent)
50 }
51 InvalidChild::InvalidChildNode(child) => write!(f, "Child Node {:?} is not in the Taffy instance", child),
52 }
53 }
54}
55
56#[cfg(feature = "std")]
57impl std::error::Error for InvalidChild {}