Skip to content

Commit 4bdbcd8

Browse files
committed
Impl NegotiateSsl for closures + rustfmt
1 parent a217c5c commit 4bdbcd8

8 files changed

Lines changed: 113 additions & 77 deletions

File tree

src/error.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ impl DbErrorNew for DbError {
4545
hint: map.remove(&b'H'),
4646
position: match map.remove(&b'P') {
4747
Some(pos) => Some(ErrorPosition::Normal(try!(pos.parse().map_err(|_| ())))),
48-
None => match map.remove(&b'p') {
49-
Some(pos) => Some(ErrorPosition::Internal {
50-
position: try!(pos.parse().map_err(|_| ())),
51-
query: try!(map.remove(&b'q').ok_or(())),
52-
}),
53-
None => None,
54-
},
48+
None => {
49+
match map.remove(&b'p') {
50+
Some(pos) => {
51+
Some(ErrorPosition::Internal {
52+
position: try!(pos.parse().map_err(|_| ())),
53+
query: try!(map.remove(&b'q').ok_or(())),
54+
})
55+
}
56+
None => None,
57+
}
58+
}
5559
},
5660
where_: map.remove(&b'W'),
5761
schema: map.remove(&b's'),
@@ -231,8 +235,9 @@ impl error::Error for ConnectError {
231235
ConnectError::BadConnectParams(_) => "Error creating `ConnectParams`",
232236
ConnectError::MissingUser => "User missing in `ConnectParams`",
233237
ConnectError::DbError(_) => "Error reported by Postgres",
234-
ConnectError::MissingPassword =>
235-
"The server requested a password but none was provided",
238+
ConnectError::MissingPassword => {
239+
"The server requested a password but none was provided"
240+
}
236241
ConnectError::UnsupportedAuthentication => {
237242
"The server requested an unsupported authentication method"
238243
}

src/io/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pub trait StreamWrapper: Read+Write+Send {
2525
///
2626
/// If the `security-framework` Cargo feature is enabled, this trait will be
2727
/// implemented for `security_framework::secure_transport::ClientBuilder`.
28+
///
29+
/// It is also implemented for `Fn(&str, Stream) -> Result<Box<StreamWrapper>,
30+
/// Box<Error + Sync + Send>` closures.
2831
pub trait NegotiateSsl {
2932
/// Negotiates an SSL session, returning a wrapper around the provided
3033
/// stream.
@@ -36,3 +39,14 @@ pub trait NegotiateSsl {
3639
stream: Stream)
3740
-> Result<Box<StreamWrapper>, Box<Error + Sync + Send>>;
3841
}
42+
43+
impl<F> NegotiateSsl for F
44+
where F: Fn(&str, Stream) -> Result<Box<StreamWrapper>, Box<Error + Sync + Send>>
45+
{
46+
fn negotiate_ssl(&self,
47+
host: &str,
48+
stream: Stream)
49+
-> Result<Box<StreamWrapper>, Box<Error + Sync + Send>> {
50+
(*self)(host, stream)
51+
}
52+
}

src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ impl InnerConnection {
477477
WHERE t.oid = $1") {
478478
Ok(..) => return Ok(()),
479479
Err(Error::IoError(e)) => return Err(ConnectError::IoError(e)),
480-
// Range types weren't added until Postgres 9.2, so pg_range may not exist
480+
// Range types weren't added until Postgres 9.2, so pg_range may not exist
481481
Err(Error::DbError(ref e)) if e.code() == &SqlState::UndefinedTable => {}
482482
Err(Error::DbError(e)) => return Err(ConnectError::DbError(e)),
483483
_ => unreachable!(),
@@ -825,8 +825,7 @@ impl InnerConnection {
825825
}
826826

827827
fn _ensure_send() {
828-
fn _is_send<T: Send>() {
829-
}
828+
fn _is_send<T: Send>() {}
830829
_is_send::<Connection>();
831830
}
832831

src/md5.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ struct StepUp<T> {
2121
ammount: T,
2222
}
2323

24-
impl <T> Iterator for StepUp<T> where
25-
T: Add<T, Output = T> + PartialOrd + Copy {
24+
impl<T> Iterator for StepUp<T> where T: Add<T, Output = T> + PartialOrd + Copy
25+
{
2626
type Item = T;
2727

2828
#[inline]
@@ -41,8 +41,8 @@ trait RangeExt<T> {
4141
fn step_up(self, ammount: T) -> StepUp<T>;
4242
}
4343

44-
impl <T> RangeExt<T> for Range<T> where
45-
T: Add<T, Output = T> + PartialOrd + Copy {
44+
impl<T> RangeExt<T> for Range<T> where T: Add<T, Output = T> + PartialOrd + Copy
45+
{
4646
fn step_up(self, ammount: T) -> StepUp<T> {
4747
StepUp {
4848
next: self.start,
@@ -108,7 +108,7 @@ trait StandardPadding {
108108
fn standard_padding<F: FnMut(&[u8])>(&mut self, rem: usize, func: F);
109109
}
110110

111-
impl <T: FixedBuffer> StandardPadding for T {
111+
impl<T: FixedBuffer> StandardPadding for T {
112112
fn standard_padding<F: FnMut(&[u8])>(&mut self, rem: usize, mut func: F) {
113113
let size = self.size();
114114

src/message.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,13 @@ impl<R: BufRead + ReadTimeout> ReadMessage for R {
323323
b'1' => ParseComplete,
324324
b'2' => BindComplete,
325325
b'3' => CloseComplete,
326-
b'A' => NotificationResponse {
327-
pid: try!(rdr.read_u32::<BigEndian>()),
328-
channel: try!(rdr.read_cstr()),
329-
payload: try!(rdr.read_cstr()),
330-
},
326+
b'A' => {
327+
NotificationResponse {
328+
pid: try!(rdr.read_u32::<BigEndian>()),
329+
channel: try!(rdr.read_cstr()),
330+
payload: try!(rdr.read_cstr()),
331+
}
332+
}
331333
b'c' => BCopyDone,
332334
b'C' => CommandComplete { tag: try!(rdr.read_cstr()) },
333335
b'd' => {
@@ -360,23 +362,29 @@ impl<R: BufRead + ReadTimeout> ReadMessage for R {
360362
}
361363
}
362364
b'I' => EmptyQueryResponse,
363-
b'K' => BackendKeyData {
364-
process_id: try!(rdr.read_u32::<BigEndian>()),
365-
secret_key: try!(rdr.read_u32::<BigEndian>()),
366-
},
365+
b'K' => {
366+
BackendKeyData {
367+
process_id: try!(rdr.read_u32::<BigEndian>()),
368+
secret_key: try!(rdr.read_u32::<BigEndian>()),
369+
}
370+
}
367371
b'n' => NoData,
368372
b'N' => NoticeResponse { fields: try!(read_fields(&mut rdr)) },
369373
b'R' => try!(read_auth_message(&mut rdr)),
370374
b's' => PortalSuspended,
371-
b'S' => ParameterStatus {
372-
parameter: try!(rdr.read_cstr()),
373-
value: try!(rdr.read_cstr()),
374-
},
375+
b'S' => {
376+
ParameterStatus {
377+
parameter: try!(rdr.read_cstr()),
378+
value: try!(rdr.read_cstr()),
379+
}
380+
}
375381
b't' => try!(read_parameter_description(&mut rdr)),
376382
b'T' => try!(read_row_description(&mut rdr)),
377383
b'Z' => ReadyForQuery { _state: try!(rdr.read_u8()) },
378-
t => return Err(io::Error::new(io::ErrorKind::Other,
379-
format!("unexpected message tag `{}`", t))),
384+
t => {
385+
return Err(io::Error::new(io::ErrorKind::Other,
386+
format!("unexpected message tag `{}`", t)))
387+
}
380388
};
381389
if rdr.limit() != 0 {
382390
return Err(io::Error::new(io::ErrorKind::Other, "didn't read entire message"));
@@ -431,8 +439,10 @@ fn read_auth_message<R: Read>(buf: &mut R) -> io::Result<BackendMessage> {
431439
6 => AuthenticationSCMCredential,
432440
7 => AuthenticationGSS,
433441
9 => AuthenticationSSPI,
434-
t => return Err(io::Error::new(io::ErrorKind::Other,
435-
format!("unexpected authentication tag `{}`", t))),
442+
t => {
443+
return Err(io::Error::new(io::ErrorKind::Other,
444+
format!("unexpected authentication tag `{}`", t)))
445+
}
436446
})
437447
}
438448

src/priv_io.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ pub trait ReadTimeout {
2828
impl ReadTimeout for BufStream<Box<StreamWrapper>> {
2929
fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
3030
match self.get_ref().get_ref().0 {
31-
InternalStream::Tcp(ref s) =>
32-
<TcpStream as TcpStreamExt>::set_read_timeout(s, timeout),
31+
InternalStream::Tcp(ref s) => {
32+
<TcpStream as TcpStreamExt>::set_read_timeout(s, timeout)
33+
}
3334
#[cfg(feature = "unix_socket")]
3435
InternalStream::Unix(ref s) => s.set_read_timeout(timeout),
3536
}

src/types/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,8 @@ pub trait ToSql: fmt::Debug {
827827
fn to_sql_checked(&self, ty: &Type, out: &mut Write, ctx: &SessionInfo) -> Result<IsNull>;
828828
}
829829

830-
impl<'a, T> ToSql for &'a T where T: ToSql {
830+
impl<'a, T> ToSql for &'a T where T: ToSql
831+
{
831832
to_sql_checked!();
832833

833834
fn to_sql<W: Write + ?Sized>(&self,

src/url.rs

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -121,45 +121,49 @@ fn decode_inner(c: &str, full_url: bool) -> DecodeResult<String> {
121121

122122
loop {
123123
match iter.next() {
124-
Some(b) => match b as char {
125-
'%' => {
126-
let bytes = match (iter.next(), iter.next()) {
127-
(Some(one), Some(two)) => [one, two],
128-
_ => return Err(format!("Malformed input: found '%' without two \
129-
trailing bytes")),
130-
};
131-
132-
// Only decode some characters if full_url:
133-
match str::from_utf8(&bytes).unwrap().from_hex().unwrap()[0] as char {
134-
// gen-delims:
135-
':' |
136-
'/' |
137-
'?' |
138-
'#' |
139-
'[' |
140-
']' |
141-
'@' |
142-
'!' |
143-
'$' |
144-
'&' |
145-
'"' |
146-
'(' |
147-
')' |
148-
'*' |
149-
'+' |
150-
',' |
151-
';' |
152-
'=' if full_url => {
153-
out.push('%');
154-
out.push(bytes[0] as char);
155-
out.push(bytes[1] as char);
124+
Some(b) => {
125+
match b as char {
126+
'%' => {
127+
let bytes = match (iter.next(), iter.next()) {
128+
(Some(one), Some(two)) => [one, two],
129+
_ => {
130+
return Err(format!("Malformed input: found '%' without two \
131+
trailing bytes"))
132+
}
133+
};
134+
135+
// Only decode some characters if full_url:
136+
match str::from_utf8(&bytes).unwrap().from_hex().unwrap()[0] as char {
137+
// gen-delims:
138+
':' |
139+
'/' |
140+
'?' |
141+
'#' |
142+
'[' |
143+
']' |
144+
'@' |
145+
'!' |
146+
'$' |
147+
'&' |
148+
'"' |
149+
'(' |
150+
')' |
151+
'*' |
152+
'+' |
153+
',' |
154+
';' |
155+
'=' if full_url => {
156+
out.push('%');
157+
out.push(bytes[0] as char);
158+
out.push(bytes[1] as char);
159+
}
160+
161+
ch => out.push(ch),
156162
}
157-
158-
ch => out.push(ch),
159163
}
164+
ch => out.push(ch),
160165
}
161-
ch => out.push(ch),
162-
},
166+
}
163167
None => return Ok(out),
164168
}
165169
}
@@ -384,10 +388,12 @@ fn get_authority(rawurl: &str) -> DecodeResult<(Option<UserInfo>, &str, Option<u
384388
// If we have a port string, ensure it parses to u16.
385389
let port = match port {
386390
None => None,
387-
opt => match opt.and_then(|p| FromStr::from_str(p).ok()) {
388-
None => return Err(format!("Failed to parse port: {:?}", port)),
389-
opt => opt,
390-
},
391+
opt => {
392+
match opt.and_then(|p| FromStr::from_str(p).ok()) {
393+
None => return Err(format!("Failed to parse port: {:?}", port)),
394+
opt => opt,
395+
}
396+
}
391397
};
392398

393399
Ok((userinfo, host, port, rest))

0 commit comments

Comments
 (0)