use std::ptr::null_mut;
use winapi::um::oleauto::{SysAllocStringLen, SysFreeString, SysStringLen};
use winapi::shared::wtypes::BSTR;
pub(crate) use widestring::U16String;
use super::errors::{BStringError, ElementError, FromVariantError, IntoSafeArrayError, IntoSafeArrElemError, IntoVariantError, SafeArrayError};
use super::ptr::Ptr;
use super::types::TryConvert;
pub trait BStringExt {
fn allocate_bstr(&mut self) -> Result<Ptr<u16>, BStringError>;
fn consume_to_bstr(self) -> Result<Ptr<u16>, BStringError>;
fn allocate_managed_bstr(&mut self) -> Result<DroppableBString, BStringError>;
fn consume_to_managed_bstr(self) -> Result<DroppableBString, BStringError>;
fn deallocate_bstr(bstr: Ptr<u16>);
fn from_bstr(bstr: *mut u16) -> U16String;
fn from_pbstr(bstr: Ptr<u16>) -> U16String;
fn from_boxed_bstr(bstr: Box<u16>) -> U16String;
}
impl BStringExt for U16String {
fn allocate_bstr(&mut self) -> Result<Ptr<u16>, BStringError> {
let sz = self.len();
let rw = self.as_ptr();
let bstr: BSTR = unsafe {SysAllocStringLen(rw, sz as u32)};
match Ptr::with_checked(bstr) {
Some(pbstr) => Ok(pbstr),
None => Err(BStringError::AllocateFailed{len: sz})
}
}
fn consume_to_bstr(self) -> Result<Ptr<u16>, BStringError> {
let sz = self.len();
let rw = self.as_ptr();
let bstr: BSTR = unsafe {SysAllocStringLen(rw, sz as u32)};
match Ptr::with_checked(bstr) {
Some(pbstr) => Ok(pbstr),
None => Err(BStringError::AllocateFailed{len: sz})
}
}
fn allocate_managed_bstr(&mut self) -> Result<DroppableBString, BStringError> {
Ok(DroppableBString{ inner: Some(self.allocate_bstr()?) })
}
fn consume_to_managed_bstr(self) -> Result<DroppableBString, BStringError> {
Ok(DroppableBString{ inner: Some(self.consume_to_bstr()?) })
}
fn deallocate_bstr(bstr: Ptr<u16>) {
let bstr: BSTR = bstr.as_ptr();
unsafe { SysFreeString(bstr) }
}
fn from_bstr(bstr: *mut u16) -> U16String {
assert!(!bstr.is_null());
let sz = unsafe {SysStringLen(bstr)};
unsafe {U16String::from_ptr(bstr, sz as usize)}
}
fn from_pbstr(bstr: Ptr<u16>) -> U16String {
U16String::from_bstr(bstr.as_ptr())
}
fn from_boxed_bstr(bstr: Box<u16>) -> U16String {
U16String::from_bstr(Box::into_raw(bstr))
}
}
#[derive( Debug, Eq, Hash, PartialEq, PartialOrd)]
pub struct DroppableBString {
inner: Option<Ptr<u16>>
}
impl DroppableBString {
pub unsafe fn consume(&mut self) -> *mut u16 {
let ret = match self.inner {
Some(ptr) => ptr.as_ptr(),
None => null_mut()
};
self.inner = None;
ret
}
}
impl Drop for DroppableBString {
fn drop(&mut self) {
match self.inner {
Some(ptr) => {
unsafe { SysFreeString(ptr.as_ptr())}
},
None => {}
}
}
}
impl TryConvert<U16String, IntoVariantError> for BSTR {
fn try_convert(u: U16String) -> Result<Self, IntoVariantError> {
Ok(u.clone().allocate_bstr()?.as_ptr())
}
}
impl TryConvert<BSTR, FromVariantError> for U16String {
fn try_convert(p: BSTR) -> Result<Self, FromVariantError> {
assert!(!p.is_null(), "BSTR ptr was null.");
Ok(U16String::from_bstr(p))
}
}
impl TryConvert<U16String, SafeArrayError> for BSTR {
fn try_convert(u: U16String) -> Result<Self, SafeArrayError> {
match u.clone().allocate_bstr() {
Ok(ptr) => Ok(ptr.as_ptr()),
Err(bse) => Err(SafeArrayError::from(IntoSafeArrayError::from_element_err(IntoSafeArrElemError::from(bse), 0)))
}
}
}
impl TryConvert<U16String, ElementError> for BSTR {
fn try_convert(u: U16String) -> Result<Self,ElementError> {
match u.clone().allocate_bstr() {
Ok(ptr) => Ok(ptr.as_ptr()),
Err(bse) => Err(ElementError::from(IntoSafeArrElemError::from(bse)))
}
}
}
impl TryConvert<BSTR, ElementError> for U16String {
fn try_convert(ptr: BSTR) -> Result<Self, ElementError> {
assert!(!ptr.is_null(), "BSTR ptr was null.");
Ok(U16String::from_bstr(ptr))
}
}