docx_reader/documents/elements/
a_graphic_data.rs

1use super::*;
2use serde::ser::{SerializeStruct, Serializer};
3use serde::Serialize;
4use std::str::FromStr;
5
6/*
7  20.1.2.2.17
8  graphicData (Graphic Object Data)
9  This element specifies the reference to a graphic object within the document. This graphic object is provided
10  entirely by the document authors who choose to persist this data within the document.
11*/
12#[derive(Debug, Clone, Serialize, PartialEq)]
13#[serde(rename_all = "camelCase")]
14pub struct AGraphicData {
15	pub data_type: GraphicDataType,
16	pub children: Vec<GraphicDataChild>,
17}
18
19#[derive(Debug, Clone, PartialEq)]
20pub enum GraphicDataChild {
21	Shape(WpsShape),
22	Pic(Pic),
23}
24
25impl Serialize for GraphicDataChild {
26	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
27	where
28		S: Serializer,
29	{
30		match *self {
31			GraphicDataChild::Shape(ref s) => {
32				let mut t = serializer.serialize_struct("Shape", 2)?;
33				t.serialize_field("type", "shape")?;
34				t.serialize_field("data", s)?;
35				t.end()
36			}
37			GraphicDataChild::Pic(ref s) => {
38				let mut t = serializer.serialize_struct("Pic", 2)?;
39				t.serialize_field("type", "pic")?;
40				t.serialize_field("data", s)?;
41				t.end()
42			}
43		}
44	}
45}
46
47impl FromStr for GraphicDataType {
48	type Err = ();
49	fn from_str(s: &str) -> Result<Self, Self::Err> {
50		if s.ends_with("picture") {
51			return Ok(GraphicDataType::Picture);
52		}
53		if s.ends_with("wordprocessingShape") {
54			return Ok(GraphicDataType::WpShape);
55		}
56		Ok(GraphicDataType::Unsupported)
57	}
58}
59
60#[derive(Debug, Clone, Serialize, PartialEq)]
61#[serde(rename_all = "camelCase")]
62pub enum GraphicDataType {
63	Picture,
64	WpShape,
65	Unsupported,
66}
67
68impl AGraphicData {
69	pub fn new(data_type: GraphicDataType) -> AGraphicData {
70		AGraphicData {
71			data_type,
72			children: vec![],
73		}
74	}
75
76	pub fn add_shape(mut self, shape: WpsShape) -> Self {
77		self.children.push(GraphicDataChild::Shape(shape));
78		self
79	}
80}