use crate::ast::Row;
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Values<'a> {
pub(crate) rows: Vec<Row<'a>>,
}
impl<'a> Values<'a> {
pub fn empty() -> Self {
Self { rows: Vec::new() }
}
pub fn new(rows: Vec<Row<'a>>) -> Self {
Self { rows }
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
rows: Vec::with_capacity(capacity),
}
}
pub fn push<T>(&mut self, row: T)
where
T: Into<Row<'a>>,
{
self.rows.push(row.into());
}
pub fn len(&self) -> usize {
self.rows.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn row_len(&self) -> usize {
match self.rows.split_first() {
Some((row, _)) => row.len(),
None => 0,
}
}
pub fn flatten_row(self) -> Option<Row<'a>> {
let mut result = Row::with_capacity(self.len());
for mut row in self.rows.into_iter() {
match row.pop() {
Some(value) => result.push(value),
None => return None,
}
}
Some(result)
}
}
impl<'a, I, R> From<I> for Values<'a>
where
I: Iterator<Item = R>,
R: Into<Row<'a>>,
{
fn from(rows: I) -> Self {
Self {
rows: rows.map(|r| r.into()).collect(),
}
}
}
impl<'a> IntoIterator for Values<'a> {
type Item = Row<'a>;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.rows.into_iter()
}
}