Skip to main content

ferroui_dynamic_data/list/
sort_exception.rs

1//! `SortException` (`List/SortException.cs`).
2
3use ferroui_system::ObservableError;
4use std::error::Error;
5use std::fmt;
6use std::rc::Rc;
7
8/// The error of a sort that cannot find the position of an item (`SortException`): a comparer
9/// that does not give a unique position to each item while a binary search is asked for, or an
10/// item that is not in the sorted collection. The operators end their sequence with it, as an
11/// exception thrown in an operator of upstream reaches `OnError`.
12#[derive(Clone, Debug)]
13pub struct SortException {
14    message: String,
15    inner: Option<ObservableError>,
16}
17
18impl SortException {
19    /// An error with `message` (`SortException(string)`).
20    pub fn new(message: impl Into<String>) -> Self {
21        Self { message: message.into(), inner: None }
22    }
23
24    /// An error with `message`, caused by `inner` (`SortException(string, Exception)`).
25    pub fn with_inner(message: impl Into<String>, inner: ObservableError) -> Self {
26        Self { message: message.into(), inner: Some(inner) }
27    }
28
29    /// The message (`Message`).
30    pub fn message(&self) -> &str {
31        &self.message
32    }
33
34    /// The error as the error of a sequence.
35    pub fn into_error(self) -> ObservableError {
36        Rc::new(self)
37    }
38}
39
40impl Default for SortException {
41    /// An error with the default message of an exception (`SortException()`).
42    fn default() -> Self {
43        Self::new("Exception of type 'DynamicData.SortException' was thrown.")
44    }
45}
46
47impl fmt::Display for SortException {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        f.write_str(&self.message)
50    }
51}
52
53impl Error for SortException {
54    fn source(&self) -> Option<&(dyn Error + 'static)> {
55        self.inner.as_deref()
56    }
57}