use bitflags::bitflags;
#[cfg(feature = "serde")]
use serde::Serialize;
bitflags! {
#[derive(Default)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct PatchFlag: i32 {
const TEXT = 1;
const CLASS = 1 << 1;
const STYLE = 1 << 2;
const PROPS = 1 << 3;
const FULL_PROPS = 1 << 4;
const HYDRATE_EVENTS = 1 << 5;
const STABLE_FRAGMENT = 1 << 6;
const KEYED_FRAGMENT = 1 << 7;
const UNKEYED_FRAGMENT = 1 << 8;
const NEED_PATCH = 1 << 9;
const DYNAMIC_SLOTS = 1 << 10;
const DEV_ROOT_FRAGMENT = 1 << 11;
const HOISTED = -1;
const BAIL = -2;
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum StaticLevel {
NotStatic,
CanSkipPatch,
CanHoist,
CanStringify,
}
#[repr(u32)]
#[derive(Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum RuntimeHelper {
Fragment,
Teleport,
Suspense,
KeepAlive,
BaseTransition,
OpenBlock,
CreateBlock,
CreateElementBlock,
CreateVNode,
CreateElementVNode,
CreateComment,
CreateText,
CreateStatic,
ResolveComponent,
ResolveDynamicComponent,
ResolveDirective,
ResolveFilter,
WithDirectives,
RenderList,
RenderSlot,
CreateSlots,
ToDisplayString,
MergeProps,
NormalizeClass,
NormalizeStyle,
NormalizeProps,
GuardReactiveProps,
ToHandlers,
Camelize,
Capitalize,
ToHandlerKey,
SetBlockTracking,
PushScopeId,
PopScopeId,
WithScopeId,
WithCtx,
Unref,
IsRef,
WithMemo,
IsMemoSame,
}
use RuntimeHelper as RH;
const HELPERS_IN_HOISTED: &[RH] = &[
RH::CreateComment,
RH::CreateElementVNode,
RH::CreateStatic,
RH::CreateText,
RH::CreateVNode,
];
#[derive(Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct HelperCollector(u64);
impl HelperCollector {
pub fn new() -> Self {
Self(0)
}
pub fn is_empty(&self) -> bool {
self.0 == 0 || (cfg!(test) && self.0 == !0)
}
pub fn collect(&mut self, helper: RuntimeHelper) {
self.0 |= 1 << (helper as u64);
}
pub fn contains(&self, helper: RuntimeHelper) -> bool {
(self.0 & (1 << helper as u64)) != 0
}
pub fn hoist_helpers(&self) -> Self {
let mut n = Self(0);
for &rh in HELPERS_IN_HOISTED {
if self.contains(rh) {
n.collect(rh);
}
}
n
}
#[cfg(test)]
pub fn ignore_missing(&mut self) {
self.0 = !0;
}
}
pub struct HelperIter(u64);
impl Iterator for HelperIter {
type Item = RuntimeHelper;
fn next(&mut self) -> Option<Self::Item> {
if cfg!(test) && self.0 == !0 {
return None;
}
if self.0 == 0 {
return None;
}
let r = self.0.trailing_zeros();
self.0 ^= 1 << r;
unsafe { std::mem::transmute(r) }
}
fn size_hint(&self) -> (usize, Option<usize>) {
let bits = self.0.count_ones() as usize;
(bits, Some(bits))
}
}
impl ExactSizeIterator for HelperIter {}
impl IntoIterator for HelperCollector {
type Item = RuntimeHelper;
type IntoIter = HelperIter;
fn into_iter(self) -> Self::IntoIter {
HelperIter(self.0)
}
}
impl RuntimeHelper {
pub fn generate_imports(&self) -> String {
todo!()
}
pub fn helper_str(&self) -> &'static str {
use RuntimeHelper::*;
match *self {
Fragment => "Fragment",
Teleport => "Teleport",
Suspense => "Suspense",
KeepAlive => "KeepAlive",
BaseTransition => "BaseTransition",
OpenBlock => "openBlock",
CreateBlock => "createBlock",
CreateElementBlock => "createElementBlock",
CreateVNode => "createVNode",
CreateElementVNode => "createElementVNode",
CreateComment => "createCommentVNode",
CreateText => "createTextVNode",
CreateStatic => "createStaticVNode",
ResolveComponent => "resolveComponent",
ResolveDynamicComponent => "resolveDynamicComponent",
ResolveDirective => "resolveDirective",
ResolveFilter => "resolveFilter",
WithDirectives => "withDirectives",
RenderList => "renderList",
RenderSlot => "renderSlot",
CreateSlots => "createSlots",
ToDisplayString => "toDisplayString",
MergeProps => "mergeProps",
NormalizeClass => "normalizeClass",
NormalizeStyle => "normalizeStyle",
NormalizeProps => "normalizeProps",
GuardReactiveProps => "guardReactiveProps",
ToHandlers => "toHandlers",
Camelize => "camelize",
Capitalize => "capitalize",
ToHandlerKey => "toHandlerKey",
SetBlockTracking => "setBlockTracking",
PushScopeId => "pushScopeId",
PopScopeId => "popScopeId",
WithScopeId => "withScopeId",
WithCtx => "withCtx",
Unref => "unref",
IsRef => "isRef",
WithMemo => "withMemo",
IsMemoSame => "isMemoSame",
}
}
}
#[repr(u8)]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum SlotFlag {
Stable = 1,
Dynamic = 2,
Forwarded = 3,
}