use std::ffi::CStr;
use std::fmt::{self, Debug, Display, Formatter};
use std::ops::Deref;
use std::ptr::NonNull;
use std::str::Utf8Error;
use crate::ffi::RawPluginHandle;
use crate::str::HexStr;
#[non_exhaustive]
#[derive(Debug, Copy, Clone)]
pub enum MircColors {
Keep,
Remove,
}
#[non_exhaustive]
#[derive(Debug, Copy, Clone)]
pub enum TextAttrs {
Keep,
Remove,
}
pub struct StrippedStr<'a> {
raw: RawPluginHandle<'a>,
stripped_ptr: NonNull<HexStr>,
}
impl<'a> StrippedStr<'a> {
pub(crate) unsafe fn new(
raw: RawPluginHandle<'a>,
stripped_ptr: &CStr,
) -> Result<Self, Utf8Error> {
let stripped_ptr = HexStr::from_cstr(stripped_ptr)?;
Ok(Self {
raw,
stripped_ptr: NonNull::from(stripped_ptr),
})
}
}
impl Drop for StrippedStr<'_> {
fn drop(&mut self) {
unsafe {
self.raw.hexchat_free(self.stripped_ptr.as_ptr().cast());
}
}
}
impl Deref for StrippedStr<'_> {
type Target = HexStr;
fn deref(&self) -> &Self::Target {
unsafe { self.stripped_ptr.as_ref() }
}
}
impl Display for StrippedStr<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(self.deref(), f)
}
}
impl Debug for StrippedStr<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(self.deref(), f)
}
}