f-office-units 0.2.4

Integer lengths and unit conversions for F-Office.
Documentation
//! The four edges of a box, by name, so that a value of one edge cannot take the place of
//! another, as a bare array of four values can. A box of a place and a size is edges too, made
//! from its corner and its size, so that the crates have one type for a box.

use crate::Length;

/// One value for each edge of a box: the distances that keep text away from a drawing, the
/// insets of a text box, the shares of an image that a crop cuts off, or the edges of a box on
/// the page or in its drawing, such as the box of a DrawingML shape.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Edges<T> {
    /// The left edge.
    pub left: T,
    /// The top edge.
    pub top: T,
    /// The right edge.
    pub right: T,
    /// The bottom edge.
    pub bottom: T,
}

impl<T> Edges<T> {
    /// Each edge's value through `f`.
    pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> Edges<U> {
        Edges {
            left: f(self.left),
            top: f(self.top),
            right: f(self.right),
            bottom: f(self.bottom),
        }
    }

    /// Each edge's value with the value of the same edge of `other`, through `f`.
    pub fn with<U, V>(self, other: Edges<U>, mut f: impl FnMut(T, U) -> V) -> Edges<V> {
        Edges {
            left: f(self.left, other.left),
            top: f(self.top, other.top),
            right: f(self.right, other.right),
            bottom: f(self.bottom, other.bottom),
        }
    }
}

impl Edges<Length> {
    /// The box whose top left corner is at `x` and `y` and whose size is `width` by `height`,
    /// or `None` when its right or bottom edge lies beyond the range of a length.
    #[must_use]
    pub fn placed(x: Length, y: Length, width: Length, height: Length) -> Option<Self> {
        Some(Self {
            left: x,
            top: y,
            right: x.checked_add(width)?,
            bottom: y.checked_add(height)?,
        })
    }

    /// The box of `width` by `height` whose top left corner is at the origin, such as a
    /// picture that fills its drawing's box.
    #[must_use]
    pub const fn sized(width: Length, height: Length) -> Self {
        Self {
            left: Length::ZERO,
            top: Length::ZERO,
            right: width,
            bottom: height,
        }
    }

    /// The width of the box, from its left edge to its right edge.
    #[must_use]
    pub fn width(self) -> Length {
        self.right - self.left
    }

    /// The height of the box, from its top edge to its bottom edge.
    #[must_use]
    pub fn height(self) -> Length {
        self.bottom - self.top
    }
}

impl<T> From<[T; 4]> for Edges<T> {
    /// The edges of four values in the order left, top, right, bottom, the order of the
    /// attributes `l`, `t`, `r` and `b` of DrawingML.
    fn from([left, top, right, bottom]: [T; 4]) -> Self {
        Self {
            left,
            top,
            right,
            bottom,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Edges;
    use crate::Length;

    #[test]
    fn makes_a_box_from_its_corner_and_its_size() {
        let units = Length::from_units;
        let placed = Edges::placed(units(10), units(-20), units(30), units(40)).unwrap();
        assert_eq!(placed, Edges::from([10, -20, 40, 20].map(units)));
        assert_eq!((placed.width(), placed.height()), (units(30), units(40)));
        let sized = Edges::sized(units(30), units(40));
        assert_eq!(sized, Edges::from([0, 0, 30, 40].map(units)));
        assert_eq!((sized.width(), sized.height()), (units(30), units(40)));
        // A right or bottom edge beyond the range of a length gives no box.
        let far = units(i64::MAX - 5);
        assert_eq!(Edges::placed(far, units(0), units(10), units(1)), None);
        assert_eq!(Edges::placed(units(0), far, units(1), units(10)), None);
    }

    #[test]
    fn keeps_each_value_on_its_edge() {
        let edges = Edges::from([1, 2, 3, 4]);
        assert_eq!(
            (edges.left, edges.top, edges.right, edges.bottom),
            (1, 2, 3, 4)
        );
        assert_eq!(edges.map(|value| value * 10), Edges::from([10, 20, 30, 40]));
        assert_eq!(
            edges.with(Edges::from([100; 4]), |a, b| a + b),
            Edges::from([101, 102, 103, 104])
        );
    }
}