grafbase_sdk/types/
response.rs

1use crate::{SdkError, wit};
2
3use super::{Data, Error};
4
5/// Represents a response from the Grafbase SDK, which can contain data or errors.
6#[derive(Debug, Default)]
7pub struct Response {
8    /// The data returned by the response, if any.
9    pub data: Option<Data>,
10    /// A list of errors that occurred during processing, if any.
11    pub errors: Vec<Error>,
12}
13
14impl From<Data> for Response {
15    fn from(data: Data) -> Self {
16        Response {
17            data: Some(data),
18            errors: Vec::new(),
19        }
20    }
21}
22
23impl From<Error> for Response {
24    fn from(error: Error) -> Self {
25        Response {
26            data: None,
27            errors: vec![error],
28        }
29    }
30}
31
32impl From<SdkError> for Response {
33    fn from(error: SdkError) -> Self {
34        Response {
35            data: None,
36            errors: vec![error.into()],
37        }
38    }
39}
40
41impl From<Vec<Error>> for Response {
42    fn from(errors: Vec<Error>) -> Self {
43        Response { data: None, errors }
44    }
45}
46
47impl<T, E> From<Result<T, E>> for Response
48where
49    Response: From<T> + From<E>,
50{
51    fn from(result: Result<T, E>) -> Self {
52        match result {
53            Ok(data) => data.into(),
54            Err(error) => error.into(),
55        }
56    }
57}
58
59impl Response {
60    /// Creates a new empty response with no data and no errors.
61    pub fn null() -> Self {
62        Self::default()
63    }
64
65    /// Creates a new response with JSON data.
66    pub fn json(bytes: Vec<u8>) -> Self {
67        Response {
68            data: Some(Data::Json(bytes)),
69            errors: Vec::new(),
70        }
71    }
72
73    /// Creates a new response with CBOR data.
74    pub fn cbor(bytes: Vec<u8>) -> Self {
75        Response {
76            data: Some(Data::Cbor(bytes)),
77            errors: Vec::new(),
78        }
79    }
80
81    /// Creates a new response with an error.
82    pub fn error<E: Into<Error>>(error: E) -> Self {
83        Response {
84            data: None,
85            errors: vec![error.into()],
86        }
87    }
88
89    /// Creates a new response with serialized data.
90    pub fn data<T: serde::Serialize>(data: T) -> Self {
91        match crate::cbor::to_vec(&data) {
92            Ok(data) => Response {
93                data: Some(Data::Cbor(data)),
94                errors: Vec::new(),
95            },
96            Err(err) => Response {
97                data: None,
98                errors: vec![SdkError::from(err).into()],
99            },
100        }
101    }
102}
103
104impl From<Response> for wit::Response {
105    fn from(response: Response) -> Self {
106        Self {
107            data: response.data.map(Into::into),
108            errors: response.errors.into_iter().map(Into::into).collect(),
109        }
110    }
111}
112
113impl<E: Into<wit::Error>> From<Result<Response, E>> for wit::Response {
114    fn from(result: Result<Response, E>) -> Self {
115        match result {
116            Ok(response) => response.into(),
117            Err(error) => Self {
118                data: None,
119                errors: vec![error.into()],
120            },
121        }
122    }
123}
124
125impl From<Error> for wit::Response {
126    fn from(error: Error) -> Self {
127        Self {
128            data: None,
129            errors: vec![error.into()],
130        }
131    }
132}