docx-rs/docx-core/src/reader/xml_element.rs

146 lines
4.3 KiB
Rust
Raw Normal View History

use std::io::Read;
use std::str::FromStr;
use xml::attribute::OwnedAttribute;
use xml::reader::EventReader;
use crate::reader::ReaderError;
#[derive(PartialEq, Debug)]
pub enum XMLElement {
Body,
Paragraph,
Run,
RunProperty,
Color,
Underline,
Size,
SizeCs,
Vanish,
Italic,
ItalicCs,
Text,
Highlight,
Bold,
BoldCs,
Break,
Tab,
ParagraphStyle,
Indent,
2020-02-12 08:44:53 +02:00
Name,
Alignment,
NumberingProperty,
IndentLevel,
NumberingId,
Justification,
Insert,
Delete,
DeleteText,
BookmarkStart,
BookmarkEnd,
CommentRangeStart,
CommentRangeEnd,
Table,
TableProperty,
TableRow,
TableCell,
TableCellProperty,
TableCellWidth,
TableCellBorders,
TableVMerge,
TableGridSpan,
TableWidth,
TableIndent,
TableBorders,
Top,
Left,
Bottom,
InsideH,
TableCellMargin,
TableGrid,
GridCol,
2020-02-12 08:44:53 +02:00
Style,
BasedOn,
Next,
VertAlign,
Spacing,
Styles,
Relationship,
Relationships,
Unsupported,
}
impl FromStr for XMLElement {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"body" => Ok(XMLElement::Body),
"p" => Ok(XMLElement::Paragraph),
"r" => Ok(XMLElement::Run),
"rPr" => Ok(XMLElement::RunProperty),
"color" => Ok(XMLElement::Color),
"t" => Ok(XMLElement::Text),
"sz" => Ok(XMLElement::Size),
"szCs" => Ok(XMLElement::SizeCs),
"u" => Ok(XMLElement::Underline),
"pStyle" => Ok(XMLElement::ParagraphStyle),
"highlight" => Ok(XMLElement::Highlight),
"b" => Ok(XMLElement::Bold),
"bCs" => Ok(XMLElement::BoldCs),
"i" => Ok(XMLElement::Italic),
"iCs" => Ok(XMLElement::ItalicCs),
"vanish" => Ok(XMLElement::Vanish),
"italic" => Ok(XMLElement::Italic),
2020-02-12 08:44:53 +02:00
"name" => Ok(XMLElement::Name),
"tab" => Ok(XMLElement::Tab),
"br" => Ok(XMLElement::Break),
"ind" => Ok(XMLElement::Indent),
"numPr" => Ok(XMLElement::NumberingProperty),
"ilvl" => Ok(XMLElement::IndentLevel),
"numId" => Ok(XMLElement::NumberingId),
"jc" => Ok(XMLElement::Justification),
"ins" => Ok(XMLElement::Insert),
"del" => Ok(XMLElement::Delete),
"delText" => Ok(XMLElement::DeleteText),
"bookmarkStart" => Ok(XMLElement::BookmarkStart),
"bookmarkEnd" => Ok(XMLElement::BookmarkEnd),
"commentRangeStart" => Ok(XMLElement::CommentRangeStart),
"commentRangeEnd" => Ok(XMLElement::CommentRangeEnd),
"tbl" => Ok(XMLElement::Table),
"tblPr" => Ok(XMLElement::TableProperty),
"tr" => Ok(XMLElement::TableRow),
"tc" => Ok(XMLElement::TableCell),
"tcPr" => Ok(XMLElement::TableCellProperty),
"tcW" => Ok(XMLElement::TableCellWidth),
"tcBorders" => Ok(XMLElement::TableCellBorders),
"vMerge" => Ok(XMLElement::TableVMerge),
"gridSpan" => Ok(XMLElement::TableGridSpan),
"tblW" => Ok(XMLElement::TableWidth),
"tblInd" => Ok(XMLElement::TableIndent),
"tblBorders" => Ok(XMLElement::TableBorders),
"top" => Ok(XMLElement::Top),
"left" => Ok(XMLElement::Left),
"bottom" => Ok(XMLElement::Bottom),
"insideH" => Ok(XMLElement::InsideH),
"tblCellMar" => Ok(XMLElement::TableCellMargin),
"tblGrid" => Ok(XMLElement::TableGrid),
"gridCol" => Ok(XMLElement::GridCol),
2020-02-12 08:44:53 +02:00
"style" => Ok(XMLElement::Style),
"basedOn" => Ok(XMLElement::BasedOn),
"next" => Ok(XMLElement::Next),
"vertAlign" => Ok(XMLElement::VertAlign),
"spacing" => Ok(XMLElement::Spacing),
"styles" => Ok(XMLElement::Styles),
"Relationships" => Ok(XMLElement::Relationships),
"Relationship" => Ok(XMLElement::Relationship),
_ => Ok(XMLElement::Unsupported),
}
}
}
pub trait ElementReader {
fn read<R: Read>(r: &mut EventReader<R>, attrs: &[OwnedAttribute]) -> Result<Self, ReaderError>
where
Self: std::marker::Sized;
}