11use crate :: ColumnSort ;
22use async_trait:: async_trait;
33use std:: collections:: VecDeque ;
4+ use std:: fmt:: Debug ;
45use std:: ops:: Range ;
56
67/// The trait that provides data for the `<TableContent>` component.
@@ -10,13 +11,16 @@ use std::ops::Range;
1011/// this is automatically implemented for `Vec<Row>`.
1112/// This way a simple list of items can be passed to the table.
1213///
13- /// This is also automatically implemented for any struct that implements [`PaginatedTableDataProvider`]
14- /// which is a more convenient way of connecting to a paginated data source.
14+ /// This is also automatically implemented for any struct that implements
15+ /// [`PaginatedTableDataProvider`] or [`ExactTableDataProvider`].
16+ /// The first is a more convenient way of connecting to a paginated data source and the second is
17+ /// more convenient if you know you're always going to return exactly the requested range (except maybe
18+ /// at the end of the data).
1519///
1620/// Please note that because of the use of [`async-trait`](https://docs.rs/async-trait/latest/async_trait/)
1721/// this documentation looks a bit cluttered.
18- #[ async_trait( ?Send ) ]
19- pub trait TableDataProvider < Row > {
22+ #[ async_trait( ? Send ) ]
23+ pub trait TableDataProvider < Row , Err : Debug = String > {
2024 /// If Some(...), data will be loaded in chunks of this size. This is useful for paginated data sources.
2125 /// If you have such a paginated data source, you probably want to implement `PaginatedTableDataProvider`
2226 /// instead of this trait.
@@ -36,7 +40,7 @@ pub trait TableDataProvider<Row> {
3640 ///
3741 /// In the case of an error the returned error `String` is going to be displayed in a
3842 /// in place of the failed rows.
39- async fn get_rows ( & self , range : Range < usize > ) -> Result < ( Vec < Row > , Range < usize > ) , String > ;
43+ async fn get_rows ( & self , range : Range < usize > ) -> Result < ( Vec < Row > , Range < usize > ) , Err > ;
4044
4145 /// The total number of rows in the table. Returns `None` if unknown (which is the default).
4246 async fn row_count ( & self ) -> Option < usize > {
@@ -64,16 +68,16 @@ pub trait TableDataProvider<Row> {
6468/// > You do not have implement this trait if you're using pagination and you vice versa if you're not using pagination
6569/// > you can still implement this trait. And in case if you use this trait together with pagination the
6670/// > display row count can be different from the `PAGE_ROW_COUNT`.
67- #[ async_trait( ?Send ) ]
68- pub trait PaginatedTableDataProvider < Row > {
71+ #[ async_trait( ? Send ) ]
72+ pub trait PaginatedTableDataProvider < Row , Err : Debug = String > {
6973 /// How many rows per page
7074 const PAGE_ROW_COUNT : usize ;
7175
7276 /// Get all data rows for the table specified by the page index (starts a 0).
7377 ///
7478 /// If you return less than `PAGE_ROW_COUNT` rows, it is assumed that the end of the
7579 /// data has been reached.
76- async fn get_page ( & self , page_index : usize ) -> Result < Vec < Row > , String > ;
80+ async fn get_page ( & self , page_index : usize ) -> Result < Vec < Row > , Err > ;
7781
7882 /// The total number of rows in the table. Returns `None` if unknown (which is the default).
7983 ///
@@ -98,14 +102,15 @@ pub trait PaginatedTableDataProvider<Row> {
98102 }
99103}
100104
101- #[ async_trait( ?Send ) ]
102- impl < Row , D > TableDataProvider < Row > for D
105+ #[ async_trait( ? Send ) ]
106+ impl < Row , Err , D > TableDataProvider < Row , Err > for D
103107where
104- D : PaginatedTableDataProvider < Row > ,
108+ D : PaginatedTableDataProvider < Row , Err > ,
109+ Err : Debug ,
105110{
106111 const CHUNK_SIZE : Option < usize > = Some ( D :: PAGE_ROW_COUNT ) ;
107112
108- async fn get_rows ( & self , range : Range < usize > ) -> Result < ( Vec < Row > , Range < usize > ) , String > {
113+ async fn get_rows ( & self , range : Range < usize > ) -> Result < ( Vec < Row > , Range < usize > ) , Err > {
109114 let Range { start, end } = range;
110115 debug_assert_eq ! ( end - start, D :: PAGE_ROW_COUNT ) ;
111116
@@ -116,11 +121,11 @@ where
116121 }
117122
118123 async fn row_count ( & self ) -> Option < usize > {
119- PaginatedTableDataProvider :: < Row > :: row_count ( self ) . await
124+ PaginatedTableDataProvider :: < Row , Err > :: row_count ( self ) . await
120125 }
121126
122127 fn set_sorting ( & mut self , sorting : & VecDeque < ( usize , ColumnSort ) > ) {
123- PaginatedTableDataProvider :: < Row > :: set_sorting ( self , sorting)
128+ PaginatedTableDataProvider :: < Row , Err > :: set_sorting ( self , sorting)
124129 }
125130}
126131
0 commit comments