f-office-drawingml 0.3.0

DrawingML shapes, pictures, fills, lines, themes and colors.
Documentation
//! What the readers of DrawingML read in another way than the file asks.

use std::fmt;

use f_office_warnings::Name;

/// What a reader of DrawingML read in another way than the file asks, while it went on with the
/// rest. A warning names the element and the attribute by the names of the schema, or a
/// [`Feature`], never content of the file. The format crate that calls the reader adds the part.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Warning {
    /// A value of an attribute that does not read as its type, such as a line width with a
    /// fraction, or an attribute that the schema requires and that is missing, such as the name
    /// of a preset geometry: the element and the attribute that hold it. The reader reads it as
    /// missing.
    InvalidValue {
        /// The element that has the attribute.
        element: Name,
        /// The attribute.
        attribute: Name,
    },
    /// An element whose children do not read as the schema gives them: it lacks a child that
    /// the schema requires, such as a custom geometry (`a:custGeom`) without its path list, or
    /// holds a child that the schema does not allow there, such as a child of a custom path
    /// (`a:path`) other than its steps. The reader reads the element as missing.
    InvalidChildren {
        /// The element.
        element: Name,
    },
    /// A name of an enumeration that F-Office draws as the case of another name, or as
    /// nothing, such as a preset shape other than the few that it draws: the element, the
    /// attribute and the name as the schema spells it. Its count is the count of the values
    /// that F-Office does not draw.
    ValueNotSupported {
        /// The element that has the attribute.
        element: Name,
        /// The attribute.
        attribute: Name,
        /// The name.
        value: Name,
    },
    /// A feature of DrawingML that F-Office does not draw, which it draws in another way or
    /// leaves out, such as a pattern fill.
    FeatureNotSupported {
        /// The feature.
        feature: Feature,
    },
}

/// A feature of DrawingML that F-Office does not draw, which [`Warning::FeatureNotSupported`]
/// names. Each says what F-Office draws in its place.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Feature {
    /// A picture fill (`a:blipFill`) of an area whose reader does not draw it, such as a
    /// shape of a SmartArt graphic or a chart area: the area has no fill.
    PictureFill,
    /// A pattern fill (`a:pattFill`) of an area: the area has no fill.
    PatternFill,
    /// A group fill (`a:grpFill`) of an area: the area has no fill.
    GroupFill,
    /// A gradient fill (`a:gradFill`) of an area or a line: the color of its first stop.
    GradientFill,
    /// A picture, pattern or group fill of a line (`a:ln`): the color of the line that the
    /// shape's style refers to, or no line.
    LineFill,
    /// A rotation of a shape or a picture that is not a whole number of quarter turns (`rot`
    /// of `a:xfrm`): the shape or the picture is not drawn.
    Turn,
    /// A picture, or a picture fill of a shape, whose outline is not a rectangle: a rounded
    /// rectangle, an ellipse, a line or a custom geometry. The picture fills its whole box.
    ShapedPicture,
    /// A line around a picture (`a:ln` of `pic:spPr` with a fill) that is dashed, compound or
    /// not of one color: no line.
    PictureLine,
    /// A color effect of a picture (a child of `a:blip`) other than its transparency
    /// (`a:alphaModFix`) and gray (`a:grayscl`), such as `a:lum`, `a:duotone`, `a:biLevel` or
    /// `a:clrChange`: the picture keeps its own colors.
    PictureEffect,
    /// A color modifier other than `a:lumMod`, `a:lumOff`, `a:satMod`, `a:shade`, `a:tint`,
    /// `a:gamma`, `a:invGamma` and `a:alpha`, such as `a:hueOff` or `a:gray`, with a value
    /// that changes the color: the color without it.
    ColorModifier,
    /// A color in the HSL or the scRGB model (`a:hslClr`, `a:scrgbClr`): no color.
    ColorModel,
    /// A shadow of a shape or a picture (`a:outerShdw`, `a:innerShdw` or `a:prstShdw` in
    /// `a:effectLst`): no shadow.
    Shadow,
    /// A glow of a shape or a picture (`a:glow`): no glow.
    Glow,
    /// Soft edges of a shape or a picture (`a:softEdge`): hard edges.
    SoftEdge,
    /// A reflection of a shape or a picture (`a:reflection`): no reflection.
    Reflection,
    /// Another effect of a shape or a picture: a blur (`a:blur`) or a fill overlay
    /// (`a:fillOverlay`) in `a:effectLst`, or effects in a graph (`a:effectDag`). It is
    /// left out.
    OtherEffect,
    /// A 3-D look of a shape or a picture: a camera of `a:scene3d` other than the flat view
    /// from the front, or a bevel, an extrusion or a contour of `a:sp3d`. The shape is drawn
    /// flat.
    ThreeD,
    /// A style of the theme's format scheme (`a:fmtScheme`) that a style reference of a
    /// shape's style names (`a:fillRef`, `a:lnRef`, `a:effectRef`) and that the shape takes,
    /// other than a solid fill or a single line of the reference's own color and no effect,
    /// such as a gradient fill, a color modifier of the style, a double line or a shadow: the
    /// shape takes the reference's color, and no effect.
    ThemeStyle,
    /// A dash of a line (`a:prstDash` other than `solid`, or `a:custDash`), its own or of the
    /// line style of the theme that it takes: the line is drawn solid.
    LineDash,
    /// An end of a line whose outline can be open, a line or a custom geometry (`a:headEnd` or
    /// `a:tailEnd` of a `type` other than `none`), such as an arrowhead, its own or of the line
    /// style of the theme that it takes: the end is not drawn.
    LineEnd,
    /// A compound line of a shape's own line (`cmpd` of `a:ln` other than `sng`) on an outline
    /// other than a straight line, such as a rectangle, an ellipse or a custom geometry: the
    /// outline is drawn as one line of the full width.
    CompoundOutline,
}

