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
//! This module contains types and methods around the [`Node`] enum. The
//! variants of the enum are `LeafNode` and [`ParentNode`], both of which are
//! defined in the respective [`leaf_node`] and [`parent_node`] submodules.
use serde::{Deserialize, Serialize};
use tls_codec::{TlsDeserialize, TlsDeserializeBytes, TlsSerialize, TlsSize};

use self::{leaf_node::LeafNodeIn, parent_node::ParentNode};

use super::LeafNode;

mod codec;
pub(crate) mod encryption_keys;
pub(crate) mod leaf_node;
pub(crate) mod parent_node;

/// Container enum for leaf and parent nodes.
///
/// ```c
/// // draft-ietf-mls-protocol-17
/// struct {
///     NodeType node_type;
///     select (Node.node_type) {
///         case leaf:   LeafNode leaf_node;
///         case parent: ParentNode parent_node;
///     };
/// } Node;
/// ```
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, TlsSize, TlsSerialize)]
#[repr(u8)]
pub enum Node {
    /// A leaf node.
    #[tls_codec(discriminant = 1)]
    LeafNode(LeafNode),
    /// A parent node.
    #[tls_codec(discriminant = 2)]
    ParentNode(ParentNode),
}

#[derive(
    Debug,
    PartialEq,
    Eq,
    Clone,
    Serialize,
    Deserialize,
    TlsSize,
    TlsDeserialize,
    TlsDeserializeBytes,
    TlsSerialize,
)]
#[repr(u8)]
pub enum NodeIn {
    /// A leaf node.
    #[tls_codec(discriminant = 1)]
    LeafNode(LeafNodeIn),
    /// A parent node.
    #[tls_codec(discriminant = 2)]
    ParentNode(ParentNode),
}

impl From<Node> for NodeIn {
    fn from(node: Node) -> Self {
        match node {
            Node::LeafNode(leaf_node) => NodeIn::LeafNode(leaf_node.into()),
            Node::ParentNode(parent_node) => NodeIn::ParentNode(parent_node),
        }
    }
}

// The following `From` implementation breaks abstraction layers and MUST
// NOT be made available outside of tests or "test-utils".
#[cfg(any(feature = "test-utils", test))]
impl From<NodeIn> for Node {
    fn from(node: NodeIn) -> Self {
        match node {
            NodeIn::LeafNode(leaf_node) => Node::LeafNode(leaf_node.into()),
            NodeIn::ParentNode(parent_node) => Node::ParentNode(parent_node),
        }
    }
}

/// Container enum with reference to a node in a tree.
pub(crate) enum NodeReference<'a> {
    Leaf(&'a LeafNode),
    Parent(&'a ParentNode),
}

#[cfg(test)]
impl Node {
    #[allow(unused)]
    pub(crate) fn into_leaf(self) -> LeafNode {
        match self {
            Node::LeafNode(l) => l,
            Node::ParentNode(_) => panic!("Tried to convert parent node into leaf node."),
        }
    }
}