node.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. use crate::core::{NodeAttributes, TextDelta};
  2. use serde::{Deserialize, Serialize};
  3. #[derive(Clone, Eq, PartialEq, Debug)]
  4. pub struct NodeData {
  5. pub node_type: String,
  6. pub attributes: NodeAttributes,
  7. pub delta: Option<TextDelta>,
  8. }
  9. impl NodeData {
  10. pub fn new(node_type: &str) -> NodeData {
  11. NodeData {
  12. node_type: node_type.into(),
  13. attributes: NodeAttributes::new(),
  14. delta: None,
  15. }
  16. }
  17. }
  18. #[derive(Clone, Serialize, Deserialize, Eq, PartialEq)]
  19. pub struct NodeSubTree {
  20. #[serde(rename = "type")]
  21. pub note_type: String,
  22. pub attributes: NodeAttributes,
  23. #[serde(skip_serializing_if = "Option::is_none")]
  24. pub delta: Option<TextDelta>,
  25. #[serde(skip_serializing_if = "Vec::is_empty")]
  26. pub children: Vec<NodeSubTree>,
  27. }
  28. impl NodeSubTree {
  29. pub fn new(node_type: &str) -> NodeSubTree {
  30. NodeSubTree {
  31. note_type: node_type.into(),
  32. attributes: NodeAttributes::new(),
  33. delta: None,
  34. children: Vec::new(),
  35. }
  36. }
  37. pub fn to_node_data(&self) -> NodeData {
  38. NodeData {
  39. node_type: self.note_type.clone(),
  40. attributes: self.attributes.clone(),
  41. delta: self.delta.clone(),
  42. }
  43. }
  44. }