wdext 0.1.0

A DbgEng wrapper framework
// SPDX-FileCopyrightText: 2026 takubokudori
// SPDX-License-Identifier: MIT OR Apache-2.0
use crate::{dbgeng::WinResult, dbgmodel::ModelObject, *};
use windows::Win32::System::Diagnostics::Debug::Extensions::IKeyStore;
use windy::WStr;

impl_debug_interface!(KeyStore, KeyStoreRef, IKeyStore);

impl KeyStore {
    pub fn get_key(
        &self,
        key: impl AsRef<WStr>,
    ) -> WinResult<(ModelObject, Option<KeyStore>)> {
        let mut object = None;
        let mut metadata = None;
        unsafe {
            self.0
                .GetKey(pcw!(key), Some(&mut object), Some(&mut metadata))?;
        }

        Ok((object.unwrap().into(), metadata.map(|x| x.into())))
    }

    pub fn set_key(
        &self,
        key: impl AsRef<WStr>,
        object: Option<&ModelObject>,
        metadata: Option<&KeyStore>,
    ) -> WinResult<()> {
        unsafe {
            self.0.SetKey(
                pcw!(key),
                object.as_ref().map(|x| x.as_raw()),
                metadata.as_ref().map(|x| x.as_raw()),
            )?;
        }

        Ok(())
    }

    pub fn get_key_value(
        &self,
        key: impl AsRef<WStr>,
    ) -> DbgModelResult<(ModelObject, Option<KeyStore>)> {
        let mut object = None;
        let mut metadata = None;
        unsafe {
            dmerr!(
                object,
                self.0.GetKeyValue(
                    pcw!(key),
                    Some(&mut object),
                    Some(&mut metadata),
                )
            );
        }

        Ok((object.unwrap().into(), metadata.map(|x| x.into())))
    }

    pub fn set_key_value(
        &self,
        key: impl AsRef<WStr>,
        object: &ModelObject,
    ) -> WinResult<()> {
        unsafe {
            self.0.SetKeyValue(pcw!(key), object.as_raw())?;
        }

        Ok(())
    }

    pub fn clear_keys(&self) -> WinResult<()> { unsafe { self.0.ClearKeys() } }
}