ferrix_app/
load_state.rs

1/* load_state.rs
2 *
3 * Copyright 2025 Michail Krasnov <mskrasnov07@ya.ru>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21//! Data loading states
22
23use serde::{Deserialize, Serialize};
24
25#[derive(Debug, Deserialize, Serialize, Clone)]
26pub enum DataLoadingState<P> {
27    Loading,
28    Error(String),
29    Loaded(P),
30}
31
32impl<P> DataLoadingState<P> {
33    pub fn to_option<'a>(&'a self) -> Option<&'a P> {
34        match self {
35            Self::Loaded(data) => Some(data),
36            _ => None,
37        }
38    }
39
40    pub fn is_none(&self) -> bool {
41        match self {
42            Self::Loaded(_) => false,
43            _ => true,
44        }
45    }
46
47    pub fn is_some(&self) -> bool {
48        !self.is_none()
49    }
50
51    pub fn is_error(&self) -> bool {
52        match self {
53            Self::Error(_) => true,
54            _ => false,
55        }
56    }
57
58    pub fn unwrap(self) -> P {
59        match self {
60            Self::Loaded(data) => data,
61            _ => panic!("Data not found!"),
62        }
63    }
64}
65
66impl<T> Default for DataLoadingState<T> {
67    fn default() -> Self {
68        Self::Loading
69    }
70}