forked from rust-postgres/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_rows.rs
More file actions
177 lines (149 loc) · 3.92 KB
/
Copy pathtext_rows.rs
File metadata and controls
177 lines (149 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! Query result rows.
use postgres_shared::rows::RowData;
use std::fmt;
use std::slice;
#[doc(inline)]
pub use postgres_shared::rows::RowIndex;
use stmt::{Column};
/// The resulting rows of a query.
pub struct TextRows {
columns: Vec<Column>,
data: Vec<RowData>,
}
impl fmt::Debug for TextRows {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("TextRows")
.field("columns", &self.columns())
.field("rows", &self.data.len())
.finish()
}
}
impl TextRows {
pub(crate) fn new(columns: Vec<Column>, data: Vec<RowData>) -> TextRows {
TextRows {
columns: columns,
data: data,
}
}
/// Returns a slice describing the columns of the `TextRows`.
pub fn columns(&self) -> &[Column] {
&self.columns[..]
}
/// Returns the number of rows present.
pub fn len(&self) -> usize {
self.data.len()
}
/// Determines if there are any rows present.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Returns a specific `TextRow`.
///
/// # Panics
///
/// Panics if `idx` is out of bounds.
pub fn get<'a>(&'a self, idx: usize) -> TextRow<'a> {
TextRow {
columns: &self.columns,
data: &self.data[idx],
}
}
/// Returns an iterator over the `TextRow`s.
pub fn iter<'a>(&'a self) -> Iter<'a> {
Iter {
columns: self.columns(),
iter: self.data.iter(),
}
}
}
impl<'a> IntoIterator for &'a TextRows {
type Item = TextRow<'a>;
type IntoIter = Iter<'a>;
fn into_iter(self) -> Iter<'a> {
self.iter()
}
}
/// An iterator over `TextRow`s.
pub struct Iter<'a> {
columns: &'a [Column],
iter: slice::Iter<'a, RowData>,
}
impl<'a> Iterator for Iter<'a> {
type Item = TextRow<'a>;
fn next(&mut self) -> Option<TextRow<'a>> {
self.iter.next().map(|row| {
TextRow {
columns: self.columns,
data: row,
}
})
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a> DoubleEndedIterator for Iter<'a> {
fn next_back(&mut self) -> Option<TextRow<'a>> {
self.iter.next_back().map(|row| {
TextRow {
columns: self.columns,
data: row,
}
})
}
}
impl<'a> ExactSizeIterator for Iter<'a> {}
/// A single result row of a query.
pub struct TextRow<'a> {
columns: &'a [Column],
data: &'a RowData,
}
impl<'a> fmt::Debug for TextRow<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("TextRow")
.field("columns", &self.columns)
.finish()
}
}
impl<'a> TextRow<'a> {
/// Returns the number of values in the row.
pub fn len(&self) -> usize {
self.data.len()
}
/// Determines if there are any values in the row.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Returns a slice describing the columns of the `TextRow`.
pub fn columns(&self) -> &[Column] {
self.columns
}
/// stub
pub fn get<I>(&self, idx: I) -> Option<&str>
where
I: RowIndex + fmt::Debug,
{
match self.get_inner(&idx) {
Some(value) => value,
None => panic!("no such column {:?}", idx),
}
}
/// stub
pub fn get_opt<I>(&self, idx: I) -> Option<Option<&str>>
where
I: RowIndex,
{
self.get_inner(&idx)
}
fn get_inner<I>(&self, idx: &I) -> Option<Option<&str>>
where
I: RowIndex,
{
let idx = match idx.__idx(self.columns) {
Some(idx) => idx,
None => return None,
};
// TODO can we assume these values will always be utf8?
Some(self.data.get(idx).map(|s| ::std::str::from_utf8(s).expect("utf8 encoded")))
}
}