Skip to content

Commit 51dac3c

Browse files
committed
Add Debug impls.
This commit makes the following changes - Add an opaque `Debug` impl for `Client`. - Add a rich `Debug` impl for `Row`. - Make the `Debug` impl for `Type` clearer. - Change the `Debug` for `Column` to be slightly neater.
1 parent 4c0ee2c commit 51dac3c

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed

postgres-types/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,15 @@ mod special;
207207
mod type_gen;
208208

209209
/// A Postgres type.
210-
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
210+
#[derive(PartialEq, Eq, Clone, Hash)]
211211
pub struct Type(Inner);
212212

213+
impl fmt::Debug for Type {
214+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
215+
fmt::Debug::fmt(&self.0, fmt)
216+
}
217+
}
218+
213219
impl fmt::Display for Type {
214220
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
215221
match self.schema() {

tokio-postgres/src/client.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use futures::{future, pin_mut, ready, StreamExt, TryStreamExt};
2121
use parking_lot::Mutex;
2222
use postgres_protocol::message::backend::Message;
2323
use std::collections::HashMap;
24+
use std::fmt;
2425
use std::sync::Arc;
2526
use std::task::{Context, Poll};
2627
use std::time::Duration;
@@ -529,3 +530,9 @@ impl Client {
529530
self.inner.sender.is_closed()
530531
}
531532
}
533+
534+
impl fmt::Debug for Client {
535+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
536+
f.debug_struct("Client").finish()
537+
}
538+
}

tokio-postgres/src/row.rs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,62 @@ pub struct Row {
100100
ranges: Vec<Option<Range<usize>>>,
101101
}
102102

103+
/// A macro to map pg types to rust types, for debug display.
104+
macro_rules! debug_row_type {
105+
($this:expr; $map:expr; $idx:expr; $name:expr; $type:expr; $($pg_ty:tt => $ty:ty),*) => {
106+
match $type {
107+
$(
108+
&Type::$pg_ty => match <$ty as FromSql>::from_sql_nullable(
109+
&Type::$pg_ty,
110+
$this.0.col_buffer($idx),
111+
) {
112+
Ok(val) => $map.entry(&$name, &val),
113+
Err(_) => $map.entry(&$name, &"<error>"),
114+
},
115+
)*
116+
_ => $map.entry(&$name, &"<opaque type>"),
117+
}
118+
}
119+
}
120+
121+
impl fmt::Debug for Row {
122+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123+
// Provides debug impl for row contents.
124+
struct RowData<'a>(&'a Row);
125+
126+
impl fmt::Debug for RowData<'_> {
127+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128+
let mut map = f.debug_map();
129+
for (idx, col) in self.0.columns().iter().enumerate() {
130+
debug_row_type!(self; map; idx; col.name(); col.type_();
131+
BOOL => bool,
132+
INT2 => i16,
133+
INT4 => i32,
134+
INT8 => i64,
135+
FLOAT4 => f32,
136+
FLOAT8 => f64,
137+
VARCHAR => String,
138+
BPCHAR => String,
139+
TEXT => String,
140+
JSON => String,
141+
XML => String,
142+
TIMESTAMPTZ => std::time::SystemTime,
143+
TIMESTAMP => std::time::SystemTime,
144+
BYTEA => Vec<u8>
145+
// More types could be added here.
146+
);
147+
}
148+
map.finish()
149+
}
150+
}
151+
152+
f.debug_struct("Row")
153+
.field("columns", &self.columns())
154+
.field("data", &RowData(self))
155+
.finish()
156+
}
157+
}
158+
103159
impl Row {
104160
pub(crate) fn new(statement: Statement, body: DataRowBody) -> Result<Row, Error> {
105161
let ranges = body.ranges().collect().map_err(Error::parse)?;
@@ -170,8 +226,13 @@ impl Row {
170226
));
171227
}
172228

173-
let buf = self.ranges[idx].clone().map(|r| &self.body.buffer()[r]);
174-
FromSql::from_sql_nullable(ty, buf).map_err(|e| Error::from_sql(e, idx))
229+
FromSql::from_sql_nullable(ty, self.col_buffer(idx)).map_err(|e| Error::from_sql(e, idx))
230+
}
231+
232+
/// Get the raw bytes for the column at the given index.
233+
fn col_buffer(&self, idx: usize) -> Option<&[u8]> {
234+
let range = self.ranges[idx].to_owned()?;
235+
Some(&self.body.buffer()[range])
175236
}
176237
}
177238

tokio-postgres/src/statement.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use crate::codec::FrontendMessage;
33
use crate::connection::RequestMessages;
44
use crate::types::Type;
55
use postgres_protocol::message::frontend;
6-
use std::sync::{Arc, Weak};
6+
use std::{
7+
fmt,
8+
sync::{Arc, Weak},
9+
};
710

811
struct StatementInner {
912
client: Weak<InnerClient>,
@@ -62,7 +65,6 @@ impl Statement {
6265
}
6366

6467
/// Information about a column of a query.
65-
#[derive(Debug)]
6668
pub struct Column {
6769
name: String,
6870
type_: Type,
@@ -83,3 +85,12 @@ impl Column {
8385
&self.type_
8486
}
8587
}
88+
89+
impl fmt::Debug for Column {
90+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
91+
fmt.debug_struct("Column")
92+
.field("name", &self.name)
93+
.field("type", &self.type_)
94+
.finish()
95+
}
96+
}

0 commit comments

Comments
 (0)