Skip to content

Commit c773b68

Browse files
committed
added generic error,
fixed sorting for tables with skipped fields
1 parent bf16336 commit c773b68

File tree

6 files changed

+44
-24
lines changed

6 files changed

+44
-24
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## [0.7.1] -
4+
5+
### Changes
6+
7+
- Added generic error type to `TableDataProvider`
8+
- Fixed sorting for tables with skipped fields
9+
310
## [0.7.0] - 2024-02-08
411

512
### Features 🚀

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "leptos-struct-table"
3-
version = "0.7.0"
3+
version = "0.7.1"
44
edition = "2021"
55
authors = ["Marc-Stefan Cassola"]
66
categories = ["gui", "web-programming"]
@@ -13,7 +13,7 @@ repository = "https://github.com/Synphonyte/leptos-struct-table"
1313

1414
[dependencies]
1515
leptos = { version = "0.6", features = ["nightly"] }
16-
leptos-struct-table-macro = "0.9"
16+
leptos-struct-table-macro = "0.9.1"
1717
leptos-use = "0.10"
1818
async-trait = "0.1"
1919
paste = { version = "1.0", optional = true }

examples/paginated_rest_datasource/src/data_provider.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ impl BookDataProvider {
4747
}
4848

4949
format!(
50-
"https://archive.org/advancedsearch.php?q=creator%3A%28Lewis%29&fl%5B%5D=creator&fl%5B%5D=identifier&fl%5B%5D=publicdate&fl%5B%5D=title{sort}&rows={}&page={}&output=json&callback=",
51-
Self::PAGE_ROW_COUNT,
52-
page_index + 1,
53-
)
50+
"https://archive.org/advancedsearch.php?q=creator%3A%28Lewis%29&fl%5B%5D=creator&fl%5B%5D=identifier&fl%5B%5D=publicdate&fl%5B%5D=title{sort}&rows={}&page={}&output=json&callback=",
51+
Self::PAGE_ROW_COUNT,
52+
page_index + 1,
53+
)
5454
}
5555
}
5656

examples/tailwind/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
mod tailwind;
22

3-
use async_trait::async_trait;
43
use chrono::NaiveDate;
54
use leptos::*;
65
use leptos_struct_table::*;

src/components/table_content.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use leptos_use::{
1616
};
1717
use std::cell::RefCell;
1818
use std::collections::{HashSet, VecDeque};
19+
use std::fmt::Debug;
20+
use std::marker::PhantomData;
1921
use std::ops::Range;
2022
use std::rc::Rc;
2123
use wasm_bindgen::JsCast;
@@ -55,7 +57,7 @@ renderer_fn!(
5557

5658
/// Render the content of a table. This is the main component of this crate.
5759
#[component]
58-
pub fn TableContent<Row, DataP, ClsP>(
60+
pub fn TableContent<Row, DataP, Err, ClsP>(
5961
/// The data to be rendered in this table.
6062
/// This must implement [`TableDataProvider`] or [`PaginatedTableDataProvider`].
6163
rows: DataP,
@@ -176,10 +178,13 @@ pub fn TableContent<Row, DataP, ClsP>(
176178
/// Please check [`DisplayStrategy`] to see explanations of all available options.
177179
#[prop(optional)]
178180
display_strategy: DisplayStrategy,
181+
182+
#[prop(optional)] _marker: PhantomData<Err>,
179183
) -> impl IntoView
180184
where
181185
Row: TableRow<ClassesProvider = ClsP> + Clone + 'static,
182-
DataP: TableDataProvider<Row> + 'static,
186+
DataP: TableDataProvider<Row, Err> + 'static,
187+
Err: Debug,
183188
ClsP: TableClassesProvider + Copy + 'static,
184189
{
185190
let on_change = store_value(on_change);
@@ -412,7 +417,11 @@ where
412417
let set_known_row_count = set_known_row_count.clone();
413418

414419
async move {
415-
let result = rows.borrow().get_rows(missing_range.clone()).await;
420+
let result = rows
421+
.borrow()
422+
.get_rows(missing_range.clone())
423+
.await
424+
.map_err(|err| format!("{err:?}"));
416425
if let Ok((_, loaded_range)) = &result {
417426
if loaded_range.end < missing_range.end {
418427
set_known_row_count(missing_range.end);

src/data_provider.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::ColumnSort;
22
use async_trait::async_trait;
33
use std::collections::VecDeque;
4+
use std::fmt::Debug;
45
use 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
103107
where
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

Comments
 (0)