#[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 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 {
let mut string = String::new();
for s in self.to_vec() {
string.push_str(match s {
NcStyle::Italic => "Italic ",
NcStyle::Underline => "Underline ",
NcStyle::Undercurl => "Undercurl ",
NcStyle::Struck => "Struck ",
NcStyle::Bold => "Bold ",
NcStyle::None => "None ",
_ => "",
});
}
let _ = string.pop();
write!(f, "{}", string)
}
}
impl fmt::Debug for NcStyle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut string = String::new();
for s in self.to_vec() {
string.push_str(match s {
NcStyle::Italic => "Italic+",
NcStyle::Underline => "Underline+",
NcStyle::Undercurl => "Undercurl+",
NcStyle::Struck => "Struck+",
NcStyle::Bold => "Bold+",
NcStyle::None => "None ",
_ => "",
});
}
let _ = string.pop();
write!(f, "NcStyle::{}", string)
}
}
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,
];
for s in &styles {
if self.has(*s) {
v.push(*s)
}
}
if v.is_empty() {
v.push(NcStyle::None)
}
v
}
pub fn has(&self, other: impl Into<NcStyle>) -> bool {
let other = other.into();
(self.0 & other.0) == other.0
}
pub fn set(&mut self, other: impl Into<NcStyle>) {
self.0 |= other.into().0
}
pub fn unset(&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;
}