Skip to content

Commit

Permalink
Implemented QueryResultItems::get_by_name_or_first() and get_by_name_…
Browse files Browse the repository at this point in the history
…or_last()
  • Loading branch information
proycon committed Feb 19, 2024
1 parent 0ae8681 commit 5b9b499
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/api/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2379,6 +2379,40 @@ impl<'store> QueryResultItems<'store> {
self.items.pop()
}

/// Returns a result by variable name (if specified), or get the first result.
/// Raises an error if there are no results.
pub fn get_by_name_or_first(
&self,
names: &QueryNames,
var: Option<&str>,
) -> Result<&QueryResultItem<'store>, StamError> {
if let Some(var) = var {
self.get_by_name(names, var)
} else {
self.iter().next().ok_or(StamError::QuerySyntaxError(
"Query returned no results".to_string(),
"(get_by_name_or_first)",
))
}
}

/// Returns a result by variable name (if specified), or get the last result.
/// Raises an error if there are no results.
pub fn get_by_name_or_last(
&self,
names: &QueryNames,
var: Option<&str>,
) -> Result<&QueryResultItem<'store>, StamError> {
if let Some(var) = var {
self.get_by_name(names, var)
} else {
self.iter().last().ok_or(StamError::QuerySyntaxError(
"Query returned no results".to_string(),
"(get_by_name_or_last)",
))
}
}

/// Returns a result by variable name
pub fn get_by_name(
&self,
Expand Down

0 comments on commit 5b9b499

Please sign in to comment.