use std::marker::PhantomData;
use std::ptr::NonNull;
use crate::ffi::hexchat_context;
use crate::str::{HexString, IntoCStr};
#[derive(Debug)]
pub struct Context<S: IntoCStr> {
pub(crate) servname: Option<S>,
pub(crate) channel: Option<S>,
}
impl Context<HexString> {
pub fn focused() -> Self {
Self {
servname: None,
channel: None,
}
}
}
impl<S: IntoCStr> Context<S> {
pub fn channel(channel: S) -> Self {
Self {
servname: None,
channel: Some(channel),
}
}
pub fn frontmost(servname: S) -> Self {
Self {
servname: Some(servname),
channel: None,
}
}
pub fn fully_qualified(servname: S, channel: S) -> Self {
Self {
servname: Some(servname),
channel: Some(channel),
}
}
}
#[derive(Debug, Copy, Clone)]
#[must_use = "context handles do nothing on their own, you must call `with_context` yourself"]
pub struct ContextHandle<'a> {
handle: NonNull<hexchat_context>,
_lifetime: PhantomData<&'a hexchat_context>,
}
impl<'a> ContextHandle<'a> {
pub(crate) unsafe fn new(context_handle: NonNull<hexchat_context>) -> Self {
Self {
handle: context_handle,
_lifetime: PhantomData,
}
}
pub(crate) fn into_raw(self) -> NonNull<hexchat_context> {
self.handle
}
}