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
151
152
153
154
155
156
157
158
159
160
161
162
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct NcStyle(pub c_api::NcStyle_u16);
impl NcStyle {
pub const Bold: Self = Self(c_api::NCSTYLE_BOLD);
pub const Italic: Self = Self(c_api::NCSTYLE_ITALIC);
pub const Struck: Self = Self(c_api::NCSTYLE_STRUCK);
pub const Underline: Self = Self(c_api::NCSTYLE_UNDERLINE);
pub const Undercurl: Self = Self(c_api::NCSTYLE_UNDERCURL);
pub const None: Self = Self(0);
pub const Mask: Self = Self(c_api::NCSTYLE_MASK);
}
mod std_impls {
use super::{c_api::NcStyle_u16, NcStyle};
use crate::Nc;
use std::fmt;
impl Default for NcStyle {
fn default() -> Self {
Self::None
}
}
impl fmt::Display for NcStyle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", Nc::str_styles(*self))
}
}
impl fmt::Debug for NcStyle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "NcStyle::{}", self)
}
}
crate::from_primitive![NcStyle, NcStyle_u16];
crate::unit_impl_from![NcStyle, NcStyle_u16];
impl From<NcStyle> for u32 {
fn from(style: NcStyle) -> Self {
style.0 as u32
}
}
crate::unit_impl_ops![bitwise; NcStyle, NcStyle_u16];
crate::unit_impl_fmt![bases; NcStyle];
}
impl NcStyle {
pub fn to_vec(&self) -> Vec<NcStyle> {
let mut v = vec![];
let styles = [
NcStyle::Italic,
NcStyle::Underline,
NcStyle::Undercurl,
NcStyle::Struck,
NcStyle::Bold,
NcStyle::None,
];
for s in &styles {
if self.has(*s) {
v.push(*s)
}
}
v
}
pub fn has(&self, other: impl Into<NcStyle>) -> bool {
let other = other.into();
(self.0 & other.0) == other.0
}
pub fn add(&mut self, other: impl Into<NcStyle>) {
self.0 |= other.into().0
}
}
pub(crate) mod c_api {
use crate::c_api::ffi;
pub type NcStyle_u16 = u16;
pub const NCSTYLE_ITALIC: u16 = ffi::NCSTYLE_ITALIC as u16;
pub const NCSTYLE_UNDERLINE: u16 = ffi::NCSTYLE_UNDERLINE as u16;
pub const NCSTYLE_UNDERCURL: u16 = ffi::NCSTYLE_UNDERCURL as u16;
pub const NCSTYLE_STRUCK: u16 = ffi::NCSTYLE_STRUCK as u16;
pub const NCSTYLE_BOLD: u16 = ffi::NCSTYLE_BOLD as u16;
pub const NCSTYLE_NONE: u16 = ffi::NCSTYLE_NONE as u16;
pub const NCSTYLE_MASK: u16 = ffi::NCSTYLE_MASK as u16;
}