1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! `NcAlign`

/// Alignment within a plane or terminal.
///
/// - `Left`|`Right` justified (horizontally).
/// - `Top`|`Down` justified (vertically).
/// - `Centered` (both horizontally & vertically).
/// - `Unaligned` for an invalid state.
///
/// # Default
/// *[`NcAlign::Left`]/[`Top`][NcAlign::Top]*
#[repr(u32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NcAlign {
    /// Anything unaligned wont be rendered.
    Unaligned = c_api::NCALIGN_UNALIGNED,
    /// Left (==[`Top`][NcAlign::Top]) alignment.
    Left = c_api::NCALIGN_LEFT,
    /// Center alignment.
    Center = c_api::NCALIGN_CENTER,
    /// Right (==[`Bottom`][NcAlign::Bottom]) alignment.
    Right = c_api::NCALIGN_RIGHT,
}

mod std_impls {
    use super::{c_api, NcAlign};
    use std::fmt;

    impl Default for NcAlign {
        fn default() -> Self {
            Self::Left
        }
    }

    /// # Aliases
    impl NcAlign {
        /// Top (==[`Left`][NcAlign::Left]) alignment.
        pub const Top: NcAlign = NcAlign::Left;
        /// Bottom (==[`Right`][NcAlign::Right]) alignment.
        pub const Bottom: NcAlign = NcAlign::Right;
    }

    impl fmt::Display for NcAlign {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            use NcAlign::*;
            write!(
                f,
                "{}",
                match self {
                    Left => "Left",
                    Center => "Center",
                    Right => "Right",
                    Unaligned => "Unaligned",
                }
            )
        }
    }

    impl From<c_api::NcAlign_u32> for NcAlign {
        fn from(align: c_api::NcAlign_u32) -> Self {
            use {c_api::*, NcAlign::*};
            match align {
                NCALIGN_LEFT => Left,
                NCALIGN_CENTER => Center,
                NCALIGN_RIGHT => Right,
                NCALIGN_UNALIGNED => Unaligned,
                _ => Unaligned, // invalid values default to `Unaligned`
            }
        }
    }

    impl From<NcAlign> for c_api::NcAlign_u32 {
        fn from(align: NcAlign) -> Self {
            use {c_api::*, NcAlign::*};
            match align {
                Left => NCALIGN_LEFT,
                Center => NCALIGN_CENTER,
                Right => NCALIGN_RIGHT,
                Unaligned => NCALIGN_UNALIGNED,
            }
        }
    }

    impl From<i32> for NcAlign {
        fn from(align: i32) -> Self {
            use {c_api::*, NcAlign::*};

            const NCALIGN_LEFT_i32: i32 = NCALIGN_LEFT as i32;
            const NCALIGN_CENTER_i32: i32 = NCALIGN_CENTER as i32;
            const NCALIGN_RIGHT_i32: i32 = NCALIGN_RIGHT as i32;
            const NCALIGN_UNALIGNED_i32: i32 = NCALIGN_UNALIGNED as i32;

            match align {
                NCALIGN_LEFT_i32 => Left,
                NCALIGN_CENTER_i32 => Center,
                NCALIGN_RIGHT_i32 => Right,
                NCALIGN_UNALIGNED_i32 => Unaligned,
                _ => Unaligned,
            }
        }
    }

    impl From<NcAlign> for i32 {
        fn from(align: NcAlign) -> Self {
            use {c_api::*, NcAlign::*};
            match align {
                Left => NCALIGN_LEFT as i32,
                Center => NCALIGN_CENTER as i32,
                Right => NCALIGN_RIGHT as i32,
                Unaligned => NCALIGN_UNALIGNED as i32,
            }
        }
    }
}

pub(crate) mod c_api {
    use crate::c_api::ffi;

    /// Alignment within an [`NcPlane`][crate::NcPlane] or terminal.
    ///
    /// It's recommended to use [`NcAlign`][crate::NcAlign] instead.
    ///
    /// # Associated `c_api` constants
    ///
    /// - [`NCALIGN_LEFT`]
    /// - [`NCALIGN_RIGHT`]
    /// - [`NCALIGN_TOP`]
    /// - [`NCALIGN_BOTTOM`]
    /// - [`NCALIGN_CENTER`]
    /// - [`NCALIGN_UNALIGNED`]
    pub type NcAlign_u32 = ffi::ncalign_e;

    /// [`NcAlign_u32`] Left alignment.
    pub const NCALIGN_LEFT: NcAlign_u32 = ffi::ncalign_e_NCALIGN_LEFT;

    /// [`NcAlign_u32`] Right alignment.
    pub const NCALIGN_RIGHT: NcAlign_u32 = ffi::ncalign_e_NCALIGN_RIGHT;

    /// [`NcAlign_u32`] Top alignment.
    pub const NCALIGN_TOP: NcAlign_u32 = NCALIGN_LEFT;

    /// [`NcAlign_u32`] Bottom alignment.
    pub const NCALIGN_BOTTOM: NcAlign_u32 = NCALIGN_RIGHT;

    /// [`NcAlign_u32`] Center alignment.
    pub const NCALIGN_CENTER: NcAlign_u32 = ffi::ncalign_e_NCALIGN_CENTER;

    /// [`NcAlign_u32`] Nothing unaligned should be rendered.
    pub const NCALIGN_UNALIGNED: NcAlign_u32 = ffi::ncalign_e_NCALIGN_UNALIGNED;
}