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
//! Encoding representation of trees and nodes

use serde::{de::Error as _, Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TreeType {
  /// A root.
  Root = 0,

  /// A local root.
  Local = 1,
}

impl Serialize for TreeType {
  fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  where
    S: serde::Serializer,
  {
    (*self as u8).serialize(serializer)
  }
}

impl<'de> Deserialize<'de> for TreeType {
  fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  where
    D: serde::Deserializer<'de>,
  {
    match u8::deserialize(deserializer)? {
      0 => Ok(TreeType::Root),
      1 => Ok(TreeType::Local),
      _ => Err(D::Error::custom("nope")),
    }
  }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Tree {
  /// Protocol version the tree is compatible with.
  #[serde(default)]
  pub version: Version,

  /// Type of node.
  #[serde(rename = "type")]
  pub ty: TreeType,

  /// A tree is also a node, so we flatten the content of a node when doing deser.
  #[serde(flatten)]
  pub node: Node,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Node {
  /// Icon associated with this node.
  #[serde(default)]
  pub(crate) icon: String,

  /// Whether the node is expanded or collapsed.
  #[serde(default)]
  pub(crate) is_expanded: bool,

  /// Text associated with the node.
  pub(crate) contents: Vec<Text>,

  /// Data file associated with, if any.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub(crate) data: Option<PathBuf>,

  /// Link associated with, if any.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub(crate) url: Option<String>,

  /// Children nodes, if any.
  #[serde(default)]
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub(crate) children: Vec<Node>,
}

impl Node {
  #[cfg(test)]
  pub(crate) fn new_by_expand_state(
    name: impl Into<String>,
    is_expanded: bool,
    children: Vec<Node>,
  ) -> Self {
    Self {
      icon: String::new(),
      is_expanded,
      contents: vec![Text { text: name.into() }],
      data: None,
      url: None,
      children,
    }
  }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
pub struct Version(u16);

impl Version {
  pub const fn current() -> Self {
    Version(1)
  }
}

impl Default for Version {
  fn default() -> Self {
    Version::current()
  }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Text {
  pub text: String,
}

#[cfg(test)]
mod tests {
  use super::Node;

  #[test]
  fn serialize() {
    let s = serde_json::to_string(&Node::new_by_expand_state("name", true, Vec::new()));
    assert_eq!(
      s.unwrap(),
      r#"{"icon":"","is_expanded":true,"contents":[{"text":"name"}]}"#
    );
  }

  #[test]
  fn deserialize() {
    let s = r#"{"icon":"","is_expanded":true,"contents":[{"text":"name"}]}"#;
    let node = serde_json::from_str::<Node>(s);
    assert_eq!(
      node.unwrap(),
      Node::new_by_expand_state("name", true, Vec::new())
    );
  }
}