2018-12-17 02:11:52 +00:00
|
|
|
use std::collections::hash_map::{self, HashMap};
|
2018-12-14 05:03:47 +00:00
|
|
|
use std::iter;
|
|
|
|
use std::str::{self, FromStr};
|
2018-11-27 06:45:14 +00:00
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
|
|
|
|
2018-12-09 01:40:37 +00:00
|
|
|
use crate::proto::ConnectFuture;
|
2018-12-14 05:03:47 +00:00
|
|
|
use crate::{Connect, Error, TlsMode};
|
2018-11-27 06:45:14 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Builder {
|
|
|
|
params: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
2018-12-10 05:23:31 +00:00
|
|
|
impl Default for Builder {
|
|
|
|
fn default() -> Builder {
|
|
|
|
Builder::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 06:45:14 +00:00
|
|
|
impl Builder {
|
|
|
|
pub fn new() -> Builder {
|
|
|
|
let mut params = HashMap::new();
|
|
|
|
params.insert("client_encoding".to_string(), "UTF8".to_string());
|
|
|
|
params.insert("timezone".to_string(), "GMT".to_string());
|
|
|
|
|
2018-12-14 05:03:47 +00:00
|
|
|
Builder { params }
|
2018-11-27 06:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn user(&mut self, user: &str) -> &mut Builder {
|
|
|
|
self.param("user", user)
|
|
|
|
}
|
|
|
|
|
2018-12-14 05:03:47 +00:00
|
|
|
pub fn dbname(&mut self, database: &str) -> &mut Builder {
|
|
|
|
self.param("dbname", database)
|
2018-11-27 06:45:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 05:03:47 +00:00
|
|
|
pub fn password(&mut self, password: &str) -> &mut Builder {
|
|
|
|
self.param("password", password)
|
2018-11-27 06:45:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 05:03:47 +00:00
|
|
|
pub fn param(&mut self, key: &str, value: &str) -> &mut Builder {
|
|
|
|
self.params.insert(key.to_string(), value.to_string());
|
2018-11-27 06:45:14 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-12-17 02:11:52 +00:00
|
|
|
/// FIXME do we want this?
|
|
|
|
pub fn iter(&self) -> Iter<'_> {
|
|
|
|
Iter(self.params.iter())
|
|
|
|
}
|
|
|
|
|
2018-11-27 06:45:14 +00:00
|
|
|
pub fn connect<S, T>(&self, stream: S, tls_mode: T) -> Connect<S, T>
|
|
|
|
where
|
|
|
|
S: AsyncRead + AsyncWrite,
|
|
|
|
T: TlsMode<S>,
|
|
|
|
{
|
2018-12-14 05:03:47 +00:00
|
|
|
Connect(ConnectFuture::new(stream, tls_mode, self.params.clone()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Builder {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Builder, Error> {
|
|
|
|
let mut parser = Parser::new(s);
|
|
|
|
let mut builder = Builder::new();
|
|
|
|
|
|
|
|
while let Some((key, value)) = parser.parameter()? {
|
2018-12-17 02:11:52 +00:00
|
|
|
builder.params.insert(key.to_string(), value);
|
2018-12-14 05:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(builder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 02:11:52 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Iter<'a>(hash_map::Iter<'a, String, String>);
|
|
|
|
|
|
|
|
impl<'a> Iterator for Iter<'a> {
|
|
|
|
type Item = (&'a str, &'a str);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<(&'a str, &'a str)> {
|
|
|
|
self.0.next().map(|(k, v)| (&**k, &**v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ExactSizeIterator for Iter<'a> {
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.0.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 05:03:47 +00:00
|
|
|
struct Parser<'a> {
|
|
|
|
s: &'a str,
|
|
|
|
it: iter::Peekable<str::CharIndices<'a>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Parser<'a> {
|
|
|
|
fn new(s: &'a str) -> Parser<'a> {
|
|
|
|
Parser {
|
|
|
|
s,
|
|
|
|
it: s.char_indices().peekable(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn skip_ws(&mut self) {
|
2018-12-17 02:11:52 +00:00
|
|
|
self.take_while(|c| c.is_whitespace());
|
2018-12-14 05:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn take_while<F>(&mut self, f: F) -> &'a str
|
|
|
|
where
|
|
|
|
F: Fn(char) -> bool,
|
|
|
|
{
|
|
|
|
let start = match self.it.peek() {
|
|
|
|
Some(&(i, _)) => i,
|
|
|
|
None => return "",
|
|
|
|
};
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match self.it.peek() {
|
|
|
|
Some(&(_, c)) if f(c) => {
|
|
|
|
self.it.next();
|
|
|
|
}
|
|
|
|
Some(&(i, _)) => return &self.s[start..i],
|
|
|
|
None => return &self.s[start..],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eat(&mut self, target: char) -> Result<(), Error> {
|
|
|
|
match self.it.next() {
|
|
|
|
Some((_, c)) if c == target => Ok(()),
|
|
|
|
Some((i, c)) => {
|
|
|
|
let m = format!(
|
|
|
|
"unexpected character at byte {}: expected `{}` but got `{}`",
|
|
|
|
i, target, c
|
|
|
|
);
|
|
|
|
Err(Error::connection_syntax(m.into()))
|
|
|
|
}
|
|
|
|
None => Err(Error::connection_syntax("unexpected EOF".into())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eat_if(&mut self, target: char) -> bool {
|
|
|
|
match self.it.peek() {
|
|
|
|
Some(&(_, c)) if c == target => {
|
|
|
|
self.it.next();
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn keyword(&mut self) -> Option<&'a str> {
|
|
|
|
let s = self.take_while(|c| match c {
|
2018-12-17 02:11:52 +00:00
|
|
|
c if c.is_whitespace() => false,
|
|
|
|
'=' => false,
|
2018-12-14 05:03:47 +00:00
|
|
|
_ => true,
|
|
|
|
});
|
|
|
|
|
|
|
|
if s.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 02:11:52 +00:00
|
|
|
fn value(&mut self) -> Result<String, Error> {
|
|
|
|
let value = if self.eat_if('\'') {
|
|
|
|
let value = self.quoted_value()?;
|
2018-12-14 05:03:47 +00:00
|
|
|
self.eat('\'')?;
|
2018-12-17 02:11:52 +00:00
|
|
|
value
|
2018-12-14 05:03:47 +00:00
|
|
|
} else {
|
2018-12-17 02:11:52 +00:00
|
|
|
self.simple_value()?
|
2018-12-14 05:03:47 +00:00
|
|
|
};
|
|
|
|
|
2018-12-17 02:11:52 +00:00
|
|
|
Ok(value)
|
2018-12-14 05:03:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 02:11:52 +00:00
|
|
|
fn simple_value(&mut self) -> Result<String, Error> {
|
|
|
|
let mut value = String::new();
|
|
|
|
|
|
|
|
while let Some(&(_, c)) = self.it.peek() {
|
|
|
|
if c.is_whitespace() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.it.next();
|
|
|
|
if c == '\\' {
|
|
|
|
if let Some((_, c2)) = self.it.next() {
|
|
|
|
value.push(c2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
value.push(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if value.is_empty() {
|
|
|
|
return Err(Error::connection_syntax("unexpected EOF".into()));
|
2018-12-14 05:03:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 02:11:52 +00:00
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn quoted_value(&mut self) -> Result<String, Error> {
|
|
|
|
let mut value = String::new();
|
|
|
|
|
|
|
|
while let Some(&(_, c)) = self.it.peek() {
|
|
|
|
if c == '\'' {
|
|
|
|
return Ok(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.it.next();
|
|
|
|
if c == '\\' {
|
|
|
|
if let Some((_, c2)) = self.it.next() {
|
|
|
|
value.push(c2);
|
2018-12-14 05:03:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-12-17 02:11:52 +00:00
|
|
|
value.push(c);
|
|
|
|
}
|
2018-12-14 05:03:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 02:11:52 +00:00
|
|
|
Err(Error::connection_syntax(
|
|
|
|
"unterminated quoted connection parameter value".into(),
|
|
|
|
))
|
2018-12-14 05:03:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 02:11:52 +00:00
|
|
|
fn parameter(&mut self) -> Result<Option<(&'a str, String)>, Error> {
|
2018-12-14 05:03:47 +00:00
|
|
|
self.skip_ws();
|
|
|
|
let keyword = match self.keyword() {
|
|
|
|
Some(keyword) => keyword,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
self.skip_ws();
|
|
|
|
self.eat('=')?;
|
|
|
|
self.skip_ws();
|
|
|
|
let value = self.value()?;
|
|
|
|
|
|
|
|
Ok(Some((keyword, value)))
|
2018-11-27 06:45:14 +00:00
|
|
|
}
|
|
|
|
}
|