docx_reader/documents/elements/
a_graphic_data.rsuse super::*;
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
use std::str::FromStr;
#[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
}
}