Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revised from previous PR90: Add contains function to RowIndex #93

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub struct Row {

/// A type suitable for indexing columns in a row.
pub trait RowIndex: std::fmt::Debug {
/// Check if the row contains a column.
fn contains(&self, row: &Row) -> bool;

/// Identify the ordinal position.
///
/// The first column has index 0.
Expand Down Expand Up @@ -169,6 +172,17 @@ impl Row {
{
T::try_from(&self.values[column.index(self)])
}

/// Check to see if a given column exists within the row.
///
/// In case of integer indices, the first column has index 0.
#[inline]
pub fn contains<U>(&self, column: U) -> bool
where
U: RowIndex,
{
column.contains(self)
}
}

impl From<Row> for Vec<Value> {
Expand Down Expand Up @@ -198,6 +212,10 @@ impl RowIndex for &str {
);
row.column_mapping[self]
}

fn contains(&self, row: &Row) -> bool {
row.column_mapping.contains_key(*self)
}
}

impl RowIndex for usize {
Expand All @@ -206,6 +224,10 @@ impl RowIndex for usize {
debug_assert!(self < row.values.len(), "the index is out of range");
self
}

fn contains(&self, row: &Row) -> bool {
self < &row.values.len()
}
}

pub fn new<'l, 'm>(statement: &'m mut Statement<'l>) -> Cursor<'l, 'm> {
Expand Down