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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
use crate::osc94::OSC94State;
/// A guard for modifying process state.
///
/// Obtained via [`crate::res::ResOSC94::get_mut`]. When the guard is dropped, the process state is
/// automatically restored to `OS94State::Clean`, so no manual cleanup is needed.
///
/// # Example
///
/// Create a guard via [`crate::res::ResOSC94`], and the state is automatically restored to Clean
/// when the guard is dropped:
///
/// ```
/// use mingling::res::ResOSC94;
/// use mingling::osc94::OSC94Guard;
///
/// let osc94 = ResOSC94::default();
/// {
/// let mut guard: OSC94Guard = osc94.get_mut();
/// guard.set_progress(0.5);
/// // When leaving this scope, the guard is dropped and the process state is automatically restored to Clean
/// }
/// ```
pub struct OSC94Guard {
pub(crate) is_support: bool,
pub(crate) msg: OSC94State,
}
impl OSC94Guard {
/// Set the process state to Clean.
///
/// Indicates that the process has finished or is in a normal, problem-free state.
///
/// # Example
///
/// ```
/// use mingling::res::ResOSC94;
/// use mingling::osc94::{OSC94Guard, OSC94State};
///
/// let osc94 = ResOSC94::default();
/// let mut guard: OSC94Guard = osc94.get_mut();
/// guard.set_progress(0.5);
/// guard.set_clean_state();
/// assert_eq!(guard.state(), OSC94State::Clean);
/// ```
pub fn set_clean_state(&mut self) {
self.msg = OSC94State::Clean;
if self.is_support {
self.msg.send();
}
}
/// Set the process state to Error.
///
/// Indicates that an error occurred during process execution.
///
/// # Example
///
/// ```
/// use mingling::res::ResOSC94;
/// use mingling::osc94::{OSC94Guard, OSC94State};
///
/// let osc94 = ResOSC94::default();
/// let mut guard: OSC94Guard = osc94.get_mut();
/// guard.set_error_state();
/// assert_eq!(guard.state(), OSC94State::Error);
/// ```
pub fn set_error_state(&mut self) {
self.msg = OSC94State::Error;
if self.is_support {
self.msg.send();
}
}
/// Set the process state to Warn.
///
/// Indicates that a warning occurred during process execution, but it has not
/// reached the level of an error.
///
/// # Example
///
/// ```
/// use mingling::res::ResOSC94;
/// use mingling::osc94::{OSC94Guard, OSC94State};
///
/// let osc94 = ResOSC94::default();
/// let mut guard: OSC94Guard = osc94.get_mut();
/// guard.set_warn_state();
/// assert_eq!(guard.state(), OSC94State::Warn);
/// ```
pub fn set_warn_state(&mut self) {
self.msg = OSC94State::Warn;
if self.is_support {
self.msg.send();
}
}
/// Set the process state to Unknown.
///
/// Indicates that the process state cannot be determined or has not been defined.
///
/// # Example
///
/// ```
/// use mingling::res::ResOSC94;
/// use mingling::osc94::{OSC94Guard, OSC94State};
///
/// let osc94 = ResOSC94::default();
/// let mut guard: OSC94Guard = osc94.get_mut();
/// guard.set_unknown_state();
/// assert_eq!(guard.state(), OSC94State::Unknown);
/// ```
pub fn set_unknown_state(&mut self) {
self.msg = OSC94State::Unknown;
if self.is_support {
self.msg.send();
}
}
/// Set the progress of the process.
///
/// The `progress` parameter should be between `0.0` and `1.0`. `0.0` indicates
/// the start of the task, and `1.0` indicates the completion of the task.
/// Values outside this range are not clamped, but it is recommended to keep them
/// within this range.
///
/// # Parameters
///
/// * `progress` - The progress value, ranging from `0.0` to `1.0`.
///
/// # Example
///
/// ```
/// use mingling::res::ResOSC94;
/// use mingling::osc94::OSC94Guard;
///
/// let osc94 = ResOSC94::default();
/// let mut guard: OSC94Guard = osc94.get_mut();
/// guard.set_progress(0.5);
/// assert_eq!(guard.progress(), 0.5);
/// ```
pub fn set_progress(&mut self, progress: f32) {
self.msg = OSC94State::Normal(progress);
if self.is_support {
self.msg.send();
}
}
/// Get the current process state.
///
/// # Returns
///
/// Returns the current [`OSC94State`] value, representing the state of the process.
///
/// # Example
///
/// ```
/// use mingling::res::ResOSC94;
/// use mingling::osc94::{OSC94Guard, OSC94State};
///
/// let osc94 = ResOSC94::default();
/// let guard: OSC94Guard = osc94.get_mut();
/// assert_eq!(guard.state(), OSC94State::Clean);
/// ```
#[must_use]
pub const fn state(&self) -> OSC94State {
self.msg
}
/// Get the current progress value.
///
/// Returns the actual progress value only when the state is [`OSC94State::Normal`];
/// otherwise returns `0.0`.
///
/// # Returns
///
/// Returns an `f32` progress value, ranging from `0.0` to `1.0`.
///
/// # Example
///
/// ```
/// use mingling::res::ResOSC94;
/// use mingling::osc94::OSC94Guard;
///
/// let osc94 = ResOSC94::default();
/// let mut guard: OSC94Guard = osc94.get_mut();
/// guard.set_progress(0.25);
/// assert_eq!(guard.progress(), 0.25);
/// ```
#[must_use]
pub const fn progress(&self) -> f32 {
match self.msg {
OSC94State::Normal(progress) => progress,
_ => 0.0,
}
}
}
impl Drop for OSC94Guard {
fn drop(&mut self) {
OSC94State::Clean.send();
}
}
|