1use serde_derive::Serialize;
2
3use crate::attribute_types::{
4 AlignH, AlignHV, AlignV, CanBeEmpty, EnumeratedListType, FixedSpace, FootnoteType, ID, Measure,
5 NameToken, TableAlignH, TableBorder, TableGroupCols,
6};
7use crate::elements as e;
8use crate::url::Url;
9
10pub trait ExtraAttributes<A> {
11 fn with_extra(extra: A) -> Self;
12 fn extra(&self) -> &A;
13 fn extra_mut(&mut self) -> &mut A;
14}
15
16macro_rules! impl_extra {
17 ( $name:ident { $( $(#[$pattr:meta])* $param:ident : $type:ty ),* $(,)* } ) => (
18 impl_extra!(
19 #[derive(Default,Debug,PartialEq,Serialize,Clone)]
20 $name { $( $(#[$pattr])* $param : $type, )* }
21 );
22 );
23 ( $(#[$attr:meta])+ $name:ident { $( $(#[$pattr:meta])* $param:ident : $type:ty ),* $(,)* } ) => (
24 $(#[$attr])+
25 pub struct $name { $(
26 $(#[$pattr])*
27 #[serde(skip_serializing_if = "CanBeEmpty::is_empty")]
28 pub $param : $type,
29 )* }
30 );
31}
32
33impl_extra!(Address { space: FixedSpace });
34impl_extra!(LiteralBlock { space: FixedSpace });
35impl_extra!(DoctestBlock { space: FixedSpace });
36impl_extra!(SubstitutionDefinition {
37 ltrim: bool,
38 rtrim: bool
39});
40impl_extra!(Comment { space: FixedSpace });
41impl_extra!(Target {
42 refuri: Option<Url>,
44 refid: Option<ID>,
46 refname: Vec<NameToken>,
48 anonymous: bool,
49});
50impl_extra!(Raw { space: FixedSpace, format: Vec<NameToken> });
51impl_extra!(#[derive(Debug,PartialEq,Serialize,Clone)] Image {
52 uri: Url,
53 align: Option<AlignHV>,
54 alt: Option<String>,
55 height: Option<Measure>,
56 width: Option<Measure>,
57 scale: Option<u8>,
58 target: Option<Url>, });
60
61impl_extra!(BulletList { bullet: Option<String> });
65impl_extra!(EnumeratedList { enumtype: Option<EnumeratedListType>, prefix: Option<String>, suffix: Option<String> });
66
67impl_extra!(Footnote { backrefs: Vec<ID>, auto: Option<FootnoteType> });
68impl_extra!(Citation { backrefs: Vec<ID> });
69impl_extra!(SystemMessage { backrefs: Vec<ID>, level: Option<usize>, line: Option<usize>, type_: Option<NameToken> });
70impl_extra!(Figure { align: Option<AlignH>, width: Option<usize> });
71impl_extra!(Table { frame: Option<TableBorder>, colsep: Option<bool>, rowsep: Option<bool>, pgwide: Option<bool> });
72
73impl_extra!(TableGroup { cols: TableGroupCols, colsep: Option<bool>, rowsep: Option<bool>, align: Option<TableAlignH> });
74impl_extra!(TableHead { valign: Option<AlignV> });
75impl_extra!(TableBody { valign: Option<AlignV> });
76impl_extra!(TableRow { rowsep: Option<bool>, valign: Option<AlignV> });
77impl_extra!(TableEntry { colname: Option<NameToken>, namest: Option<NameToken>, nameend: Option<NameToken>, morerows: Option<usize>, colsep: Option<bool>, rowsep: Option<bool>, align: Option<TableAlignH>, r#char: Option<char>, charoff: Option<usize>, valign: Option<AlignV>, morecols: Option<usize> });
78impl_extra!(TableColspec { colnum: Option<usize>, colname: Option<NameToken>, colwidth: Option<String>, colsep: Option<bool>, rowsep: Option<bool>, align: Option<TableAlignH>, r#char: Option<char>, charoff: Option<usize>, stub: Option<bool> });
79
80impl_extra!(OptionArgument { delimiter: Option<String> });
81
82impl_extra!(Reference {
83 name: Option<NameToken>, refuri: Option<Url>,
86 refid: Option<ID>,
88 refname: Vec<NameToken>,
90});
91impl_extra!(FootnoteReference { refid: Option<ID>, refname: Vec<NameToken>, auto: Option<FootnoteType> });
92impl_extra!(CitationReference { refid: Option<ID>, refname: Vec<NameToken> });
93impl_extra!(SubstitutionReference { refname: Vec<NameToken> });
94impl_extra!(Problematic { refid: Option<ID> });
95
96impl_extra!(TargetInline {
98 refuri: Option<Url>,
100 refid: Option<ID>,
102 refname: Vec<NameToken>,
104 anonymous: bool,
105});
106impl_extra!(RawInline { space: FixedSpace, format: Vec<NameToken> });
107pub type ImageInline = Image;
108
109pub trait FootnoteTypeExt {
110 fn is_auto(&self) -> bool;
112 fn is_symbol(&self) -> bool;
114 fn footnote_type(&self) -> FootnoteType;
116}
117
118impl FootnoteTypeExt for Option<FootnoteType> {
119 fn is_auto(&self) -> bool {
120 self.is_some()
121 }
122 fn is_symbol(&self) -> bool {
123 matches!(self, Some(FootnoteType::Symbol))
124 }
125 fn footnote_type(&self) -> FootnoteType {
126 self.unwrap_or(FootnoteType::Number)
128 }
129}
130
131impl FootnoteTypeExt for e::Footnote {
132 fn is_auto(&self) -> bool {
133 self.extra().auto.is_auto()
134 }
135 fn is_symbol(&self) -> bool {
136 self.extra().auto.is_symbol()
137 }
138 fn footnote_type(&self) -> FootnoteType {
139 self.extra().auto.footnote_type()
140 }
141}
142
143impl FootnoteTypeExt for e::FootnoteReference {
144 fn is_auto(&self) -> bool {
145 self.extra().auto.is_auto()
146 }
147 fn is_symbol(&self) -> bool {
148 self.extra().auto.is_symbol()
149 }
150 fn footnote_type(&self) -> FootnoteType {
151 self.extra().auto.footnote_type()
152 }
153}
154
155impl Image {
156 #[must_use]
157 pub fn new(uri: Url) -> Image {
158 Image {
159 uri,
160 align: None,
161 alt: None,
162 height: None,
163 width: None,
164 scale: None,
165 target: None,
166 }
167 }
168}