Skip to main content

ferroui_dynamic_data/list/
page_change_set.rs

1//! `IPageChangeSet<T>` and `PageChangeSet<T>` (`List/IPageChangeSet.cs`, `List/PageChangeSet.cs`).
2
3use super::{ChangeSet, IChangeSet};
4use crate::cache::PageResponse;
5use std::fmt;
6use std::ops::Deref;
7
8/// A change set of a page of a list, with the description of the page (`IPageChangeSet<T>`).
9pub trait IPageChangeSet<T>: IChangeSet<T> {
10    /// The page (`Response`).
11    fn response(&self) -> &PageResponse;
12}
13
14/// The changes of a page of a list, and the page (`PageChangeSet<T>`).
15///
16/// It dereferences to its [`ChangeSet`], as upstream's forwards the members of `IChangeSet<T>`.
17pub struct PageChangeSet<T> {
18    changes: ChangeSet<T>,
19    response: PageResponse,
20}
21
22impl<T> Clone for PageChangeSet<T> {
23    fn clone(&self) -> Self {
24        Self { changes: self.changes.clone(), response: self.response }
25    }
26}
27
28impl<T> PageChangeSet<T> {
29    /// The changes of the page `response` (`PageChangeSet(IChangeSet<T>, IPageResponse)`).
30    pub fn new(changes: ChangeSet<T>, response: PageResponse) -> Self {
31        Self { changes, response }
32    }
33
34    /// The page (`Response`).
35    pub fn response(&self) -> &PageResponse {
36        &self.response
37    }
38
39    /// The changes.
40    pub fn changes(&self) -> &ChangeSet<T> {
41        &self.changes
42    }
43}
44
45impl<T> Deref for PageChangeSet<T> {
46    type Target = ChangeSet<T>;
47
48    fn deref(&self) -> &Self::Target {
49        &self.changes
50    }
51}
52
53impl<'a, T> IntoIterator for &'a PageChangeSet<T> {
54    type Item = &'a super::Change<T>;
55    type IntoIter = std::slice::Iter<'a, super::Change<T>>;
56
57    fn into_iter(self) -> Self::IntoIter {
58        self.changes.changes().iter()
59    }
60}
61
62impl<T: 'static> IChangeSet<T> for PageChangeSet<T> {
63    fn change_set(&self) -> &ChangeSet<T> {
64        &self.changes
65    }
66}
67
68impl<T: 'static> IPageChangeSet<T> for PageChangeSet<T> {
69    fn response(&self) -> &PageResponse {
70        &self.response
71    }
72}
73
74impl<T: PartialEq> PartialEq for PageChangeSet<T> {
75    fn eq(&self, other: &Self) -> bool {
76        self.changes == other.changes && self.response == other.response
77    }
78}
79
80impl<T: fmt::Debug> fmt::Debug for PageChangeSet<T> {
81    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82        f.debug_struct("PageChangeSet").field("changes", &self.changes).field("response", &self.response).finish()
83    }
84}