use super::{WarpTable, WarpTableInsertWithIdOutcome, WarpTableRow};
#[derive(Debug, Eq, PartialEq)]
#[must_use]
pub enum WarpTableUpsertOutcome<Key, Row> {
Inserted { key: Key, row: usize },
Replaced { key: Key, row: usize, previous: Row },
Full { key: Key, rejected: Row },
}
impl<Row: WarpTableRow, Brand> WarpTable<Row, Brand> {
pub fn upsert(&mut self, key: Row::Key, row: Row) -> WarpTableUpsertOutcome<Row::Key, Row> {
match self.insert_with_id(key, row) {
WarpTableInsertWithIdOutcome::Inserted { key, row } => {
WarpTableUpsertOutcome::Inserted { key, row }
}
WarpTableInsertWithIdOutcome::AlreadyPresent {
key,
existing_row,
rejected: replacement,
} => WarpTableUpsertOutcome::Replaced {
key,
row: existing_row,
previous: self.replace_at(key, existing_row, replacement),
},
WarpTableInsertWithIdOutcome::Full { key, rejected } => {
WarpTableUpsertOutcome::Full { key, rejected }
}
}
}
}