impl Feature {
    /// Every feature, in the order of their declaration.
    pub const ALL: [Self; 21] = [
        Self::PictureFill,
        Self::PatternFill,
        Self::GroupFill,
        Self::GradientFill,
        Self::LineFill,
        Self::Turn,
        Self::ShapedPicture,
        Self::PictureLine,
        Self::PictureEffect,
        Self::ColorModifier,
        Self::ColorModel,
        Self::Shadow,
        Self::Glow,
        Self::SoftEdge,
        Self::Reflection,
        Self::OtherEffect,
        Self::ThreeD,
        Self::ThemeStyle,
        Self::LineDash,
        Self::LineEnd,
        Self::CompoundOutline,
    ];

    /// The kind of the warning of the feature, as [`Warning::kind`] gives it.
    #[must_use]
    pub fn kind(self) -> &'static str {
        match self {
            Self::PictureFill => "drawingml-picture-fill-not-supported",
            Self::PatternFill => "drawingml-pattern-fill-not-supported",
            Self::GroupFill => "drawingml-group-fill-not-supported",
            Self::GradientFill => "drawingml-gradient-fill-not-supported",
            Self::LineFill => "drawingml-line-fill-not-supported",
            Self::Turn => "drawingml-turn-not-supported",
            Self::ShapedPicture => "drawingml-shaped-picture-not-supported",
            Self::PictureLine => "drawingml-picture-line-not-supported",
            Self::PictureEffect => "drawingml-picture-effect-not-supported",
            Self::ColorModifier => "drawingml-color-modifier-not-supported",
            Self::ColorModel => "drawingml-color-model-not-supported",
            Self::Shadow => "drawingml-shadow-not-supported",
            Self::Glow => "drawingml-glow-not-supported",
            Self::SoftEdge => "drawingml-soft-edge-not-supported",
            Self::Reflection => "drawingml-reflection-not-supported",
            Self::OtherEffect => "drawingml-other-effect-not-supported",
            Self::ThreeD => "drawingml-three-d-not-supported",
            Self::ThemeStyle => "drawingml-theme-style-not-supported",
            Self::LineDash => "drawingml-line-dash-not-supported",
            Self::LineEnd => "drawingml-line-end-not-supported",
            Self::CompoundOutline => "drawingml-compound-outline-not-supported",
        }
    }
}

