supabase_client_core/
response.rs1use crate::error::{StatusCode, SupabaseError};
2
3#[derive(Debug)]
5pub struct SupabaseResponse<T> {
6 pub data: Vec<T>,
8 pub error: Option<SupabaseError>,
10 pub count: Option<i64>,
12 pub status: StatusCode,
14}
15
16impl<T> SupabaseResponse<T> {
17 pub fn ok(data: Vec<T>) -> Self {
19 Self {
20 data,
21 error: None,
22 count: None,
23 status: StatusCode::Ok,
24 }
25 }
26
27 pub fn ok_with_count(data: Vec<T>, count: i64) -> Self {
29 Self {
30 data,
31 error: None,
32 count: Some(count),
33 status: StatusCode::Ok,
34 }
35 }
36
37 pub fn created(data: Vec<T>) -> Self {
39 Self {
40 data,
41 error: None,
42 count: None,
43 status: StatusCode::Created,
44 }
45 }
46
47 pub fn error(err: SupabaseError) -> Self {
49 let status = match &err {
50 SupabaseError::NoRows => StatusCode::NotFound,
51 #[cfg(feature = "direct-sql")]
52 SupabaseError::Database(_) => StatusCode::InternalError,
53 _ => StatusCode::InternalError,
54 };
55 Self {
56 data: Vec::new(),
57 error: Some(err),
58 count: None,
59 status,
60 }
61 }
62
63 pub fn no_content() -> Self {
65 Self {
66 data: Vec::new(),
67 error: None,
68 count: None,
69 status: StatusCode::NoContent,
70 }
71 }
72
73 pub fn is_ok(&self) -> bool {
75 self.error.is_none()
76 }
77
78 pub fn is_err(&self) -> bool {
80 self.error.is_some()
81 }
82
83 pub fn into_result(self) -> Result<Vec<T>, SupabaseError> {
86 match self.error {
87 Some(err) => Err(err),
88 None => Ok(self.data),
89 }
90 }
91
92 pub fn first(&self) -> Option<&T> {
94 self.data.first()
95 }
96
97 pub fn into_single(self) -> Result<T, SupabaseError> {
99 if let Some(err) = self.error {
100 return Err(err);
101 }
102 let mut data = self.data;
103 match data.len() {
104 0 => Err(SupabaseError::NoRows),
105 1 => Ok(data.remove(0)),
106 n => Err(SupabaseError::MultipleRows(n)),
107 }
108 }
109
110 pub fn into_maybe_single(self) -> Result<Option<T>, SupabaseError> {
112 if let Some(err) = self.error {
113 return Err(err);
114 }
115 let mut data = self.data;
116 match data.len() {
117 0 => Ok(None),
118 1 => Ok(Some(data.remove(0))),
119 n => Err(SupabaseError::MultipleRows(n)),
120 }
121 }
122}
123
124impl<T> SupabaseResponse<T>
125where
126 T: Clone,
127{
128 pub fn with_status(mut self, status: StatusCode) -> Self {
130 self.status = status;
131 self
132 }
133
134 pub fn with_count(mut self, count: i64) -> Self {
136 self.count = Some(count);
137 self
138 }
139}