Skip to main content

icydb_core/db/response/
write.rs

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