use crate::statement::Statement;
use crate::visitor::{VisitWith, Visitor, VisitorMut};
use boa_interner::{Interner, Sym, ToInternedString};
use core::ops::ControlFlow;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Continue {
label: Option<Sym>,
}
impl Continue {
#[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 Continue {
fn to_interned_string(&self, interner: &Interner) -> String {
self.label.map_or_else(
|| "continue".to_owned(),
|label| format!("continue {}", interner.resolve_expect(label)),
)
}
}
impl From<Continue> for Statement {
fn from(cont: Continue) -> Self {
Self::Continue(cont)
}
}
impl VisitWith for Continue {
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(())
}
}
}