icydb_core/db/response/
write.rs1use crate::{
2 traits::{EntityKind, EntityValue, View},
3 types::Ref,
4 view::View as EntityView,
5};
6
7#[derive(Debug)]
15pub struct WriteResponse<E> {
16 entity: E,
17}
18
19impl<E> WriteResponse<E> {
20 #[must_use]
22 pub const fn new(entity: E) -> Self {
23 Self { entity }
24 }
25
26 #[must_use]
28 pub fn entity(self) -> E {
29 self.entity
30 }
31
32 #[must_use]
34 pub fn key(&self) -> E::Id
35 where
36 E: EntityValue,
37 {
38 self.entity.id()
39 }
40
41 #[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 #[must_use]
52 pub fn view(&self) -> EntityView<E>
53 where
54 E: View,
55 {
56 self.entity.to_view()
57 }
58}
59
60#[derive(Debug)]
68pub struct WriteBatchResponse<E> {
69 entries: Vec<WriteResponse<E>>,
70}
71
72impl<E> WriteBatchResponse<E> {
73 #[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 #[must_use]
83 pub fn entries(&self) -> &[WriteResponse<E>] {
84 &self.entries
85 }
86
87 #[must_use]
89 pub const fn len(&self) -> usize {
90 self.entries.len()
91 }
92
93 #[must_use]
95 pub const fn is_empty(&self) -> bool {
96 self.entries.is_empty()
97 }
98
99 #[must_use]
101 pub fn entities(self) -> Vec<E> {
102 self.entries
103 .into_iter()
104 .map(WriteResponse::entity)
105 .collect()
106 }
107
108 #[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 #[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 #[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}