endpoint_libs/libs/
datatable.rs1use std::future::Future;
2
3use eyre::Result;
4use serde::*;
5
6#[derive(Serialize, Deserialize, Debug, Clone)]
7pub struct RDataTable<T> {
8 rows: Vec<T>,
9}
10
11impl<T> IntoIterator for RDataTable<T> {
12 type Item = T;
13 type IntoIter = std::vec::IntoIter<T>;
14
15 fn into_iter(self) -> Self::IntoIter {
16 self.rows.into_iter()
17 }
18}
19impl<T> RDataTable<T> {
20 pub fn with_capacity(capacity: usize) -> Self {
21 Self {
22 rows: Vec::with_capacity(capacity),
23 }
24 }
25 pub fn first<R>(&self, f: impl Fn(&T) -> R) -> Option<R> {
26 self.rows.first().map(f)
27 }
28 pub fn rows(&self) -> &Vec<T> {
29 &self.rows
30 }
31 pub fn into_rows(self) -> Vec<T> {
32 self.rows
33 }
34 pub fn iter(&self) -> impl Iterator<Item = &T> {
35 self.rows.iter()
36 }
37 pub fn len(&self) -> usize {
38 self.rows.len()
39 }
40 pub fn is_empty(&self) -> bool {
41 self.rows.is_empty()
42 }
43 pub fn into_result(self) -> Option<T> {
44 self.rows.into_iter().next()
45 }
46 pub fn push(&mut self, row: T) {
47 self.rows.push(row);
48 }
49 pub fn map<R>(self, f: impl Fn(T) -> R) -> Vec<R> {
50 self.rows.into_iter().map(f).collect()
51 }
52 pub async fn map_async<R, F: Future<Output = Result<R>>>(
53 self,
54 f: impl Fn(T) -> F,
55 ) -> Result<Vec<R>> {
56 let mut futures = Vec::with_capacity(self.rows.len());
57 for row in self.rows {
58 futures.push(f(row).await?);
59 }
60 Ok(futures)
61 }
62}