impl fmt::Display for Feature {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::PictureFill => "a picture fill is not supported here, and the area has no fill",
            Self::PatternFill => "a pattern fill is not supported, and the area has no fill",
            Self::GroupFill => "a group fill is not supported, and the area has no fill",
            Self::GradientFill => {
                "a gradient fill is not supported, and it takes the color of its first stop"
            }
            Self::LineFill => {
                "a picture, pattern or group fill of a line is not supported, and the line takes \
                 the color of the shape's style or is not drawn"
            }
            Self::Turn => {
                "a rotation other than a whole number of quarter turns is not supported, and \
                 the shape or picture is not drawn"
            }
            Self::ShapedPicture => {
                "a picture in an outline other than a rectangle is not supported, and the \
                 picture fills its box"
            }
            Self::PictureLine => {
                "a dashed, compound or shaded line around a picture is not supported, and is not \
                 drawn"
            }
            Self::PictureEffect => {
                "a color effect of a picture other than its transparency or gray is not \
                 supported, and the picture keeps its own colors"
            }
            Self::ColorModifier => {
                "a color modifier other than lumMod, lumOff, shade, tint and alpha is not \
                 supported, and the color is drawn without it"
            }
            Self::ColorModel => {
                "a color in the HSL or scRGB model is not supported, and gives no color"
            }
            Self::Shadow => "a shadow is not supported, and is not drawn",
            Self::Glow => "a glow is not supported, and is not drawn",
            Self::SoftEdge => "a soft edge is not supported, and the edges are drawn hard",
            Self::Reflection => "a reflection is not supported, and is not drawn",
            Self::OtherEffect => {
                "a blur, a fill overlay or a graph of effects is not supported, and is not \
                 drawn"
            }
            Self::ThreeD => "a 3-D look is not supported, and the shape is drawn flat",
            Self::ThemeStyle => {
                "a style of the theme other than a fill or a line of the reference's own color \
                 is not supported, and the shape takes only that color"
            }
            Self::LineDash => "a dash of a line is not supported, and the line is drawn solid",
            Self::LineEnd => {
                "an end of a line, such as an arrowhead, is not supported, and is not drawn"
            }
            Self::CompoundOutline => {
                "a compound line on an outline other than a straight line is not supported, and \
                 the outline is drawn as one line of the full width"
            }
        })
    }
}

impl Warning {
    /// The warning's kind, a short name that stays the same across versions, for a caller
    /// that counts warnings.
    #[must_use]
    pub fn kind(&self) -> &'static str {
        match self {
            Self::InvalidValue { .. } => "drawingml-invalid-value",
            Self::InvalidChildren { .. } => "drawingml-invalid-children",
            Self::ValueNotSupported { .. } => "drawingml-value-not-supported",
            Self::FeatureNotSupported { feature } => feature.kind(),
        }
    }
}

impl fmt::Display for Warning {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidValue { element, attribute } => write!(
                f,
                "the value of attribute {attribute} of element {element} does not read and \
                 counts as missing"
            ),
            Self::InvalidChildren { element } => write!(
                f,
                "the children of element {element} do not read as the schema gives them, and \
                 the element counts as missing"
            ),
            Self::ValueNotSupported {
                element,
                attribute,
                value,
            } => write!(
                f,
                "the value {value} of attribute {attribute} of element {element} is drawn as \
                 another value or not at all"
            ),
            Self::FeatureNotSupported { feature } => feature.fmt(f),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use super::{Feature, Warning};

    /// The place of `feature` in `Feature::ALL`. The match has no wildcard, so a new feature does
    /// not compile until it is named here with the next place, which `Feature::ALL` must hold.
    const fn place_in_all(feature: Feature) -> usize {
        match feature {
            Feature::PictureFill => 0,
            Feature::PatternFill => 1,
            Feature::GroupFill => 2,
            Feature::GradientFill => 3,
            Feature::LineFill => 4,
            Feature::Turn => 5,
            Feature::ShapedPicture => 6,
            Feature::PictureLine => 7,
            Feature::PictureEffect => 8,
            Feature::ColorModifier => 9,
            Feature::ColorModel => 10,
            Feature::Shadow => 11,
            Feature::Glow => 12,
            Feature::SoftEdge => 13,
            Feature::Reflection => 14,
            Feature::OtherEffect => 15,
            Feature::ThreeD => 16,
            Feature::ThemeStyle => 17,
            Feature::LineDash => 18,
            Feature::LineEnd => 19,
            Feature::CompoundOutline => 20,
        }
    }

    #[test]
    fn lists_each_feature_once_in_its_place() {
        for (index, feature) in Feature::ALL.into_iter().enumerate() {
            assert_eq!(place_in_all(feature), index, "{feature:?}");
        }
    }

    #[test]
    fn gives_each_feature_a_kind_of_its_own_and_says_what_is_drawn_in_its_place() {
        let mut kinds = HashSet::new();
        for feature in Feature::ALL {
            let warning = Warning::FeatureNotSupported { feature };
            let kind = warning.kind();
            assert_eq!(kind, feature.kind());
            assert!(
                kind.starts_with("drawingml-") && kind.ends_with("-not-supported"),
                "{kind}"
            );
            assert!(kinds.insert(kind), "{kind}");
            let text = warning.to_string();
            assert_eq!(text, feature.to_string());
            assert!(text.contains(" is not supported"), "{text}");
        }
        assert_eq!(
            Warning::FeatureNotSupported {
                feature: Feature::PatternFill
            }
            .to_string(),
            "a pattern fill is not supported, and the area has no fill"
        );
    }
}