Skip to main content

gpui_query/core/
status.rs

1//! Query status enum representing the lifecycle states of a query resource.
2
3use serde::{Deserialize, Serialize};
4
5/// The status of a query resource.
6///
7/// A query transitions through these states:
8/// `Idle` → `LoadingEmpty` → `Success` / `Failure`
9/// `Success` → `LoadingWithData` → `Success` / `Failure` (refetch)
10#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
11pub enum QueryStatus {
12    /// No data has been fetched yet. Initial state.
13    #[default]
14    Idle,
15    /// Loading for the first time (no data available).
16    LoadingEmpty,
17    /// Refetching with existing data available.
18    LoadingWithData,
19    /// Data loaded successfully.
20    Success,
21    /// The last fetch failed.
22    Failure,
23    /// The request was cancelled.
24    Cancelled,
25}
26
27impl QueryStatus {
28    /// Human-readable label for the status.
29    pub fn label(self) -> &'static str {
30        match self {
31            Self::Idle => "Idle",
32            Self::LoadingEmpty => "Loading empty",
33            Self::LoadingWithData => "Loading with data",
34            Self::Success => "Success",
35            Self::Failure => "Failure",
36            Self::Cancelled => "Cancelled",
37        }
38    }
39
40    /// Whether the resource is currently loading (first time or refetch).
41    pub fn is_loading(self) -> bool {
42        matches!(self, Self::LoadingEmpty | Self::LoadingWithData)
43    }
44
45    /// Whether the resource is pending (no data yet and currently loading).
46    ///
47    /// Equivalent to TanStack Query's `isPending`.
48    pub fn is_pending(self) -> bool {
49        matches!(self, Self::Idle | Self::LoadingEmpty)
50    }
51}