use boa_interner::{Interner, Sym, ToInternedString};
use core::ops::ControlFlow;
use crate::Statement;
use crate::visitor::{VisitWith, Visitor, VisitorMut};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Break {
label: Option<Sym>,
}
impl Break {
#[must_use]
pub const fn new(label: Option<Sym>) -> Self {
Self { label }
}
#[must_use]
pub const fn label(&self) -> Option<Sym> {
self.label
}
}
impl ToInternedString for Break {
fn to_interned_string(&self, interner: &Interner) -> String {
self.label.map_or_else(
|| "break".to_owned(),
|label| format!("break {}", interner.resolve_expect(label)),
)
}
}
impl From<Break> for Statement {
fn from(break_smt: Break) -> Self {
Self::Break(break_smt)
}
}
impl VisitWith for Break {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: Visitor<'a>,
{
if let Some(sym) = &self.label {
visitor.visit_sym(sym)
} else {
ControlFlow::Continue(())
}
}
fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: VisitorMut<'a>,
{
if let Some(sym) = &mut self.label {
visitor.visit_sym_mut(sym)
} else {
ControlFlow::Continue(())
}
}
}