1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use crate::io::Buf;
use crate::postgres::protocol::Decode;
use std::{
fmt, io,
pin::Pin,
ptr::NonNull,
str::{self, FromStr},
};
#[derive(Debug, Copy, Clone)]
pub enum Severity {
Panic,
Fatal,
Error,
Warning,
Notice,
Debug,
Info,
Log,
}
impl Severity {
pub fn is_error(self) -> bool {
match self {
Severity::Panic | Severity::Fatal | Severity::Error => true,
_ => false,
}
}
pub fn is_notice(self) -> bool {
match self {
Severity::Warning
| Severity::Notice
| Severity::Debug
| Severity::Info
| Severity::Log => true,
_ => false,
}
}
pub fn to_str(self) -> &'static str {
match self {
Severity::Panic => "PANIC",
Severity::Fatal => "FATAL",
Severity::Error => "ERROR",
Severity::Warning => "WARNING",
Severity::Notice => "NOTICE",
Severity::Debug => "DEBUG",
Severity::Info => "INFO",
Severity::Log => "LOG",
}
}
}
impl FromStr for Severity {
type Err = crate::Error;
fn from_str(s: &str) -> crate::Result<Self> {
Ok(match s {
"PANIC" => Severity::Panic,
"FATAL" => Severity::Fatal,
"ERROR" => Severity::Error,
"WARNING" => Severity::Warning,
"NOTICE" => Severity::Notice,
"DEBUG" => Severity::Debug,
"INFO" => Severity::Info,
"LOG" => Severity::Log,
_ => {
return Err(protocol_err!("unexpected response severity: {}", s).into());
}
})
}
}
#[derive(Debug)]
pub struct Response {
pub severity: Severity,
pub code: Box<str>,
pub message: Box<str>,
pub detail: Option<Box<str>>,
pub hint: Option<Box<str>>,
pub position: Option<usize>,
pub internal_position: Option<usize>,
pub internal_query: Option<Box<str>>,
pub where_: Option<Box<str>>,
pub schema: Option<Box<str>>,
pub table: Option<Box<str>>,
pub column: Option<Box<str>>,
pub data_type: Option<Box<str>>,
pub constraint: Option<Box<str>>,
pub file: Option<Box<str>>,
pub line: Option<usize>,
pub routine: Option<Box<str>>,
}
impl Decode for Response {
fn decode(mut buf: &[u8]) -> crate::Result<Self> {
let mut code = None::<Box<str>>;
let mut message = None::<Box<str>>;
let mut severity = None::<Box<str>>;
let mut severity_non_local = None::<Severity>;
let mut detail = None::<Box<str>>;
let mut hint = None::<Box<str>>;
let mut position = None::<usize>;
let mut internal_position = None::<usize>;
let mut internal_query = None::<Box<str>>;
let mut where_ = None::<Box<str>>;
let mut schema = None::<Box<str>>;
let mut table = None::<Box<str>>;
let mut column = None::<Box<str>>;
let mut data_type = None::<Box<str>>;
let mut constraint = None::<Box<str>>;
let mut file = None::<Box<str>>;
let mut line = None::<usize>;
let mut routine = None::<Box<str>>;
loop {
let field_type = buf.get_u8()?;
if field_type == 0 {
break;
}
let field_value = buf.get_str_nul()?;
match field_type {
b'S' => {
severity = Some(field_value.into());
}
b'V' => {
severity_non_local = Some(field_value.parse()?);
}
b'C' => {
code = Some(field_value.into());
}
b'M' => {
message = Some(field_value.into());
}
b'D' => {
detail = Some(field_value.into());
}
b'H' => {
hint = Some(field_value.into());
}
b'P' => {
position = Some(
field_value
.parse()
.or(Err(protocol_err!("expected int, got: {}", field_value)))?,
);
}
b'p' => {
internal_position = Some(
field_value
.parse()
.or(Err(protocol_err!("expected int, got: {}", field_value)))?,
);
}
b'q' => {
internal_query = Some(field_value.into());
}
b'w' => {
where_ = Some(field_value.into());
}
b's' => {
schema = Some(field_value.into());
}
b't' => {
table = Some(field_value.into());
}
b'c' => {
column = Some(field_value.into());
}
b'd' => {
data_type = Some(field_value.into());
}
b'n' => {
constraint = Some(field_value.into());
}
b'F' => {
file = Some(field_value.into());
}
b'L' => {
line = Some(
field_value
.parse()
.or(Err(protocol_err!("expected int, got: {}", field_value)))?,
);
}
b'R' => {
routine = Some(field_value.into());
}
_ => {
return Err(protocol_err!(
"received unknown field in Response: {}",
field_type
)
.into());
}
}
}
let severity = severity_non_local
.or_else(move || severity?.as_ref().parse().ok())
.ok_or(protocol_err!(
"did not receieve field `severity` for Response"
))?;
let code = code.ok_or(protocol_err!("did not receieve field `code` for Response",))?;
let message = message.ok_or(protocol_err!(
"did not receieve field `message` for Response"
))?;
Ok(Self {
severity,
code,
message,
detail,
hint,
internal_query,
where_,
schema,
table,
column,
data_type,
constraint,
file,
routine,
line,
position,
internal_position,
})
}
}
#[cfg(test)]
mod tests {
use super::{Decode, Response, Severity};
use matches::assert_matches;
const RESPONSE: &[u8] = b"SNOTICE\0VNOTICE\0C42710\0Mextension \"uuid-ossp\" already exists, \
skipping\0Fextension.c\0L1656\0RCreateExtension\0\0";
#[test]
fn it_decodes_response() {
let message = Response::decode(RESPONSE).unwrap();
assert_matches!(message.severity, Severity::Notice);
assert_eq!(&*message.code, "42710");
assert_eq!(&*message.file.unwrap(), "extension.c");
assert_eq!(message.line, Some(1656));
assert_eq!(&*message.routine.unwrap(), "CreateExtension");
assert_eq!(
&*message.message,
"extension \"uuid-ossp\" already exists, skipping"
);
}
}