pipecircuit 0.0.14

Correct. Robust. Fast. Portable.
Documentation
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 { row, .. } => Ok(UniqueIndexLookupOutcome::Present { key, row }),
            FindOutcome::Absent => Ok(UniqueIndexLookupOutcome::Absent { key }),
        }
    }
}