icydb_core/db/response/
mod.rs1use crate::{Error, Key, ThisError, db::DbError, traits::EntityKind};
2
3#[derive(Debug, ThisError)]
8pub enum ResponseError {
9 #[error("expected one or more rows, found 0 (entity {0})")]
10 NoRowsFound(String),
11}
12
13impl From<ResponseError> for Error {
14 fn from(err: ResponseError) -> Self {
15 DbError::from(err).into()
16 }
17}
18
19#[derive(Debug)]
24pub struct Response<E: EntityKind>(pub Vec<(Key, E)>);
25
26impl<E> Response<E>
27where
28 E: EntityKind,
29{
30 #[must_use]
33 #[allow(clippy::cast_possible_truncation)]
34 pub const fn count(&self) -> u32 {
36 self.0.len() as u32
37 }
38
39 #[must_use]
40 pub const fn is_empty(&self) -> bool {
42 self.0.is_empty()
43 }
44
45 #[must_use]
50 pub fn key(&self) -> Option<Key> {
52 self.0.first().map(|(key, _)| *key)
53 }
54
55 pub fn try_key(&self) -> Result<Key, Error> {
57 let key = self
58 .key()
59 .ok_or_else(|| ResponseError::NoRowsFound(E::PATH.to_string()))?;
60
61 Ok(key)
62 }
63
64 #[must_use]
65 pub fn keys(&self) -> Vec<Key> {
67 self.0.iter().map(|(key, _)| *key).collect()
68 }
69
70 pub fn keys_iter(self) -> impl Iterator<Item = Key> {
72 self.0.into_iter().map(|(key, _)| key)
73 }
74
75 #[must_use]
80 pub fn pk(&self) -> Option<E::PrimaryKey> {
82 self.0.first().map(|(_, e)| e.primary_key())
83 }
84
85 pub fn try_pk(&self) -> Result<E::PrimaryKey, Error> {
87 let pk = self
88 .pk()
89 .ok_or_else(|| ResponseError::NoRowsFound(E::PATH.to_string()))?;
90
91 Ok(pk)
92 }
93
94 #[must_use]
95 pub fn pks(&self) -> Vec<E::PrimaryKey> {
97 self.0.iter().map(|(_, e)| e.primary_key()).collect()
98 }
99
100 pub fn pks_iter(self) -> impl Iterator<Item = E::PrimaryKey> {
102 self.0.into_iter().map(|(_, e)| e.primary_key())
103 }
104
105 #[must_use]
110 pub fn entity(self) -> Option<E> {
112 self.0.into_iter().next().map(|(_, e)| e)
113 }
114
115 pub fn try_entity(self) -> Result<E, Error> {
117 let res = self
118 .entity()
119 .ok_or_else(|| ResponseError::NoRowsFound(E::PATH.to_string()))?;
120
121 Ok(res)
122 }
123
124 #[must_use]
125 pub fn entities(self) -> Vec<E> {
127 self.0.into_iter().map(|(_, e)| e).collect()
128 }
129
130 pub fn entities_iter(self) -> impl Iterator<Item = E> {
132 self.0.into_iter().map(|(_, e)| e)
133 }
134
135 #[must_use]
140 pub fn view(self) -> Option<E::ViewType> {
142 self.entity().map(|e| e.to_view())
143 }
144
145 pub fn try_view(self) -> Result<E::ViewType, Error> {
147 self.try_entity().map(|e| e.to_view())
148 }
149
150 #[must_use]
151 pub fn views(self) -> Vec<E::ViewType> {
153 self.entities().into_iter().map(|e| e.to_view()).collect()
154 }
155
156 pub fn views_iter(self) -> impl Iterator<Item = E::ViewType> {
158 self.entities().into_iter().map(|e| e.to_view())
159 }
160}
161
162impl<E: EntityKind> IntoIterator for Response<E> {
163 type Item = (Key, E);
164 type IntoIter = std::vec::IntoIter<Self::Item>;
165
166 fn into_iter(self) -> Self::IntoIter {
167 self.0.into_iter()
168 }
169}