#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CloseGuard {
allow: bool,
}
impl CloseGuard {
pub fn new() -> Self {
Self::default()
}
pub fn is_allowed(&self) -> bool {
self.allow
}
pub fn should_prompt(&mut self, has_unsaved: bool) -> bool {
!self.allow && has_unsaved
}
pub fn allow(&mut self) {
self.allow = true;
}
pub fn reset(&mut self) {
self.allow = false;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn prompts_only_while_dirty_and_not_allowed() {
let mut guard = CloseGuard::new();
assert!(!guard.should_prompt(false));
assert!(guard.should_prompt(true));
guard.allow();
assert!(!guard.should_prompt(true));
guard.reset();
assert!(guard.should_prompt(true));
}
}