use std::str::FromStr;

use dfir_windows_types::Guid;

use crate::{cache::Value, value::FromValue};

impl FromValue for Guid {
    fn from_value_opt(value: &crate::cache::Value) -> crate::ntds::Result<Option<Self>>
    where
        Self: Sized,
    {
        match value {
            Value::Null(_) => Ok(None),
            Value::Binary(v) | Value::Guid(v) => Ok(Some(Guid::from_slice_le(&v[..])?)),
            Value::Text(v) => Ok(Some(Guid::from_str(v)?)),
            v => {
                log::error!("I don't know how to extract GUIDs from {v:?}");
                Ok(None)
            }
        }
    }
}
