use crate::cache::{ColumnIndex, Value};
use crate::ntds::Error;

pub trait FromValue {
    /// does the same as [`from_value_opt`], but returns an Error instead of `None`, if no value was found
    fn from_value(value: &Value) -> crate::ntds::Result<Self>
    where
        Self: Sized,
    {
        Self::from_value_opt(value)?.ok_or(Error::ValueIsMissing)
    }

    /// converts the value into the requested type, if possible
    fn from_value_opt(value: &Value) -> crate::ntds::Result<Option<Self>>
    where
        Self: Sized;

    fn from_record_opt(
        record: &libesedb::Record,
        record_id: &ColumnIndex,
    ) -> crate::ntds::Result<Option<Self>>
    where
        Self: Sized,
    {
        match Value::from_record_opt(record, record_id)? {
            Some(v) => Self::from_value_opt(&v),
            None => Ok(None),
        }
    }
}
