diff options
| author | Philipp A | 2018-11-22 10:29:15 +0100 |
|---|---|---|
| committer | Philipp A | 2018-11-22 10:29:15 +0100 |
| commit | bc5af7498160a9072af5a731f5116ef2db1e30b9 (patch) | |
| tree | 6c0b07effec5eb3ceab76607ecc1bfdffe2051b2 /src/document_tree/element_categories.rs | |
| parent | 088c7fc7d38989dd9e523d073e0a717ea57affd6 (diff) | |
| download | rust-rst-bc5af7498160a9072af5a731f5116ef2db1e30b9.tar.bz2 | |
More into
Diffstat (limited to 'src/document_tree/element_categories.rs')
| -rw-r--r-- | src/document_tree/element_categories.rs | 80 |
1 files changed, 59 insertions, 21 deletions
diff --git a/src/document_tree/element_categories.rs b/src/document_tree/element_categories.rs index 29da801..16a1e3d 100644 --- a/src/document_tree/element_categories.rs +++ b/src/document_tree/element_categories.rs @@ -20,32 +20,49 @@ pub trait HasChildren<C> { } } -macro_rules! synonymous_enum {( $name:ident { $( $entry:ident ),* } ) => ( - #[derive(Serialize)] - pub enum $name { - $( $entry($entry), )* - } - - impl Debug for $name { - fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> { - match *self { - $( $name::$entry(ref inner) => inner.fmt(fmt), )* +macro_rules! synonymous_enum { + ( $name:ident : $super1:ident + $super2:ident { $( $entry:ident ),* $(,)* } ) => { + synonymous_enum!($name: $super1 { $( $entry, )* }); + $( impl Into<$super2> for $entry { + fn into(self) -> $super2 { + $super2::$super1($super1::$name($name::$entry(self))) + } + } )* + }; + ( $name:ident : $super:ident { $( $entry:ident ),* $(,)* } ) => { + synonymous_enum!($name { $( $entry, )* }); + $( impl Into<$super> for $entry { + fn into(self) -> $super { + $super::$name($name::$entry(self)) } + } )* + }; + ( $name:ident { $( $entry:ident ),* $(,)* } ) => { + #[derive(Serialize)] + pub enum $name { + $( $entry($entry), )* } - } - - $( - impl Into<$name> for $entry { + + impl Debug for $name { + fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> { + match *self { + $( $name::$entry(ref inner) => inner.fmt(fmt), )* + } + } + } + + $( impl Into<$name> for $entry { fn into(self) -> $name { $name::$entry(self) } - } - )* -)} + } )* + }; +} -synonymous_enum!(SubStructure { Topic, Sidebar, Transition, Section, BodyElement }); -synonymous_enum!(StructuralSubElement { Title, Subtitle, Decoration, Docinfo, Transition, SubStructure }); -synonymous_enum!(BodyElement { +synonymous_enum!(StructuralSubElement { Title, Subtitle, Decoration, Docinfo, SubStructure }); +synonymous_enum!(SubStructure: StructuralSubElement { Topic, Sidebar, Transition, Section, BodyElement }); +//TODO: also implement into: SubTopic, SubSidebar, SubBlockQuote, SubFootnote, SubFigure +synonymous_enum!(BodyElement: SubStructure + StructuralSubElement { //Simple Paragraph, LiteralBlock, DoctestBlock, MathBlock, Rubric, SubstitutionDefinition, Comment, Pending, Target, Raw, Image, //Compound @@ -66,7 +83,7 @@ synonymous_enum!(TextOrInlineElement { //Content Models\\ //--------------\\ -synonymous_enum!(SubSection { Title, Subtitle, Docinfo, Decoration, SubStructure, BodyElement }); +synonymous_enum!(SubSection { Title, Subtitle, Docinfo, Decoration, SubStructure }); synonymous_enum!(AuthorInfo { Author, Organization, Address, Contact }); synonymous_enum!(DecorationElement { Header, Footer }); synonymous_enum!(SubTopic { Title, BodyElement }); @@ -79,3 +96,24 @@ synonymous_enum!(SubLineBlock { LineBlock, Line }); synonymous_enum!(SubBlockQuote { Attribution, BodyElement }); synonymous_enum!(SubFootnote { Label, BodyElement }); synonymous_enum!(SubFigure { Image, Caption, Legend, BodyElement }); + +#[cfg(test)] +mod test { + use std::default::Default; + use super::*; + + #[test] + fn test_convert_basic() { + let _: BodyElement = Paragraph::default().into(); + } + + #[test] + fn test_convert_more() { + let _: SubStructure = Paragraph::default().into(); + } + + #[test] + fn test_convert_super() { + let _: StructuralSubElement = BodyElement::Paragraph(Paragraph::default()).into(); + } +} |
