use crate::{WString, PCWSTR};
use mssf_com::FabricCommon::{IFabricStringResult, IFabricStringResult_Impl};
use windows_core::implement;
#[derive(Debug)]
#[implement(IFabricStringResult)]
pub struct StringResult {
data: WString,
}
impl StringResult {
pub fn new(data: WString) -> StringResult {
StringResult { data }
}
}
impl IFabricStringResult_Impl for StringResult_Impl {
fn get_String(&self) -> crate::PCWSTR {
crate::PCWSTR::from_raw(self.data.as_ptr())
}
}
fn safe_pwstr_to_wstring(raw: PCWSTR) -> WString {
if raw.is_null() {
return WString::new();
}
WString::from_wide(unsafe { raw.as_wide() })
}
pub struct WStringWrap {
h: WString,
}
impl WStringWrap {
pub fn into_wstring(self) -> WString {
self.h
}
}
impl From<WString> for WStringWrap {
fn from(value: WString) -> Self {
Self { h: value }
}
}
impl From<PCWSTR> for WStringWrap {
fn from(value: PCWSTR) -> Self {
let h = safe_pwstr_to_wstring(value);
Self { h }
}
}
impl From<WStringWrap> for WString {
fn from(val: WStringWrap) -> Self {
val.h
}
}
impl From<&IFabricStringResult> for WStringWrap {
fn from(value: &IFabricStringResult) -> Self {
let content = unsafe { value.get_String() };
let h = safe_pwstr_to_wstring(content);
Self { h }
}
}
impl From<WStringWrap> for IFabricStringResult {
fn from(value: WStringWrap) -> Self {
StringResult::new(value.h).into()
}
}
pub fn get_pcwstr_from_opt(opt: &Option<WString>) -> PCWSTR {
match opt {
Some(x) => PCWSTR(x.as_ptr()),
None => PCWSTR::null(),
}
}
#[cfg(test)]
mod test {
use crate::strings::WStringWrap;
use super::StringResult;
use crate::WString;
use mssf_com::FabricCommon::IFabricStringResult;
#[test]
fn test_str_addr() {
let addr = "1.2.3.4:1234";
let haddr = WString::from(addr);
let haddr_slice = haddr.as_wide();
assert_eq!(haddr_slice.len(), 12);
let com_addr: IFabricStringResult = StringResult::new(haddr.clone()).into();
let raw = unsafe { com_addr.get_String() };
let slice = unsafe { raw.as_wide() };
assert_eq!(slice.len(), 12);
let haddr2: WString = WStringWrap::from(&com_addr).into();
assert_eq!(haddr, haddr2);
}
}