use super::super::dense_key_index::FindOutcome;
use super::{IndexKey, UniqueIndex, UniqueIndexInconsistency};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum UniqueIndexLookupError<Key> {
Inconsistent {
key: Key,
reason: UniqueIndexInconsistency,
},
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[must_use]
pub enum UniqueIndexLookupOutcome<Key> {
Present { key: Key, row: usize },
Absent { key: Key },
}
impl<Key: IndexKey> UniqueIndex<Key> {
pub fn find(
&self,
key: Key,
) -> Result<UniqueIndexLookupOutcome<Key>, UniqueIndexLookupError<Key>> {
match self.key_index.find(key, &self.keys).map_err(|reason| {
UniqueIndexLookupError::Inconsistent {
key,
reason: reason.into(),
}
})? {
FindOutcome::Present { location, .. } => Ok(UniqueIndexLookupOutcome::Present {
key,
row: location.row,
}),
FindOutcome::Absent => Ok(UniqueIndexLookupOutcome::Absent { key }),
}
}
}