Skip to main content

ferroui_dynamic_data/binding/
binding_options.rs

1//! `BindingOptions` (`Binding/BindingOptions.cs`).
2
3/// The options of the binding of change sets to an observable collection (`BindingOptions`).
4#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
5pub struct BindingOptions {
6    /// The number of changes above which the collection is loaded again and a reset is raised
7    /// (`ResetThreshold`).
8    pub reset_threshold: usize,
9    /// Whether an update replaces the item in place, rather than removing and adding it
10    /// (`UseReplaceForUpdates`).
11    pub use_replace_for_updates: bool,
12    /// Whether the first change set loads the collection with a reset (`ResetOnFirstTimeLoad`).
13    pub reset_on_first_time_load: bool,
14}
15
16impl BindingOptions {
17    /// `DefaultResetThreshold`.
18    pub const DEFAULT_RESET_THRESHOLD: usize = 25;
19    /// `DefaultUseReplaceForUpdates`.
20    pub const DEFAULT_USE_REPLACE_FOR_UPDATES: bool = true;
21    /// `DefaultResetOnFirstTimeLoad`.
22    pub const DEFAULT_RESET_ON_FIRST_TIME_LOAD: bool = true;
23
24    /// The options with the threshold and upstream's defaults for the others
25    /// (`BindingOptions(int ResetThreshold)`).
26    pub const fn new(reset_threshold: usize) -> Self {
27        Self { reset_threshold, use_replace_for_updates: Self::DEFAULT_USE_REPLACE_FOR_UPDATES, reset_on_first_time_load: Self::DEFAULT_RESET_ON_FIRST_TIME_LOAD }
28    }
29
30    /// Every part (`BindingOptions(int, bool, bool)`).
31    pub const fn with_all(reset_threshold: usize, use_replace_for_updates: bool, reset_on_first_time_load: bool) -> Self {
32        Self { reset_threshold, use_replace_for_updates, reset_on_first_time_load }
33    }
34
35    /// The options that never reset the collection (`NeverFireReset(bool useReplaceForUpdates =
36    /// true)`).
37    pub const fn never_fire_reset() -> Self {
38        Self::never_fire_reset_with(Self::DEFAULT_RESET_ON_FIRST_TIME_LOAD)
39    }
40
41    /// [`never_fire_reset`](Self::never_fire_reset) with the choice of replacements
42    /// (`NeverFireReset(bool)`).
43    pub const fn never_fire_reset_with(use_replace_for_updates: bool) -> Self {
44        Self { reset_threshold: usize::MAX, use_replace_for_updates, reset_on_first_time_load: false }
45    }
46
47    /// A copy with another threshold (`with { ResetThreshold = .. }`).
48    pub const fn with_reset_threshold(self, reset_threshold: usize) -> Self {
49        Self { reset_threshold, ..self }
50    }
51
52    /// A copy with another choice of replacements (`with { UseReplaceForUpdates = .. }`).
53    pub const fn with_use_replace_for_updates(self, use_replace_for_updates: bool) -> Self {
54        Self { use_replace_for_updates, ..self }
55    }
56
57    /// A copy with another choice of the first load (`with { ResetOnFirstTimeLoad = .. }`).
58    pub const fn with_reset_on_first_time_load(self, reset_on_first_time_load: bool) -> Self {
59        Self { reset_on_first_time_load, ..self }
60    }
61}
62
63impl Default for BindingOptions {
64    /// `new BindingOptions(DefaultResetThreshold)`.
65    fn default() -> Self {
66        Self::new(Self::DEFAULT_RESET_THRESHOLD)
67    }
68}