Skip to main content

f_office_drawingml/
warning.rs

1//! What the readers of DrawingML read in another way than the file asks.
2
3use std::fmt;
4
5use f_office_warnings::Name;
6
7/// What a reader of DrawingML read in another way than the file asks, while it went on with the
8/// rest. A warning names the element and the attribute by the names of the schema, or a
9/// [`Feature`], never content of the file. The format crate that calls the reader adds the part.
10#[derive(Clone, Debug, PartialEq, Eq, Hash)]
11#[non_exhaustive]
12pub enum Warning {
13    /// A value of an attribute that does not read as its type, such as a line width with a
14    /// fraction, or an attribute that the schema requires and that is missing, such as the name
15    /// of a preset geometry: the element and the attribute that hold it. The reader reads it as
16    /// missing.
17    InvalidValue {
18        /// The element that has the attribute.
19        element: Name,
20        /// The attribute.
21        attribute: Name,
22    },
23    /// An element whose children do not read as the schema gives them: it lacks a child that
24    /// the schema requires, such as a custom geometry (`a:custGeom`) without its path list, or
25    /// holds a child that the schema does not allow there, such as a child of a custom path
26    /// (`a:path`) other than its steps. The reader reads the element as missing.
27    InvalidChildren {
28        /// The element.
29        element: Name,
30    },
31    /// A name of an enumeration that F-Office draws as the case of another name, or as
32    /// nothing, such as a preset shape other than the few that it draws: the element, the
33    /// attribute and the name as the schema spells it. Its count is the count of the values
34    /// that F-Office does not draw.
35    ValueNotSupported {
36        /// The element that has the attribute.
37        element: Name,
38        /// The attribute.
39        attribute: Name,
40        /// The name.
41        value: Name,
42    },
43    /// A feature of DrawingML that F-Office does not draw, which it draws in another way or
44    /// leaves out, such as a pattern fill.
45    FeatureNotSupported {
46        /// The feature.
47        feature: Feature,
48    },
49}
50
51/// A feature of DrawingML that F-Office does not draw, which [`Warning::FeatureNotSupported`]
52/// names. Each says what F-Office draws in its place.
53#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
54#[non_exhaustive]
55pub enum Feature {
56    /// A picture fill (`a:blipFill`) of an area whose reader does not draw it, such as a
57    /// shape of a SmartArt graphic or a chart area: the area has no fill.
58    PictureFill,
59    /// A pattern fill (`a:pattFill`) of an area: the area has no fill.
60    PatternFill,
61    /// A group fill (`a:grpFill`) of an area: the area has no fill.
62    GroupFill,
63    /// A gradient fill (`a:gradFill`) of an area or a line: the color of its first stop.
64    GradientFill,
65    /// A picture, pattern or group fill of a line (`a:ln`): the color of the line that the
66    /// shape's style refers to, or no line.
67    LineFill,
68    /// A rotation of a shape or a picture that is not a whole number of quarter turns (`rot`
69    /// of `a:xfrm`): the shape or the picture is not drawn.
70    Turn,
71    /// A picture, or a picture fill of a shape, whose outline is not a rectangle: a rounded
72    /// rectangle, an ellipse, a line or a custom geometry. The picture fills its whole box.
73    ShapedPicture,
74    /// A line around a picture (`a:ln` of `pic:spPr` with a fill) that is dashed, compound or
75    /// not of one color: no line.
76    PictureLine,
77    /// A color effect of a picture (a child of `a:blip`) other than its transparency
78    /// (`a:alphaModFix`) and gray (`a:grayscl`), such as `a:lum`, `a:duotone`, `a:biLevel` or
79    /// `a:clrChange`: the picture keeps its own colors.
80    PictureEffect,
81    /// A color modifier other than `a:lumMod`, `a:lumOff`, `a:satMod`, `a:shade`, `a:tint`,
82    /// `a:gamma`, `a:invGamma` and `a:alpha`, such as `a:hueOff` or `a:gray`, with a value
83    /// that changes the color: the color without it.
84    ColorModifier,
85    /// A color in the HSL or the scRGB model (`a:hslClr`, `a:scrgbClr`): no color.
86    ColorModel,
87    /// A shadow of a shape or a picture (`a:outerShdw`, `a:innerShdw` or `a:prstShdw` in
88    /// `a:effectLst`): no shadow.
89    Shadow,
90    /// A glow of a shape or a picture (`a:glow`): no glow.
91    Glow,
92    /// Soft edges of a shape or a picture (`a:softEdge`): hard edges.
93    SoftEdge,
94    /// A reflection of a shape or a picture (`a:reflection`): no reflection.
95    Reflection,
96    /// Another effect of a shape or a picture: a blur (`a:blur`) or a fill overlay
97    /// (`a:fillOverlay`) in `a:effectLst`, or effects in a graph (`a:effectDag`). It is
98    /// left out.
99    OtherEffect,
100    /// A 3-D look of a shape or a picture: a camera of `a:scene3d` other than the flat view
101    /// from the front, or a bevel, an extrusion or a contour of `a:sp3d`. The shape is drawn
102    /// flat.
103    ThreeD,
104    /// A style of the theme's format scheme (`a:fmtScheme`) that a style reference of a
105    /// shape's style names (`a:fillRef`, `a:lnRef`, `a:effectRef`) and that the shape takes,
106    /// other than a solid fill or a single line of the reference's own color and no effect,
107    /// such as a gradient fill, a color modifier of the style, a double line or a shadow: the
108    /// shape takes the reference's color, and no effect.
109    ThemeStyle,
110    /// A dash of a line (`a:prstDash` other than `solid`, or `a:custDash`), its own or of the
111    /// line style of the theme that it takes: the line is drawn solid.
112    LineDash,
113    /// An end of a line whose outline can be open, a line or a custom geometry (`a:headEnd` or
114    /// `a:tailEnd` of a `type` other than `none`), such as an arrowhead, its own or of the line
115    /// style of the theme that it takes: the end is not drawn.
116    LineEnd,
117    /// A compound line of a shape's own line (`cmpd` of `a:ln` other than `sng`) on an outline
118    /// other than a straight line, such as a rectangle, an ellipse or a custom geometry: the
119    /// outline is drawn as one line of the full width.
120    CompoundOutline,
121}
122
123impl Feature {
124    /// Every feature, in the order of their declaration.
125    pub const ALL: [Self; 21] = [
126        Self::PictureFill,
127        Self::PatternFill,
128        Self::GroupFill,
129        Self::GradientFill,
130        Self::LineFill,
131        Self::Turn,
132        Self::ShapedPicture,
133        Self::PictureLine,
134        Self::PictureEffect,
135        Self::ColorModifier,
136        Self::ColorModel,
137        Self::Shadow,
138        Self::Glow,
139        Self::SoftEdge,
140        Self::Reflection,
141        Self::OtherEffect,
142        Self::ThreeD,
143        Self::ThemeStyle,
144        Self::LineDash,
145        Self::LineEnd,
146        Self::CompoundOutline,
147    ];
148
149    /// The kind of the warning of the feature, as [`Warning::kind`] gives it.
150    #[must_use]
151    pub fn kind(self) -> &'static str {
152        match self {
153            Self::PictureFill => "drawingml-picture-fill-not-supported",
154            Self::PatternFill => "drawingml-pattern-fill-not-supported",
155            Self::GroupFill => "drawingml-group-fill-not-supported",
156            Self::GradientFill => "drawingml-gradient-fill-not-supported",
157            Self::LineFill => "drawingml-line-fill-not-supported",
158            Self::Turn => "drawingml-turn-not-supported",
159            Self::ShapedPicture => "drawingml-shaped-picture-not-supported",
160            Self::PictureLine => "drawingml-picture-line-not-supported",
161            Self::PictureEffect => "drawingml-picture-effect-not-supported",
162            Self::ColorModifier => "drawingml-color-modifier-not-supported",
163            Self::ColorModel => "drawingml-color-model-not-supported",
164            Self::Shadow => "drawingml-shadow-not-supported",
165            Self::Glow => "drawingml-glow-not-supported",
166            Self::SoftEdge => "drawingml-soft-edge-not-supported",
167            Self::Reflection => "drawingml-reflection-not-supported",
168            Self::OtherEffect => "drawingml-other-effect-not-supported",
169            Self::ThreeD => "drawingml-three-d-not-supported",
170            Self::ThemeStyle => "drawingml-theme-style-not-supported",
171            Self::LineDash => "drawingml-line-dash-not-supported",
172            Self::LineEnd => "drawingml-line-end-not-supported",
173            Self::CompoundOutline => "drawingml-compound-outline-not-supported",
174        }
175    }
176}
177
178impl fmt::Display for Feature {
179    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180        f.write_str(match self {
181            Self::PictureFill => "a picture fill is not supported here, and the area has no fill",
182            Self::PatternFill => "a pattern fill is not supported, and the area has no fill",
183            Self::GroupFill => "a group fill is not supported, and the area has no fill",
184            Self::GradientFill => {
185                "a gradient fill is not supported, and it takes the color of its first stop"
186            }
187            Self::LineFill => {
188                "a picture, pattern or group fill of a line is not supported, and the line takes \
189                 the color of the shape's style or is not drawn"
190            }
191            Self::Turn => {
192                "a rotation other than a whole number of quarter turns is not supported, and \
193                 the shape or picture is not drawn"
194            }
195            Self::ShapedPicture => {
196                "a picture in an outline other than a rectangle is not supported, and the \
197                 picture fills its box"
198            }
199            Self::PictureLine => {
200                "a dashed, compound or shaded line around a picture is not supported, and is not \
201                 drawn"
202            }
203            Self::PictureEffect => {
204                "a color effect of a picture other than its transparency or gray is not \
205                 supported, and the picture keeps its own colors"
206            }
207            Self::ColorModifier => {
208                "a color modifier other than lumMod, lumOff, shade, tint and alpha is not \
209                 supported, and the color is drawn without it"
210            }
211            Self::ColorModel => {
212                "a color in the HSL or scRGB model is not supported, and gives no color"
213            }
214            Self::Shadow => "a shadow is not supported, and is not drawn",
215            Self::Glow => "a glow is not supported, and is not drawn",
216            Self::SoftEdge => "a soft edge is not supported, and the edges are drawn hard",
217            Self::Reflection => "a reflection is not supported, and is not drawn",
218            Self::OtherEffect => {
219                "a blur, a fill overlay or a graph of effects is not supported, and is not \
220                 drawn"
221            }
222            Self::ThreeD => "a 3-D look is not supported, and the shape is drawn flat",
223            Self::ThemeStyle => {
224                "a style of the theme other than a fill or a line of the reference's own color \
225                 is not supported, and the shape takes only that color"
226            }
227            Self::LineDash => "a dash of a line is not supported, and the line is drawn solid",
228            Self::LineEnd => {
229                "an end of a line, such as an arrowhead, is not supported, and is not drawn"
230            }
231            Self::CompoundOutline => {
232                "a compound line on an outline other than a straight line is not supported, and \
233                 the outline is drawn as one line of the full width"
234            }
235        })
236    }
237}
238
239impl Warning {
240    /// The warning's kind, a short name that stays the same across versions, for a caller
241    /// that counts warnings.
242    #[must_use]
243    pub fn kind(&self) -> &'static str {
244        match self {
245            Self::InvalidValue { .. } => "drawingml-invalid-value",
246            Self::InvalidChildren { .. } => "drawingml-invalid-children",
247            Self::ValueNotSupported { .. } => "drawingml-value-not-supported",
248            Self::FeatureNotSupported { feature } => feature.kind(),
249        }
250    }
251}
252
253impl fmt::Display for Warning {
254    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
255        match self {
256            Self::InvalidValue { element, attribute } => write!(
257                f,
258                "the value of attribute {attribute} of element {element} does not read and \
259                 counts as missing"
260            ),
261            Self::InvalidChildren { element } => write!(
262                f,
263                "the children of element {element} do not read as the schema gives them, and \
264                 the element counts as missing"
265            ),
266            Self::ValueNotSupported {
267                element,
268                attribute,
269                value,
270            } => write!(
271                f,
272                "the value {value} of attribute {attribute} of element {element} is drawn as \
273                 another value or not at all"
274            ),
275            Self::FeatureNotSupported { feature } => feature.fmt(f),
276        }
277    }
278}
279
280#[cfg(test)]
281mod tests {
282    use std::collections::HashSet;
283
284    use super::{Feature, Warning};
285
286    /// The place of `feature` in `Feature::ALL`. The match has no wildcard, so a new feature does
287    /// not compile until it is named here with the next place, which `Feature::ALL` must hold.
288    const fn place_in_all(feature: Feature) -> usize {
289        match feature {
290            Feature::PictureFill => 0,
291            Feature::PatternFill => 1,
292            Feature::GroupFill => 2,
293            Feature::GradientFill => 3,
294            Feature::LineFill => 4,
295            Feature::Turn => 5,
296            Feature::ShapedPicture => 6,
297            Feature::PictureLine => 7,
298            Feature::PictureEffect => 8,
299            Feature::ColorModifier => 9,
300            Feature::ColorModel => 10,
301            Feature::Shadow => 11,
302            Feature::Glow => 12,
303            Feature::SoftEdge => 13,
304            Feature::Reflection => 14,
305            Feature::OtherEffect => 15,
306            Feature::ThreeD => 16,
307            Feature::ThemeStyle => 17,
308            Feature::LineDash => 18,
309            Feature::LineEnd => 19,
310            Feature::CompoundOutline => 20,
311        }
312    }
313
314    #[test]
315    fn lists_each_feature_once_in_its_place() {
316        for (index, feature) in Feature::ALL.into_iter().enumerate() {
317            assert_eq!(place_in_all(feature), index, "{feature:?}");
318        }
319    }
320
321    #[test]
322    fn gives_each_feature_a_kind_of_its_own_and_says_what_is_drawn_in_its_place() {
323        let mut kinds = HashSet::new();
324        for feature in Feature::ALL {
325            let warning = Warning::FeatureNotSupported { feature };
326            let kind = warning.kind();
327            assert_eq!(kind, feature.kind());
328            assert!(
329                kind.starts_with("drawingml-") && kind.ends_with("-not-supported"),
330                "{kind}"
331            );
332            assert!(kinds.insert(kind), "{kind}");
333            let text = warning.to_string();
334            assert_eq!(text, feature.to_string());
335            assert!(text.contains(" is not supported"), "{text}");
336        }
337        assert_eq!(
338            Warning::FeatureNotSupported {
339                feature: Feature::PatternFill
340            }
341            .to_string(),
342            "a pattern fill is not supported, and the area has no fill"
343        );
344    }
345}