Skip to content

Commit f7d57eb

Browse files
committed
Implement Show for stuff
1 parent 900ae55 commit f7d57eb

2 files changed

Lines changed: 85 additions & 14 deletions

File tree

src/error.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ make_errors! {
356356
}
357357

358358
/// Reasons a new Postgres connection could fail
359-
#[derive(Clone, PartialEq, Eq)]
359+
#[derive(Clone, PartialEq, Eq, Show)]
360360
pub enum ConnectError {
361361
/// The provided URL could not be parsed
362362
InvalidUrl(String),
@@ -431,7 +431,7 @@ impl error::FromError<SslError> for ConnectError {
431431
}
432432
}
433433

434-
impl fmt::Show for ConnectError {
434+
impl fmt::String for ConnectError {
435435
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
436436
match *self {
437437
ConnectError::InvalidUrl(ref err) => write!(fmt, "Invalid URL: {}", err),
@@ -454,7 +454,7 @@ impl fmt::Show for ConnectError {
454454
}
455455

456456
/// Represents the position of an error in a query
457-
#[derive(Clone, PartialEq, Eq)]
457+
#[derive(Clone, PartialEq, Eq, Show)]
458458
pub enum ErrorPosition {
459459
/// A position in the original query
460460
Normal(usize),
@@ -468,7 +468,7 @@ pub enum ErrorPosition {
468468
}
469469

470470
/// Encapsulates a Postgres error or notice.
471-
#[derive(Clone, PartialEq, Eq)]
471+
#[derive(Clone, PartialEq, Eq, Show)]
472472
pub struct DbError {
473473
/// The field contents are ERROR, FATAL, or PANIC (in an error message),
474474
/// or WARNING, NOTICE, DEBUG, INFO, or LOG (in a notice message), or a
@@ -572,7 +572,7 @@ impl DbError {
572572
}
573573
}
574574

575-
impl fmt::Show for DbError {
575+
impl fmt::String for DbError {
576576
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
577577
write!(fmt, "{}: {}", self.severity, self.message)
578578
}
@@ -589,7 +589,7 @@ impl error::Error for DbError {
589589
}
590590

591591
/// An error encountered when communicating with the Postgres server
592-
#[derive(Clone, PartialEq, Eq)]
592+
#[derive(Clone, PartialEq, Eq, Show)]
593593
pub enum Error {
594594
/// An error reported by the Postgres server
595595
DbError(DbError),
@@ -623,7 +623,7 @@ pub enum Error {
623623
BadData,
624624
}
625625

626-
impl fmt::Show for Error {
626+
impl fmt::String for Error {
627627
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
628628
match *self {
629629
Error::DbError(ref err) => err.fmt(fmt),

src/lib.rs

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ const TYPENAME_QUERY: &'static str = "t";
105105
pub type Result<T> = result::Result<T, Error>;
106106

107107
/// Specifies the target server to connect to.
108-
#[derive(Clone)]
108+
#[derive(Clone, Show)]
109109
pub enum ConnectTarget {
110110
/// Connect via TCP to the specified host.
111111
Tcp(String),
@@ -114,7 +114,7 @@ pub enum ConnectTarget {
114114
}
115115

116116
/// Authentication information
117-
#[derive(Clone)]
117+
#[derive(Clone, Show)]
118118
pub struct UserInfo {
119119
/// The username
120120
pub user: String,
@@ -123,7 +123,7 @@ pub struct UserInfo {
123123
}
124124

125125
/// Information necessary to open a new connection to a Postgres server.
126-
#[derive(Clone)]
126+
#[derive(Clone, Show)]
127127
pub struct ConnectParams {
128128
/// The target server
129129
pub target: ConnectTarget,
@@ -205,7 +205,7 @@ pub trait NoticeHandler: Send {
205205
/// A notice handler which logs at the `info` level.
206206
///
207207
/// This is the default handler used by a `Connection`.
208-
#[derive(Copy)]
208+
#[derive(Copy, Show)]
209209
pub struct DefaultNoticeHandler;
210210

211211
impl NoticeHandler for DefaultNoticeHandler {
@@ -215,6 +215,7 @@ impl NoticeHandler for DefaultNoticeHandler {
215215
}
216216

217217
/// An asynchronous notification
218+
#[derive(Clone, Show)]
218219
pub struct Notification {
219220
/// The process ID of the notifying backend process
220221
pub pid: u32,
@@ -229,6 +230,12 @@ pub struct Notifications<'conn> {
229230
conn: &'conn Connection
230231
}
231232

233+
impl<'a> fmt::Show for Notifications<'a> {
234+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
235+
write!(fmt, "Notifications")
236+
}
237+
}
238+
232239
impl<'conn> Iterator for Notifications<'conn> {
233240
type Item = Notification;
234241

@@ -326,7 +333,7 @@ impl<'conn> Notifications<'conn> {
326333
}
327334

328335
/// Contains information necessary to cancel queries for a session
329-
#[derive(Copy)]
336+
#[derive(Copy, Clone, Show)]
330337
pub struct CancelData {
331338
/// The process ID of the session
332339
pub process_id: u32,
@@ -783,6 +790,16 @@ pub struct Connection {
783790
conn: RefCell<InnerConnection>
784791
}
785792

793+
impl fmt::Show for Connection {
794+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
795+
let conn = self.conn.borrow();
796+
write!(fmt,
797+
"Connection {{ cancel_data: {:?}, notifications: {:?}, transaction_depth: {:?}, \
798+
desynchronized: {:?} }}", conn.cancel_data, conn.notifications.len(),
799+
conn.trans_depth, conn.desynchronized)
800+
}
801+
}
802+
786803
impl Connection {
787804
/// Creates a new connection to a Postgres database.
788805
///
@@ -1056,6 +1073,8 @@ impl Connection {
10561073
}
10571074

10581075
/// Specifies the SSL support requested for a new connection
1076+
// FIXME
1077+
// #[derive(Show)]
10591078
pub enum SslMode {
10601079
/// The connection will not use SSL
10611080
None,
@@ -1075,6 +1094,13 @@ pub struct Transaction<'conn> {
10751094
finished: bool,
10761095
}
10771096

1097+
impl<'a> fmt::Show for Transaction<'a> {
1098+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1099+
write!(fmt, "Transaction {{ connection: {:?}, commit: {:?}, depth: {:?} }}",
1100+
self.conn, self.commit.get(), self.depth)
1101+
}
1102+
}
1103+
10781104
#[unsafe_destructor]
10791105
impl<'conn> Drop for Transaction<'conn> {
10801106
fn drop(&mut self) {
@@ -1205,6 +1231,18 @@ pub struct Statement<'conn> {
12051231
finished: bool,
12061232
}
12071233

1234+
impl<'a> fmt::Show for Statement<'a> {
1235+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1236+
write!(fmt,
1237+
"Statement {{ connection: {:?}, name: {:?}, parameter_types: {:?},
1238+
result_descriptions: {:?} }}",
1239+
self.conn,
1240+
self.name,
1241+
self.param_types,
1242+
self.result_desc)
1243+
}
1244+
}
1245+
12081246
#[unsafe_destructor]
12091247
impl<'conn> Drop for Statement<'conn> {
12101248
fn drop(&mut self) {
@@ -1382,7 +1420,7 @@ impl<'conn> Statement<'conn> {
13821420
}
13831421

13841422
/// Information about a column of the result of a query.
1385-
#[derive(PartialEq, Eq)]
1423+
#[derive(PartialEq, Eq, Clone, Show)]
13861424
pub struct ResultDescription {
13871425
/// The name of the column
13881426
pub name: String,
@@ -1400,6 +1438,13 @@ pub struct Rows<'stmt> {
14001438
finished: bool,
14011439
}
14021440

1441+
impl<'a> fmt::Show for Rows<'a> {
1442+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1443+
write!(fmt, "Rows {{ statement: {:?}, name: {:?}, remaining_rows: {:?} }}",
1444+
self.stmt, self.name, self.data.len())
1445+
}
1446+
}
1447+
14031448
#[unsafe_destructor]
14041449
impl<'stmt> Drop for Rows<'stmt> {
14051450
fn drop(&mut self) {
@@ -1511,6 +1556,12 @@ pub struct Row<'stmt> {
15111556
data: Vec<Option<Vec<u8>>>
15121557
}
15131558

1559+
impl<'a> fmt::Show for Row<'a> {
1560+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1561+
write!(fmt, "Row {{ statement: {:?} }}", self.stmt)
1562+
}
1563+
}
1564+
15141565
impl<'stmt> Row<'stmt> {
15151566
/// Returns the number of values in the row
15161567
pub fn len(&self) -> usize {
@@ -1594,6 +1645,19 @@ pub struct LazyRows<'trans, 'stmt> {
15941645
_trans: &'trans Transaction<'trans>,
15951646
}
15961647

1648+
impl<'a, 'b> fmt::Show for LazyRows<'a, 'b> {
1649+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1650+
write!(fmt,
1651+
"LazyRows {{ statement: {:?}, name: {:?}, row_limit: {:?}, remaining_rows: {:?}, \
1652+
more_rows: {:?} }}",
1653+
self.result.stmt,
1654+
self.result.name,
1655+
self.result.row_limit,
1656+
self.result.data.len(),
1657+
self.result.more_rows)
1658+
}
1659+
}
1660+
15971661
impl<'trans, 'stmt> LazyRows<'trans, 'stmt> {
15981662
/// Like `Rows::finish`.
15991663
pub fn finish(self) -> Result<()> {
@@ -1621,6 +1685,13 @@ pub struct CopyInStatement<'a> {
16211685
finished: bool,
16221686
}
16231687

1688+
impl<'a> fmt::Show for CopyInStatement<'a> {
1689+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1690+
write!(fmt, "CopyInStatement {{ connection: {:?}, name: {:?}, column_types: {:?} }}",
1691+
self.conn, self.name, self.column_types)
1692+
}
1693+
}
1694+
16241695
#[unsafe_destructor]
16251696
impl<'a> Drop for CopyInStatement<'a> {
16261697
fn drop(&mut self) {
@@ -1710,7 +1781,7 @@ impl<'a> CopyInStatement<'a> {
17101781
// FIXME this is not the right way to handle this
17111782
try_desync!(conn, conn.stream.write_message(
17121783
&CopyFail {
1713-
message: &*format!("{:?}", err),
1784+
message: &*format!("{}", err),
17141785
}));
17151786
break 'l;
17161787
}

0 commit comments

Comments
 (0)