1use crate::Node;
7use decanter::prelude::Hashable;
8use serde::{Deserialize, Serialize};
9use smart_default::SmartDefault;
10use std::string::ToString;
11use strum::Display;
12
13#[derive(
14 Clone,
15 Debug,
16 Deserialize,
17 Display,
18 Eq,
19 Hash,
20 Hashable,
21 Ord,
22 PartialEq,
23 PartialOrd,
24 Serialize,
25 SmartDefault,
26)]
27pub enum Payload<T = String>
28where
29 T: Default + ToString,
30{
31 #[default]
32 Leaf(T),
33 Node(Box<Node<T>>, Box<Node<T>>),
34}
35
36impl<T> Payload<T>
37where
38 T: Default + ToString,
39{
40 pub fn leaf(data: T) -> Self {
41 Self::Leaf(data)
42 }
43 pub fn node(left: Box<Node<T>>, right: Box<Node<T>>) -> Self {
44 Self::Node(left, right)
45 }
46 pub fn is_leaf(&self) -> bool {
47 match self {
48 Self::Leaf(_) => true,
49 _ => false,
50 }
51 }
52 pub fn is_node(&self) -> bool {
53 match self {
54 Self::Node(_, _) => true,
55 _ => false,
56 }
57 }
58}
59
60impl<T> From<T> for Payload<T>
61where
62 T: Default + ToString,
63{
64 fn from(data: T) -> Self {
65 Self::Leaf(data)
66 }
67}
68
69impl<T> From<(Box<Node<T>>, Box<Node<T>>)> for Payload<T>
70where
71 T: Default + ToString,
72{
73 fn from(data: (Box<Node<T>>, Box<Node<T>>)) -> Self {
74 Self::node(data.0, data.1)
75 }
76}