unicode-plot 0.1.0

unicode-plot-rs: Unicode terminal plotting library for Rust
Documentation
/// The eight characters that make up a plot border (corners, sides, top/bottom).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BorderChars {
    /// Top-left corner.
    pub tl: char,
    /// Top edge (repeated for the border width).
    pub t: char,
    /// Top-right corner.
    pub tr: char,
    /// Left edge.
    pub l: char,
    /// Right edge.
    pub r: char,
    /// Bottom-left corner.
    pub bl: char,
    /// Bottom edge (repeated for the border width).
    pub b: char,
    /// Bottom-right corner.
    pub br: char,
}

/// Border style for a plot frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BorderType {
    /// Full box-drawing border: `┌─┐││└─┘`.
    Solid,
    /// Corner-only border with spaces for sides and edges.
    Corners,
    /// Barplot border with a tick mark (`┤`) on the left side.
    Barplot,
    #[doc(hidden)]
    Ascii,
}

impl BorderType {
    /// Returns the eight border characters for this border style.
    #[must_use]
    pub const fn chars(self) -> BorderChars {
        match self {
            Self::Solid => BorderChars {
                tl: '',
                t: '',
                tr: '',
                l: '',
                r: '',
                bl: '',
                b: '',
                br: '',
            },
            Self::Corners => BorderChars {
                tl: '',
                t: ' ',
                tr: '',
                l: ' ',
                r: ' ',
                bl: '',
                b: ' ',
                br: '',
            },
            Self::Barplot => BorderChars {
                tl: '',
                t: ' ',
                tr: '',
                l: '',
                r: ' ',
                bl: '',
                b: ' ',
                br: '',
            },
            Self::Ascii => BorderChars {
                tl: '+',
                t: '-',
                tr: '+',
                l: '|',
                r: '|',
                bl: '+',
                b: '-',
                br: '+',
            },
        }
    }
}

/// Returns the public border types in alphabetical order: Barplot, Corners, Solid.
#[must_use]
pub const fn border_types() -> &'static [BorderType] {
    &[BorderType::Barplot, BorderType::Corners, BorderType::Solid]
}

#[cfg(test)]
mod tests {
    use super::{BorderType, border_types};

    #[test]
    fn solid_border_chars_match_reference() {
        let chars = BorderType::Solid.chars();
        assert_eq!(chars.tl, '');
        assert_eq!(chars.t, '');
        assert_eq!(chars.tr, '');
        assert_eq!(chars.l, '');
        assert_eq!(chars.r, '');
        assert_eq!(chars.bl, '');
        assert_eq!(chars.b, '');
        assert_eq!(chars.br, '');
    }

    #[test]
    fn corners_border_chars_match_reference() {
        let chars = BorderType::Corners.chars();
        assert_eq!(chars.tl, '');
        assert_eq!(chars.t, ' ');
        assert_eq!(chars.tr, '');
        assert_eq!(chars.l, ' ');
        assert_eq!(chars.r, ' ');
        assert_eq!(chars.bl, '');
        assert_eq!(chars.b, ' ');
        assert_eq!(chars.br, '');
    }

    #[test]
    fn barplot_border_chars_match_reference() {
        let chars = BorderType::Barplot.chars();
        assert_eq!(chars.tl, '');
        assert_eq!(chars.t, ' ');
        assert_eq!(chars.tr, '');
        assert_eq!(chars.l, '');
        assert_eq!(chars.r, ' ');
        assert_eq!(chars.bl, '');
        assert_eq!(chars.b, ' ');
        assert_eq!(chars.br, '');
    }

    #[test]
    fn border_types_lists_public_variants() {
        assert_eq!(
            border_types(),
            &[BorderType::Barplot, BorderType::Corners, BorderType::Solid]
        );
    }

    #[test]
    fn ascii_border_chars_match_reference() {
        let chars = BorderType::Ascii.chars();
        assert_eq!(chars.tl, '+');
        assert_eq!(chars.t, '-');
        assert_eq!(chars.tr, '+');
        assert_eq!(chars.l, '|');
        assert_eq!(chars.r, '|');
        assert_eq!(chars.bl, '+');
        assert_eq!(chars.b, '-');
        assert_eq!(chars.br, '+');
    }
}