use crate::{
traits::{EntityKind, EntityValue},
types::Id,
};
use icydb_core::db::WriteBatchResponse as CoreWriteBatchResponse;
#[derive(Debug)]
pub struct WriteResponse<E: EntityKind> {
entity: E,
}
impl<E: EntityKind> WriteResponse<E> {
#[must_use]
pub const fn new(entity: E) -> Self {
Self { entity }
}
#[must_use]
pub fn entity(self) -> E {
self.entity
}
#[must_use]
pub fn id(&self) -> Id<E>
where
E: EntityValue,
{
self.entity.id()
}
}
#[derive(Debug)]
pub struct WriteBatchResponse<E: EntityKind> {
entities: Vec<E>,
}
impl<E: EntityKind> WriteBatchResponse<E> {
#[must_use]
pub const fn new(entities: Vec<E>) -> Self {
Self { entities }
}
#[must_use]
pub fn from_core(inner: CoreWriteBatchResponse<E>) -> Self {
Self {
entities: inner.into_iter().collect(),
}
}
#[must_use]
pub const fn len(&self) -> usize {
self.entities.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.entities.is_empty()
}
#[must_use]
pub fn entities(self) -> Vec<E> {
self.entities
}
pub fn ids(&self) -> impl Iterator<Item = Id<E>> + '_
where
E: EntityValue,
{
self.entities.iter().map(EntityValue::id)
}
}
impl<E: EntityKind> WriteBatchResponse<E> {
pub fn iter(&self) -> std::slice::Iter<'_, E> {
self.entities.iter()
}
}
impl<'a, E: EntityKind> IntoIterator for &'a WriteBatchResponse<E> {
type Item = &'a E;
type IntoIter = std::slice::Iter<'a, E>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}