Skip to content

Commit 2562d01

Browse files
committed
Shift stuff around
1 parent 2dc80be commit 2562d01

File tree

9 files changed

+124
-120
lines changed

9 files changed

+124
-120
lines changed

postgres-shared/src/lib.rs

Lines changed: 2 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@ extern crate fallible_iterator;
55
extern crate phf;
66
extern crate postgres_protocol;
77

8-
use fallible_iterator::{FallibleIterator, FromFallibleIterator};
9-
use std::ascii::AsciiExt;
10-
use std::ops::Range;
11-
12-
use types::Type;
13-
148
pub mod error;
159
pub mod params;
1610
pub mod types;
11+
pub mod rows;
12+
pub mod stmt;
1713

1814
/// Contains information necessary to cancel queries for a session.
1915
#[derive(Copy, Clone, Debug)]
@@ -23,110 +19,3 @@ pub struct CancelData {
2319
/// The secret key for the session.
2420
pub secret_key: i32,
2521
}
26-
27-
pub struct RowData {
28-
buf: Vec<u8>,
29-
indices: Vec<Option<Range<usize>>>,
30-
}
31-
32-
impl<'a> FromFallibleIterator<Option<&'a [u8]>> for RowData {
33-
fn from_fallible_iterator<I>(mut it: I) -> Result<RowData, I::Error>
34-
where I: FallibleIterator<Item = Option<&'a [u8]>>
35-
{
36-
let mut row = RowData {
37-
buf: vec![],
38-
indices: Vec::with_capacity(it.size_hint().0),
39-
};
40-
41-
while let Some(cell) = try!(it.next()) {
42-
let index = match cell {
43-
Some(cell) => {
44-
let base = row.buf.len();
45-
row.buf.extend_from_slice(cell);
46-
Some(base..row.buf.len())
47-
}
48-
None => None,
49-
};
50-
row.indices.push(index);
51-
}
52-
53-
Ok(row)
54-
}
55-
}
56-
57-
impl RowData {
58-
pub fn len(&self) -> usize {
59-
self.indices.len()
60-
}
61-
62-
pub fn get(&self, index: usize) -> Option<&[u8]> {
63-
match &self.indices[index] {
64-
&Some(ref range) => Some(&self.buf[range.clone()]),
65-
&None => None,
66-
}
67-
}
68-
}
69-
70-
pub struct Column {
71-
name: String,
72-
type_: Type,
73-
}
74-
75-
impl Column {
76-
#[doc(hidden)]
77-
pub fn new(name: String, type_: Type) -> Column {
78-
Column {
79-
name: name,
80-
type_: type_,
81-
}
82-
}
83-
84-
pub fn name(&self) -> &str {
85-
&self.name
86-
}
87-
88-
pub fn type_(&self) -> &Type {
89-
&self.type_
90-
}
91-
}
92-
93-
/// A trait implemented by types that can index into columns of a row.
94-
pub trait RowIndex {
95-
/// Returns the index of the appropriate column, or `None` if no such
96-
/// column exists.
97-
fn idx(&self, stmt: &[Column]) -> Option<usize>;
98-
}
99-
100-
impl RowIndex for usize {
101-
#[inline]
102-
fn idx(&self, stmt: &[Column]) -> Option<usize> {
103-
if *self >= stmt.len() {
104-
None
105-
} else {
106-
Some(*self)
107-
}
108-
}
109-
}
110-
111-
impl<'a> RowIndex for str {
112-
#[inline]
113-
fn idx(&self, stmt: &[Column]) -> Option<usize> {
114-
if let Some(idx) = stmt.iter().position(|d| d.name() == self) {
115-
return Some(idx);
116-
};
117-
118-
// FIXME ASCII-only case insensitivity isn't really the right thing to
119-
// do. Postgres itself uses a dubious wrapper around tolower and JDBC
120-
// uses the US locale.
121-
stmt.iter().position(|d| d.name().eq_ignore_ascii_case(self))
122-
}
123-
}
124-
125-
impl<'a, T: ?Sized> RowIndex for &'a T
126-
where T: RowIndex
127-
{
128-
#[inline]
129-
fn idx(&self, columns: &[Column]) -> Option<usize> {
130-
T::idx(*self, columns)
131-
}
132-
}

