docx_reader/documents/elements/
a_graphic_data.rs

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
use super::*;
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
use std::str::FromStr;

/*
  20.1.2.2.17
  graphicData (Graphic Object Data)
  This element specifies the reference to a graphic object within the document. This graphic object is provided
  entirely by the document authors who choose to persist this data within the document.
*/
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AGraphicData {
	pub data_type: GraphicDataType,
	pub children: Vec<GraphicDataChild>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum GraphicDataChild {
	Shape(WpsShape),
	Pic(Pic),
}

impl Serialize for GraphicDataChild {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: Serializer,
	{
		match *self {
			GraphicDataChild::Shape(ref s) => {
				let mut t = serializer.serialize_struct("Shape", 2)?;
				t.serialize_field("type", "shape")?;
				t.serialize_field("data", s)?;
				t.end()
			}
			GraphicDataChild::Pic(ref s) => {
				let mut t = serializer.serialize_struct("Pic", 2)?;
				t.serialize_field("type", "pic")?;
				t.serialize_field("data", s)?;
				t.end()
			}
		}
	}
}

impl FromStr for GraphicDataType {
	type Err = ();
	fn from_str(s: &str) -> Result<Self, Self::Err> {
		if s.ends_with("picture") {
			return Ok(GraphicDataType::Picture);
		}
		if s.ends_with("wordprocessingShape") {
			return Ok(GraphicDataType::WpShape);
		}
		Ok(GraphicDataType::Unsupported)
	}
}

#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum GraphicDataType {
	Picture,
	WpShape,
	Unsupported,
}

impl AGraphicData {
	pub fn new(data_type: GraphicDataType) -> AGraphicData {
		AGraphicData {
			data_type,
			children: vec![],
		}
	}

	pub fn add_shape(mut self, shape: WpsShape) -> Self {
		self.children.push(GraphicDataChild::Shape(shape));
		self
	}
}