Skip to main content

icydb_core/db/response/
write.rs

1use crate::{
2    traits::{EntityKind, EntityValue, View},
3    types::Ref,
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 identity.
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    #[must_use]
34    pub fn key(&self) -> E::Id
35    where
36        E: EntityValue,
37    {
38        self.entity.id()
39    }
40
41    /// Return a typed reference to the stored entity.
42    #[must_use]
43    pub fn reference(&self) -> Ref<E>
44    where
45        E: EntityKind + EntityValue,
46    {
47        Ref::new(self.entity.id())
48    }
49
50    /// Return the stored entity as its view type.
51    #[must_use]
52    pub fn view(&self) -> EntityView<E>
53    where
54        E: View,
55    {
56        self.entity.to_view()
57    }
58}
59
60///
61/// WriteBatchResponse
62///
63/// Result of a batch write operation.
64/// Provides explicit access to stored entities and their identities.
65///
66
67#[derive(Debug)]
68pub struct WriteBatchResponse<E> {
69    entries: Vec<WriteResponse<E>>,
70}
71
72impl<E> WriteBatchResponse<E> {
73    /// Construct a batch response from stored entities.
74    #[must_use]
75    pub fn new(entities: Vec<E>) -> Self {
76        Self {
77            entries: entities.into_iter().map(WriteResponse::new).collect(),
78        }
79    }
80
81    /// Return all write responses.
82    #[must_use]
83    pub fn entries(&self) -> &[WriteResponse<E>] {
84        &self.entries
85    }
86
87    /// Return the number of entries.
88    #[must_use]
89    pub const fn len(&self) -> usize {
90        self.entries.len()
91    }
92
93    /// Returns `true` if the batch is empty.
94    #[must_use]
95    pub const fn is_empty(&self) -> bool {
96        self.entries.is_empty()
97    }
98
99    /// Return all stored entities.
100    #[must_use]
101    pub fn entities(self) -> Vec<E> {
102        self.entries
103            .into_iter()
104            .map(WriteResponse::entity)
105            .collect()
106    }
107
108    /// Return all primary keys.
109    #[must_use]
110    pub fn keys(&self) -> Vec<E::Id>
111    where
112        E: EntityValue,
113    {
114        self.entries.iter().map(WriteResponse::key).collect()
115    }
116
117    /// Return all typed references.
118    #[must_use]
119    pub fn references(&self) -> Vec<Ref<E>>
120    where
121        E: EntityKind + EntityValue,
122    {
123        self.entries.iter().map(WriteResponse::reference).collect()
124    }
125
126    /// Return all views.
127    #[must_use]
128    pub fn views(&self) -> Vec<EntityView<E>>
129    where
130        E: View,
131    {
132        self.entries.iter().map(WriteResponse::view).collect()
133    }
134}
135
136impl<E> IntoIterator for WriteBatchResponse<E> {
137    type Item = WriteResponse<E>;
138    type IntoIter = std::vec::IntoIter<WriteResponse<E>>;
139
140    fn into_iter(self) -> Self::IntoIter {
141        self.entries.into_iter()
142    }
143}
144
145impl<E> WriteBatchResponse<E> {
146    pub fn iter(&self) -> std::slice::Iter<'_, WriteResponse<E>> {
147        self.entries.iter()
148    }
149}
150
151impl<'a, E> IntoIterator for &'a WriteBatchResponse<E> {
152    type Item = &'a WriteResponse<E>;
153    type IntoIter = std::slice::Iter<'a, WriteResponse<E>>;
154
155    fn into_iter(self) -> Self::IntoIter {
156        self.iter()
157    }
158}