datafusion_delta_sharing/client/
response.rs

1//! Delta Sharing server response types.
2
3use std::fmt::{Display, Formatter};
4
5use serde::Deserialize;
6
7use crate::securable::{Schema, Share, Table};
8
9use super::action::{File, Metadata, Protocol};
10
11/// Delta Sharing server response for failed requests.
12#[derive(Debug, Deserialize)]
13#[serde(rename_all = "camelCase")]
14pub struct ErrorResponse {
15    error_code: String,
16    message: String,
17}
18
19impl ErrorResponse {
20    /// Retrieve the error code of the response
21    pub fn error_code(&self) -> &str {
22        &self.error_code
23    }
24
25    /// Retrieve the message of the response
26    pub fn _message(&self) -> &str {
27        &self.message
28    }
29}
30
31impl Display for ErrorResponse {
32    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
33        write!(f, "[{}] {}", self.error_code, self.message)
34    }
35}
36
37/// Delta Sharing server response for successful `list_shares` requests.
38#[derive(Debug, Deserialize)]
39#[serde(rename_all = "camelCase")]
40pub struct ListSharesResponse {
41    items: Vec<Share>,
42    next_page_token: Option<String>,
43}
44
45impl ListSharesResponse {
46    /// Retrieve the shares of the response
47    pub fn items(&self) -> &[Share] {
48        &self.items
49    }
50
51    /// Retrieve the next page token of the response
52    pub fn next_page_token(&self) -> Option<&str> {
53        self.next_page_token.as_deref()
54    }
55}
56
57impl IntoIterator for ListSharesResponse {
58    type Item = Share;
59    type IntoIter = std::vec::IntoIter<Self::Item>;
60
61    fn into_iter(self) -> Self::IntoIter {
62        self.items.into_iter()
63    }
64}
65
66/// Delta Sharing server response for successful `get_share` requests.
67#[derive(Debug, Deserialize)]
68pub struct GetShareResponse {
69    share: Share,
70}
71
72impl GetShareResponse {
73    /// Retrieve the share of the response
74    pub fn share(&self) -> &Share {
75        &self.share
76    }
77}
78
79/// Delta Sharing server response for successful `list_schemas` requests.
80#[derive(Debug, Deserialize)]
81#[serde(rename_all = "camelCase")]
82pub struct ListSchemasResponse {
83    items: Vec<Schema>,
84    next_page_token: Option<String>,
85}
86
87impl ListSchemasResponse {
88    /// Retrieve the schemas of the response
89    pub fn items(&self) -> &[Schema] {
90        &self.items
91    }
92
93    /// Retrieve the next page token of the response
94    pub fn next_page_token(&self) -> Option<&str> {
95        self.next_page_token.as_deref()
96    }
97}
98
99impl IntoIterator for ListSchemasResponse {
100    type Item = Schema;
101    type IntoIter = std::vec::IntoIter<Self::Item>;
102
103    fn into_iter(self) -> Self::IntoIter {
104        self.items.into_iter()
105    }
106}
107
108/// Delta Sharing server response for successful `list_tables_in_share` and
109/// `list_tables_in_schema` requests.
110#[derive(Debug, Deserialize)]
111#[serde(rename_all = "camelCase")]
112pub struct ListTablesResponse {
113    items: Vec<Table>,
114    next_page_token: Option<String>,
115}
116
117impl ListTablesResponse {
118    /// Retrieve the tables of the response
119    pub fn items(&self) -> &[Table] {
120        &self.items
121    }
122
123    /// Retrieve the next page token of the response
124    pub fn next_page_token(&self) -> Option<&str> {
125        self.next_page_token.as_deref()
126    }
127}
128
129impl IntoIterator for ListTablesResponse {
130    type Item = Table;
131    type IntoIter = std::vec::IntoIter<Self::Item>;
132
133    fn into_iter(self) -> Self::IntoIter {
134        self.items.into_iter()
135    }
136}
137
138/// Delta Sharing server response lines for successful `get_table_metadata`,
139/// `get_table_data` and `get_table_changes` requests (in parquet format).
140#[derive(Debug, Deserialize)]
141pub enum ParquetResponse {
142    /// Protocol response
143    #[serde(rename = "protocol")]
144    Protocol(Protocol),
145    /// Metadata response
146    #[serde(rename = "metaData")]
147    Metadata(Metadata),
148    /// File response
149    #[serde(rename = "file")]
150    File(File),
151}
152
153impl ParquetResponse {
154    /// Retrieve the protocol of the response
155    pub fn to_protocol(self) -> Option<Protocol> {
156        match self {
157            ParquetResponse::Protocol(p) => Some(p),
158            _ => None,
159        }
160    }
161
162    /// Retrieve the metadata of the response
163    pub fn to_file(self) -> Option<File> {
164        match self {
165            ParquetResponse::File(f) => Some(f),
166            _ => None,
167        }
168    }
169
170    /// Retrieve the metadata of the response
171    pub fn to_metadata(self) -> Option<Metadata> {
172        match self {
173            ParquetResponse::Metadata(m) => Some(m),
174            _ => None,
175        }
176    }
177}