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