Unmangle ConnectTarget and MaybeSslStream
This commit is contained in:
parent
ad82d28d18
commit
a442b2ed4c
28
src/io.rs
28
src/io.rs
@ -4,7 +4,7 @@ use std::io::net::tcp;
|
|||||||
use std::io::net::pipe;
|
use std::io::net::pipe;
|
||||||
use std::io::{Stream, IoResult};
|
use std::io::{Stream, IoResult};
|
||||||
|
|
||||||
use {ConnectParams, SslMode, NoSsl, PreferSsl, RequireSsl, TargetTcp, TargetUnix};
|
use {ConnectParams, SslMode, NoSsl, PreferSsl, RequireSsl, ConnectTarget};
|
||||||
use error::{PostgresConnectError, PgConnectStreamError, NoSslSupport, SslError, SocketError};
|
use error::{PostgresConnectError, PgConnectStreamError, NoSslSupport, SslError, SocketError};
|
||||||
use message;
|
use message;
|
||||||
use message::{SslRequest, WriteMessage};
|
use message::{SslRequest, WriteMessage};
|
||||||
@ -12,15 +12,15 @@ use message::{SslRequest, WriteMessage};
|
|||||||
const DEFAULT_PORT: Port = 5432;
|
const DEFAULT_PORT: Port = 5432;
|
||||||
|
|
||||||
pub enum MaybeSslStream<S> {
|
pub enum MaybeSslStream<S> {
|
||||||
SslStream(ssl::SslStream<S>),
|
Ssl(ssl::SslStream<S>),
|
||||||
NormalStream(S),
|
Normal(S),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Stream> Reader for MaybeSslStream<S> {
|
impl<S: Stream> Reader for MaybeSslStream<S> {
|
||||||
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
||||||
match *self {
|
match *self {
|
||||||
SslStream(ref mut s) => s.read(buf),
|
MaybeSslStream::Ssl(ref mut s) => s.read(buf),
|
||||||
NormalStream(ref mut s) => s.read(buf),
|
MaybeSslStream::Normal(ref mut s) => s.read(buf),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -28,15 +28,15 @@ impl<S: Stream> Reader for MaybeSslStream<S> {
|
|||||||
impl<S: Stream> Writer for MaybeSslStream<S> {
|
impl<S: Stream> Writer for MaybeSslStream<S> {
|
||||||
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
||||||
match *self {
|
match *self {
|
||||||
SslStream(ref mut s) => s.write(buf),
|
MaybeSslStream::Ssl(ref mut s) => s.write(buf),
|
||||||
NormalStream(ref mut s) => s.write(buf),
|
MaybeSslStream::Normal(ref mut s) => s.write(buf),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&mut self) -> IoResult<()> {
|
fn flush(&mut self) -> IoResult<()> {
|
||||||
match *self {
|
match *self {
|
||||||
SslStream(ref mut s) => s.flush(),
|
MaybeSslStream::Ssl(ref mut s) => s.flush(),
|
||||||
NormalStream(ref mut s) => s.flush(),
|
MaybeSslStream::Normal(ref mut s) => s.flush(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -75,9 +75,9 @@ fn open_socket(params: &ConnectParams)
|
|||||||
-> Result<InternalStream, PostgresConnectError> {
|
-> Result<InternalStream, PostgresConnectError> {
|
||||||
let port = params.port.unwrap_or(DEFAULT_PORT);
|
let port = params.port.unwrap_or(DEFAULT_PORT);
|
||||||
let socket = match params.target {
|
let socket = match params.target {
|
||||||
TargetTcp(ref host) =>
|
ConnectTarget::Tcp(ref host) =>
|
||||||
tcp::TcpStream::connect(host[], port).map(TcpStream),
|
tcp::TcpStream::connect(host[], port).map(TcpStream),
|
||||||
TargetUnix(ref path) => {
|
ConnectTarget::Unix(ref path) => {
|
||||||
let mut path = path.clone();
|
let mut path = path.clone();
|
||||||
path.push(format!(".s.PGSQL.{}", port));
|
path.push(format!(".s.PGSQL.{}", port));
|
||||||
pipe::UnixStream::connect(&path).map(UnixStream)
|
pipe::UnixStream::connect(&path).map(UnixStream)
|
||||||
@ -91,7 +91,7 @@ pub fn initialize_stream(params: &ConnectParams, ssl: &SslMode)
|
|||||||
let mut socket = try!(open_socket(params));
|
let mut socket = try!(open_socket(params));
|
||||||
|
|
||||||
let (ssl_required, ctx) = match *ssl {
|
let (ssl_required, ctx) = match *ssl {
|
||||||
NoSsl => return Ok(NormalStream(socket)),
|
NoSsl => return Ok(MaybeSslStream::Normal(socket)),
|
||||||
PreferSsl(ref ctx) => (false, ctx),
|
PreferSsl(ref ctx) => (false, ctx),
|
||||||
RequireSsl(ref ctx) => (true, ctx)
|
RequireSsl(ref ctx) => (true, ctx)
|
||||||
};
|
};
|
||||||
@ -103,12 +103,12 @@ pub fn initialize_stream(params: &ConnectParams, ssl: &SslMode)
|
|||||||
if ssl_required {
|
if ssl_required {
|
||||||
return Err(NoSslSupport);
|
return Err(NoSslSupport);
|
||||||
} else {
|
} else {
|
||||||
return Ok(NormalStream(socket));
|
return Ok(MaybeSslStream::Normal(socket));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match ssl::SslStream::new(ctx, socket) {
|
match ssl::SslStream::new(ctx, socket) {
|
||||||
Ok(stream) => Ok(SslStream(stream)),
|
Ok(stream) => Ok(MaybeSslStream::Ssl(stream)),
|
||||||
Err(err) => Err(SslError(err))
|
Err(err) => Err(SslError(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
src/lib.rs
12
src/lib.rs
@ -155,9 +155,9 @@ pub type Result<T> = result::Result<T, PostgresError>;
|
|||||||
#[deriving(Clone)]
|
#[deriving(Clone)]
|
||||||
pub enum ConnectTarget {
|
pub enum ConnectTarget {
|
||||||
/// Connect via TCP to the specified host.
|
/// Connect via TCP to the specified host.
|
||||||
TargetTcp(String),
|
Tcp(String),
|
||||||
/// Connect via a Unix domain socket in the specified directory.
|
/// Connect via a Unix domain socket in the specified directory.
|
||||||
TargetUnix(Path)
|
Unix(Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Authentication information
|
/// Authentication information
|
||||||
@ -221,9 +221,9 @@ impl IntoConnectParams for Url {
|
|||||||
|
|
||||||
let maybe_path = try!(url::decode_component(host[]).map_err(InvalidUrl));
|
let maybe_path = try!(url::decode_component(host[]).map_err(InvalidUrl));
|
||||||
let target = if maybe_path[].starts_with("/") {
|
let target = if maybe_path[].starts_with("/") {
|
||||||
TargetUnix(Path::new(maybe_path))
|
ConnectTarget::Unix(Path::new(maybe_path))
|
||||||
} else {
|
} else {
|
||||||
TargetTcp(host)
|
ConnectTarget::Tcp(host)
|
||||||
};
|
};
|
||||||
|
|
||||||
let user = user.map(|url::UserInfo { user, pass }| {
|
let user = user.map(|url::UserInfo { user, pass }| {
|
||||||
@ -737,11 +737,11 @@ impl Connection {
|
|||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use postgres::{Connection, UserInfo, ConnectParams, NoSsl, TargetUnix};
|
/// # use postgres::{Connection, UserInfo, ConnectParams, NoSsl, ConnectTarget};
|
||||||
/// # let _ = || {
|
/// # let _ = || {
|
||||||
/// # let some_crazy_path = Path::new("");
|
/// # let some_crazy_path = Path::new("");
|
||||||
/// let params = ConnectParams {
|
/// let params = ConnectParams {
|
||||||
/// target: TargetUnix(some_crazy_path),
|
/// target: ConnectTarget::Unix(some_crazy_path),
|
||||||
/// port: None,
|
/// port: None,
|
||||||
/// user: Some(UserInfo {
|
/// user: Some(UserInfo {
|
||||||
/// user: "postgres".into_string(),
|
/// user: "postgres".into_string(),
|
||||||
|
Loading…
Reference in New Issue
Block a user