GitCorner

Code

ossartcraftfigcraft🌐
b45255bb6fe2
//! What the export changed or left out: one entry per kind, naming the first few layers.

/// The kinds of change, in the order they're reported.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Warn {
    DropShadow,
    InnerShadow,
    LayerBlur,
    BackgroundBlur,
    Noise,
    Texture,
    Glass,
    Blend,
    ImageFill,
    PatternFill,
    DiamondApprox,
    DiamondOmitted,
    OpacityApprox,
    OpacityOmitted,
    EvenOddRewritten,
    EvenOddKept,
    ClipEvenOddRewritten,
    ClipEvenOddApprox,
    ClipEvenOddOmitted,
    MaskApprox,
    MaskOmitted,
    TooLarge,
    NothingDrawn,
}

impl Warn {
    fn message(self) -> &'static str {
        match self {
            Warn::DropShadow => "drop shadows were left out",
            Warn::InnerShadow => "inner shadows were left out",
            Warn::LayerBlur => "layer blurs were left out",
            Warn::BackgroundBlur => "background blurs were left out",
            Warn::Noise => "noise effects were left out",
            Warn::Texture => "texture effects were left out",
            Warn::Glass => "glass effects were left out",
            Warn::Blend => "blend modes were drawn as normal",
            Warn::ImageFill => "image fills were left out",
            Warn::PatternFill => "pattern fills were left out",
            Warn::DiamondApprox => "diamond gradients were drawn as radial gradients",
            Warn::DiamondOmitted => "diamond gradients were left out (exact mode)",
            Warn::OpacityApprox => "layer opacity was applied to each shape: where shapes overlap they look denser",
            Warn::OpacityOmitted => "layers with opacity over overlapping shapes were left out (exact mode)",
            Warn::EvenOddRewritten => "even-odd fills were rewritten as non-zero",
            Warn::EvenOddKept => "even-odd fills couldn't be rewritten and use fillType=\"evenOdd\" (API 24+ or AndroidX)",
            Warn::ClipEvenOddRewritten => "even-odd clip outlines were rewritten as non-zero",
            Warn::ClipEvenOddApprox => "even-odd clip outlines couldn't be rewritten and clip as non-zero",
            Warn::ClipEvenOddOmitted => "layers under even-odd clip outlines that couldn't be rewritten were left out (exact mode)",
            Warn::MaskApprox => "alpha and luminance masks clip to the mask's outline",
            Warn::MaskOmitted => "layers under alpha or luminance masks were left out (exact mode)",
            Warn::TooLarge => "shapes with coordinates beyond ±10,000,000 were left out",
            Warn::NothingDrawn => "nothing in it could be drawn: the drawable is empty",
        }
    }
}

/// Layers named per warning.
const NAMED: usize = 3;

#[derive(Default)]
pub(crate) struct Warnings {
    /// Per warning: the distinct names so far (a set, so a document with many distinct names
    /// doesn't make each check a scan of all before it), and the first few in order.
    seen: std::collections::BTreeMap<Warn, (std::collections::BTreeSet<String>, Vec<String>)>,
}

impl Warnings {
    pub fn add(&mut self, w: Warn, layer: &str) {
        let (distinct, first) = self.seen.entry(w).or_default();
        if distinct.insert(layer.to_string()) && first.len() < NAMED {
            first.push(layer.to_string());
        }
    }

    pub fn finish(self) -> Vec<String> {
        self.seen
            .into_iter()
            .map(|(w, (distinct, names))| {
                let shown: Vec<String> =
                    names.iter().take(NAMED).map(|n| if n.is_empty() { "unnamed layer".to_string() } else { n.clone() }).collect();
                let more = distinct.len().saturating_sub(NAMED);
                let more = if more > 0 { format!(", {more} more") } else { String::new() };
                format!("{} ({}{more})", w.message(), shown.join(", "))
            })
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn names_are_counted_once_each_and_the_first_three_shown() {
        let mut w = Warnings::default();
        for name in ["a", "b", "a", "c", "d", "b", "e"] {
            w.add(Warn::NothingDrawn, name);
        }
        let lines = w.finish();
        assert_eq!(lines.len(), 1);
        assert!(lines[0].ends_with("(a, b, c, 2 more)"), "{}", lines[0]);
        // Many distinct names: each is one set insertion, not a scan of all before it.
        let mut w = Warnings::default();
        let start = std::time::Instant::now();
        for i in 0..200_000 {
            w.add(Warn::TooLarge, &format!("layer {i}"));
        }
        assert!(w.finish()[0].ends_with("199997 more)"));
        assert!(start.elapsed() < std::time::Duration::from_secs(5), "{:?}", start.elapsed());
    }
}