postgres-shared/src/rows.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use fallible_iterator::{FallibleIterator, FromFallibleIterator};
2+
use std::ascii::AsciiExt;
3+
use std::ops::Range;
4+
5+
use stmt::Column;
6+
7+
/// A trait implemented by types that can index into columns of a row.
8+
pub trait RowIndex {
9+
/// Returns the index of the appropriate column, or `None` if no such
10+
/// column exists.
11+
fn idx(&self, stmt: &[Column]) -> Option<usize>;
12+
}
13+
14+
impl RowIndex for usize {
15+
#[inline]
16+
fn idx(&self, stmt: &[Column]) -> Option<usize> {
17+
if *self >= stmt.len() {
18+
None
19+
} else {
20+
Some(*self)
21+
}
22+
}
23+
}
24+
25+
impl<'a> RowIndex for str {
26+
#[inline]
27+
fn idx(&self, stmt: &[Column]) -> Option<usize> {
28+
if let Some(idx) = stmt.iter().position(|d| d.name() == self) {
29+
return Some(idx);
30+
};
31+
32+
// FIXME ASCII-only case insensitivity isn't really the right thing to
33+
// do. Postgres itself uses a dubious wrapper around tolower and JDBC
34+
// uses the US locale.
35+
stmt.iter().position(|d| d.name().eq_ignore_ascii_case(self))
36+
}
37+
}
38+
39+
impl<'a, T: ?Sized> RowIndex for &'a T
40+
where T: RowIndex
41+
{
42+
#[inline]
43+
fn idx(&self, columns: &[Column]) -> Option<usize> {
44+
T::idx(*self, columns)
45+
}
46+
}
47+
48+
#[doc(hidden)]
49+
pub struct RowData {
50+
buf: Vec<u8>,
51+
indices: Vec<Option<Range<usize>>>,
52+
}
53+
54+
impl<'a> FromFallibleIterator<Option<&'a [u8]>> for RowData {
55+
fn from_fallible_iterator<I>(mut it: I) -> Result<RowData, I::Error>
56+
where I: FallibleIterator<Item = Option<&'a [u8]>>
57+
{
58+
let mut row = RowData {
59+
buf: vec![],
60+
indices: Vec::with_capacity(it.size_hint().0),
61+
};
62+
63+
while let Some(cell) = try!(it.next()) {
64+
let index = match cell {
65+
Some(cell) => {
66+
let base = row.buf.len();
67+
row.buf.extend_from_slice(cell);
68+
Some(base..row.buf.len())
69+
}
70+
None => None,
71+
};
72+
row.indices.push(index);
73+
}
74+
75+
Ok(row)
76+
}
77+
}
78+
79+
impl RowData {
80+
pub fn len(&self) -> usize {
81+
self.indices.len()
82+
}
83+
84+
pub fn get(&self, index: usize) -> Option<&[u8]> {
85+
match &self.indices[index] {
86+
&Some(ref range) => Some(&self.buf[range.clone()]),
87+
&None => None,
88+
}
89+
}
90+
}

postgres-shared/src/stmt.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use types::Type;
2+
3+
pub struct Column {
4+
name: String,
5+
type_: Type,
6+
}
7+
8+
impl Column {
9+
#[doc(hidden)]
10+
pub fn new(name: String, type_: Type) -> Column {
11+
Column {
12+
name: name,
13+
type_: type_,
14+
}
15+
}
16+
17+
pub fn name(&self) -> &str {
18+
&self.name
19+
}
20+
21+
pub fn type_(&self) -> &Type {
22+
&self.type_
23+
}
24+
}

postgres-tokio/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use futures_state_stream::{StreamEvent, StateStream, BoxStateStream, FutureExt};
2222
use postgres_protocol::authentication;
2323
use postgres_protocol::message::{backend, frontend};
2424
use postgres_protocol::message::backend::{ErrorResponseBody, ErrorFields};
25-
use postgres_shared::RowData;
25+
use postgres_shared::rows::RowData;
2626
use std::collections::HashMap;
2727
use std::fmt;
2828
use std::io;

postgres-tokio/src/rows.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//! Postgres rows.
22
3-
use postgres_shared::{RowData, Column};
3+
use postgres_shared::rows::RowData;
4+
use postgres_shared::stmt::Column;
45
use std::collections::HashMap;
56
use std::error::Error;
67
use std::fmt;
78
use std::sync::Arc;
89

910
#[doc(inline)]
10-
pub use postgres_shared::RowIndex;
11+
pub use postgres_shared::rows::RowIndex;
1112

1213
use RowNew;
1314
use types::{WrongType, FromSql, SessionInfo};

postgres-tokio/src/stmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55
use std::sync::mpsc::Sender;
66

77
#[doc(inline)]
8-
pub use postgres_shared::Column;
8+
pub use postgres_shared::stmt::Column;
99

1010
use StatementNew;
1111
use types::Type;

postgres/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ use std::time::Duration;
9191
use postgres_protocol::authentication;
9292
use postgres_protocol::message::backend::{self, ErrorFields};
9393
use postgres_protocol::message::frontend;
94-
use postgres_shared::RowData;
94+
use postgres_shared::rows::RowData;
9595

9696
use error::{Error, ConnectError, SqlState, DbError};
9797
use tls::TlsHandshake;

postgres/src/rows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use fallible_iterator::FallibleIterator;
44
use postgres_protocol::message::frontend;
5-
use postgres_shared::RowData;
5+
use postgres_shared::rows::RowData;
66
use std::ascii::AsciiExt;
77
use std::collections::VecDeque;
88
use std::fmt;

postgres/src/stmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::fmt;
77
use std::io::{self, Read, Write};
88
use std::sync::Arc;
99
use postgres_protocol::message::{backend, frontend};
10-
use postgres_shared::RowData;
10+
use postgres_shared::rows::RowData;
1111

1212
use error::Error;
1313
use types::{SessionInfo, Type, ToSql};

0 commit comments

Comments
 (0)