Skip to main content

icydb_core/db/response/
write.rs

1use crate::{
2    traits::{AsView, EntityValue},
3    types::Id,
4};
5
6///
7/// WriteResponse
8///
9/// Result of a single write operation.
10/// Provides explicit access to the stored entity and its identifier.
11///
12
13#[derive(Debug)]
14pub struct WriteResponse<E> {
15    entity: E,
16}
17
18impl<E> WriteResponse<E> {
19    /// Construct a write response from the stored entity.
20    #[must_use]
21    pub const fn new(entity: E) -> Self {
22        Self { entity }
23    }
24
25    /// Return the stored entity.
26    #[must_use]
27    pub fn entity(self) -> E {
28        self.entity
29    }
30
31    /// Return the stored entity's identity
32    #[must_use]
33    pub fn id(&self) -> Id<E>
34    where
35        E: EntityValue,
36    {
37        self.entity.id()
38    }
39
40    /// Return the stored entity as its view type.
41    #[must_use]
42    pub fn view(&self) -> <E as AsView>::ViewType
43    where
44        E: AsView,
45    {
46        self.entity.as_view()
47    }
48}
49
50///
51/// WriteBatchResponse
52///
53/// Result of a batch write operation.
54/// Provides explicit access to stored entities and their identifiers.
55///
56
57#[derive(Debug)]
58pub struct WriteBatchResponse<E> {
59    entries: Vec<WriteResponse<E>>,
60}
61
62impl<E> WriteBatchResponse<E> {
63    /// Construct a batch response from stored entities.
64    #[must_use]
65    pub fn new(entities: Vec<E>) -> Self {
66        Self {
67            entries: entities.into_iter().map(WriteResponse::new).collect(),
68        }
69    }
70
71    /// Return all write responses.
72    #[must_use]
73    pub fn entries(&self) -> &[WriteResponse<E>] {
74        &self.entries
75    }
76
77    /// Return the number of entries.
78    #[must_use]
79    pub const fn len(&self) -> usize {
80        self.entries.len()
81    }
82
83    /// Returns `true` if the batch is empty.
84    #[must_use]
85    pub const fn is_empty(&self) -> bool {
86        self.entries.is_empty()
87    }
88
89    /// Return all stored entities.
90    #[must_use]
91    pub fn entities(self) -> Vec<E> {
92        self.entries
93            .into_iter()
94            .map(WriteResponse::entity)
95            .collect()
96    }
97
98    /// Return all primary keys for correlation, reporting, and lookup.
99    #[must_use]
100    pub fn ids(&self) -> Vec<Id<E>>
101    where
102        E: EntityValue,
103    {
104        self.entries.iter().map(WriteResponse::id).collect()
105    }
106
107    /// Return all views.
108    #[must_use]
109    pub fn views(&self) -> Vec<<E as AsView>::ViewType>
110    where
111        E: AsView,
112    {
113        self.entries.iter().map(WriteResponse::view).collect()
114    }
115}
116
117impl<E> IntoIterator for WriteBatchResponse<E> {
118    type Item = WriteResponse<E>;
119    type IntoIter = std::vec::IntoIter<WriteResponse<E>>;
120
121    fn into_iter(self) -> Self::IntoIter {
122        self.entries.into_iter()
123    }
124}
125
126impl<E> WriteBatchResponse<E> {
127    pub fn iter(&self) -> std::slice::Iter<'_, WriteResponse<E>> {
128        self.entries.iter()
129    }
130}
131
132impl<'a, E> IntoIterator for &'a WriteBatchResponse<E> {
133    type Item = &'a WriteResponse<E>;
134    type IntoIter = std::slice::Iter<'a, WriteResponse<E>>;
135
136    fn into_iter(self) -> Self::IntoIter {
137        self.iter()
138    }
139}