use crate::error::CassandraResult;
#[derive(Debug, Default, Clone)]
pub struct Row {
pub(crate) columns: Vec<(String, Vec<u8>)>,
}
impl Row {
pub fn empty() -> Self {
Self::default()
}
pub fn column_bytes(&self, name: &str) -> Option<&[u8]> {
self.columns
.iter()
.find(|(n, _)| n == name)
.map(|(_, b)| b.as_slice())
}
pub fn len(&self) -> usize {
self.columns.len()
}
pub fn is_empty(&self) -> bool {
self.columns.is_empty()
}
}
pub trait FromRow: Sized {
fn from_row(row: &Row) -> CassandraResult<Self>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_empty_row() {
let row = Row::empty();
assert!(row.is_empty());
assert_eq!(row.len(), 0);
}
#[test]
fn test_column_bytes_lookup() {
let row = Row {
columns: vec![
("id".into(), vec![1, 2, 3]),
("name".into(), b"alice".to_vec()),
],
};
assert_eq!(row.column_bytes("id"), Some(&[1u8, 2, 3][..]));
assert_eq!(row.column_bytes("name"), Some(&b"alice"[..]));
assert!(row.column_bytes("missing").is_none());
}
}