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