core_pb/util/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use core::time::Duration;
#[cfg(feature = "std")]
use ecolor::Color32;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};

pub mod average_rate;
pub mod moving_average;
pub mod stopwatch;
pub mod utilization;

#[cfg(feature = "std")]
pub const TRANSLUCENT_GREEN_COLOR: Color32 = Color32::from_rgba_premultiplied(0, 50, 0, 50);
#[cfg(feature = "std")]
pub const TRANSLUCENT_YELLOW_COLOR: Color32 = Color32::from_rgba_premultiplied(50, 50, 0, 50);
#[cfg(feature = "std")]
pub const TRANSLUCENT_RED_COLOR: Color32 = Color32::from_rgba_premultiplied(50, 0, 0, 50);

#[cfg(feature = "std")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ColoredStatus {
    Ok(Option<String>),
    Warn(Option<String>),
    Error(Option<String>),
    NotApplicable(Option<String>),
}

#[cfg(feature = "std")]
impl ColoredStatus {
    pub fn to_color32(&self) -> Color32 {
        match self {
            ColoredStatus::Ok(_) => TRANSLUCENT_GREEN_COLOR,
            ColoredStatus::Warn(_) => TRANSLUCENT_YELLOW_COLOR,
            ColoredStatus::Error(_) => TRANSLUCENT_RED_COLOR,
            ColoredStatus::NotApplicable(_) => Color32::TRANSPARENT,
        }
    }

    pub fn to_color32_solid(&self) -> Color32 {
        match self {
            ColoredStatus::Ok(_) => Color32::GREEN,
            ColoredStatus::Warn(_) => Color32::YELLOW,
            ColoredStatus::Error(_) => Color32::RED,
            ColoredStatus::NotApplicable(_) => Color32::GRAY,
        }
    }

    #[cfg(feature = "egui-phosphor")]
    pub fn icon(&self) -> &str {
        match self {
            ColoredStatus::Ok(_) => egui_phosphor::regular::CHECK,
            ColoredStatus::Warn(_) => egui_phosphor::regular::WARNING,
            ColoredStatus::Error(_) => egui_phosphor::regular::X,
            ColoredStatus::NotApplicable(_) => egui_phosphor::regular::MINUS,
        }
    }

    pub fn severity(&self) -> usize {
        match self {
            ColoredStatus::Ok(_) => 1,
            ColoredStatus::Warn(_) => 2,
            ColoredStatus::Error(_) => 3,
            ColoredStatus::NotApplicable(_) => 0,
        }
    }

    pub fn message(&self) -> Option<String> {
        match self {
            ColoredStatus::Ok(s) => s.clone(),
            ColoredStatus::Warn(s) => s.clone(),
            ColoredStatus::Error(s) => s.clone(),
            ColoredStatus::NotApplicable(s) => s.clone(),
        }
    }
}

#[allow(async_fn_in_trait)]
pub trait CrossPlatformInstant: Copy {
    fn elapsed(&self) -> Duration;

    fn checked_duration_since(&self, other: Self) -> Option<Duration>;

    async fn sleep(duration: Duration);
}

#[cfg(feature = "std")]
#[derive(Copy, Clone)]
pub struct WebTimeInstant(web_time::Instant);

#[cfg(feature = "std")]
impl Default for WebTimeInstant {
    fn default() -> Self {
        Self(web_time::Instant::now())
    }
}

#[cfg(feature = "std")]
impl CrossPlatformInstant for WebTimeInstant {
    fn elapsed(&self) -> Duration {
        self.0.elapsed()
    }

    fn checked_duration_since(&self, other: Self) -> Option<Duration> {
        self.0.checked_duration_since(other.0)
    }

    async fn sleep(duration: Duration) {
        async_std::task::sleep(duration).await
    }
}