use dfir_windows_types::Sid;

use crate::cache::Value;

use crate::ntds::Error;
use crate::value::{StringOrStringSet, ToStringOrStringSet};
use crate::CDatabase;

use super::FromValue;

impl FromValue for Sid {
    fn from_value_opt(value: &Value) -> Result<Option<Self>, Error>
    where
        Self: Sized,
    {
        match value {
            Value::Binary(val) => Ok(Some(Sid::try_from(&val[..])?.with_big_endian_rid())),
            Value::Null(()) => Ok(None),
            _ => Err(Error::InvalidValueDetected(
                value.to_string(),
                "Sid (binary)",
            )),
        }
    }
}


impl ToStringOrStringSet for Sid
{
    fn to_string_or_stringset(&self, _database: &CDatabase<'_, '_>) -> StringOrStringSet {
        StringOrStringSet::String(self.to_string())
    }
}