use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct ClassBitmask(pub [u64; 4]);
impl ClassBitmask {
pub const fn empty() -> Self {
Self([0, 0, 0, 0])
}
pub const fn from_raw(raw: [u64; 4]) -> Self {
Self(raw)
}
pub const fn raw(&self) -> [u64; 4] {
self.0
}
pub const fn is_empty(&self) -> bool {
self.0[0] == 0 && self.0[1] == 0 && self.0[2] == 0 && self.0[3] == 0
}
pub const fn contains_index(&self, index: usize) -> bool {
if index >= 256 {
return false;
}
let word = index / 64;
let bit = index % 64;
(self.0[word] & (1u64 << bit)) != 0
}
pub const fn with_bit(mut self, index: usize) -> Self {
if index < 256 {
let word = index / 64;
let bit = index % 64;
self.0[word] |= 1u64 << bit;
}
self
}
pub fn set_bit(&mut self, index: usize) {
if index < 256 {
let word = index / 64;
let bit = index % 64;
self.0[word] |= 1u64 << bit;
}
}
pub fn count_ones(&self) -> u32 {
self.0[0].count_ones()
+ self.0[1].count_ones()
+ self.0[2].count_ones()
+ self.0[3].count_ones()
}
}
pub const STANDARD_CLASSES: [&str; 256] = [
"flex",
"grid",
"inline",
"inline-flex",
"inline-grid",
"block",
"inline-block",
"hidden",
"contents",
"table",
"table-row",
"table-cell",
"flex-row",
"flex-row-reverse",
"flex-col",
"flex-col-reverse",
"flex-wrap",
"flex-wrap-reverse",
"flex-nowrap",
"flex-1",
"flex-auto",
"flex-initial",
"flex-none",
"grow",
"grow-0",
"shrink",
"shrink-0",
"items-start",
"items-end",
"items-center",
"items-baseline",
"items-stretch",
"justify-start",
"justify-end",
"justify-center",
"justify-between",
"justify-around",
"justify-evenly",
"justify-items-start",
"justify-items-end",
"justify-items-center",
"justify-items-stretch",
"justify-self-auto",
"justify-self-start",
"justify-self-end",
"justify-self-center",
"justify-self-stretch",
"content-center",
"content-start",
"content-end",
"content-between",
"content-around",
"content-evenly",
"content-baseline",
"content-stretch",
"self-auto",
"self-start",
"self-end",
"self-center",
"self-stretch",
"self-baseline",
"relative",
"absolute",
"fixed",
"sticky",
"static",
"inset-0",
"top-0",
"bottom-0",
"left-0",
"right-0",
"w-full",
"w-auto",
"w-screen",
"w-min",
"w-max",
"w-fit",
"w-0",
"w-1",
"w-2",
"w-3",
"w-4",
"w-5",
"w-6",
"w-8",
"w-10",
"w-12",
"w-16",
"w-20",
"w-24",
"w-32",
"w-40",
"w-48",
"w-56",
"w-64",
"w-72",
"w-80",
"w-96",
"h-full",
"h-auto",
"h-screen",
"h-min",
"h-max",
"h-fit",
"h-0",
"h-1",
"h-2",
"h-3",
"h-4",
"h-5",
"h-6",
"h-8",
"h-10",
"h-12",
"h-16",
"h-20",
"h-24",
"h-32",
"h-40",
"h-48",
"h-56",
"h-64",
"h-72",
"h-80",
"h-96",
"min-w-0",
"min-w-full",
"max-w-none",
"max-w-full",
"min-h-0",
"min-h-full",
"min-h-screen",
"max-h-full",
"max-h-screen",
"overflow-auto",
"overflow-hidden",
"overflow-clip",
"overflow-visible",
"overflow-scroll",
"overflow-x-auto",
"overflow-x-hidden",
"overflow-x-scroll",
"overflow-y-auto",
"overflow-y-hidden",
"overflow-y-scroll",
"p-0",
"p-1",
"p-2",
"p-3",
"p-4",
"p-5",
"p-6",
"p-8",
"p-10",
"p-12",
"px-0",
"px-1",
"px-2",
"px-3",
"px-4",
"px-6",
"px-8",
"py-0",
"py-1",
"py-2",
"py-3",
"py-4",
"py-6",
"py-8",
"m-0",
"m-1",
"m-2",
"m-3",
"m-4",
"m-5",
"m-6",
"m-8",
"m-auto",
"mx-auto",
"my-auto",
"mx-0",
"mx-1",
"mx-2",
"mx-4",
"my-0",
"my-1",
"my-2",
"my-4",
"gap-0",
"gap-1",
"gap-2",
"gap-3",
"gap-4",
"gap-6",
"gap-8",
"text-xs",
"text-sm",
"text-base",
"text-lg",
"text-xl",
"text-2xl",
"text-3xl",
"text-4xl",
"font-thin",
"font-normal",
"font-medium",
"font-semibold",
"font-bold",
"font-extrabold",
"text-left",
"text-center",
"text-right",
"text-justify",
"text-white",
"text-black",
"text-transparent",
"truncate",
"bg-white",
"bg-black",
"bg-transparent",
"bg-neutral-900",
"bg-neutral-800",
"bg-neutral-700",
"bg-neutral-100",
"bg-neutral-50",
"border",
"border-0",
"border-2",
"border-4",
"border-neutral-800",
"border-neutral-700",
"border-transparent",
"rounded-none",
"rounded-sm",
"rounded",
"rounded-md",
"rounded-lg",
"rounded-xl",
"rounded-2xl",
"rounded-full",
"shadow-none",
"shadow-sm",
"shadow",
"shadow-md",
"shadow-lg",
"shadow-xl",
"shadow-2xl",
"cursor-auto",
"cursor-default",
"cursor-pointer",
"cursor-wait",
"cursor-text",
"cursor-move",
"cursor-not-allowed",
"contain-strict",
"contain-paint",
];
pub fn standard_class_index(name: &str) -> Option<usize> {
STANDARD_CLASSES.iter().position(|&s| s == name)
}
#[derive(Clone, PartialEq, Eq, Default)]
pub enum ClassList {
#[default]
Empty,
Single(&'static str),
Static(&'static [&'static str]),
Masked {
mask: ClassBitmask,
tail: &'static [&'static str],
},
Dynamic(Vec<String>),
}
impl ClassList {
pub const fn empty() -> Self {
Self::Empty
}
pub const fn from_mask(raw: [u64; 4]) -> Self {
Self::Masked {
mask: ClassBitmask::from_raw(raw),
tail: &[],
}
}
pub const fn from_single(s: &'static str) -> Self {
Self::Single(s)
}
pub const fn from_static(slice: &'static [&'static str]) -> Self {
Self::Static(slice)
}
pub const fn from_masked(mask: ClassBitmask, tail: &'static [&'static str]) -> Self {
Self::Masked { mask, tail }
}
pub fn is_empty(&self) -> bool {
match self {
Self::Empty => true,
Self::Single(s) => s.is_empty(),
Self::Static(s) => s.is_empty(),
Self::Masked { mask, tail } => mask.is_empty() && tail.is_empty(),
Self::Dynamic(v) => v.is_empty(),
}
}
pub fn len(&self) -> usize {
match self {
Self::Empty => 0,
Self::Single(_) => 1,
Self::Static(s) => s.len(),
Self::Masked { mask, tail } => mask.count_ones() as usize + tail.len(),
Self::Dynamic(v) => v.len(),
}
}
pub fn contains(&self, class: &str) -> bool {
match self {
Self::Empty => false,
Self::Single(s) => *s == class,
Self::Static(s) => s.contains(&class),
Self::Masked { mask, tail } => {
if let Some(idx) = standard_class_index(class) {
if mask.contains_index(idx) {
return true;
}
}
tail.contains(&class)
}
Self::Dynamic(v) => v.iter().any(|c| c == class),
}
}
pub fn push(&mut self, class: impl Into<String>) {
self.as_mut_vec().push(class.into());
}
pub fn extend(&mut self, iter: impl IntoIterator<Item = impl Into<String>>) {
let vec = self.as_mut_vec();
for item in iter {
vec.push(item.into());
}
}
fn as_mut_vec(&mut self) -> &mut Vec<String> {
if let Self::Dynamic(ref mut v) = self {
return v;
}
let owned: Vec<String> = self.iter().map(|s| s.to_string()).collect();
*self = Self::Dynamic(owned);
if let Self::Dynamic(ref mut v) = self {
v
} else {
unreachable!()
}
}
pub fn to_vec(&self) -> Vec<String> {
self.iter().map(|s| s.to_string()).collect()
}
pub fn join(&self, sep: &str) -> String {
let mut out = String::new();
for (i, c) in self.iter().enumerate() {
if i > 0 {
out.push_str(sep);
}
out.push_str(c);
}
out
}
pub fn iter(&self) -> ClassIter<'_> {
match self {
Self::Empty => ClassIter::Empty,
Self::Single(item) => ClassIter::Single { item: Some(item) },
Self::Static(slice) => ClassIter::Static { slice, index: 0 },
Self::Masked { mask, tail } => ClassIter::Masked {
mask: *mask,
current_bit: 0,
tail,
tail_index: 0,
},
Self::Dynamic(slice) => ClassIter::Dynamic {
slice: slice.as_slice(),
index: 0,
},
}
}
}
impl<'a> IntoIterator for &'a ClassList {
type Item = &'a str;
type IntoIter = ClassIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl From<Vec<String>> for ClassList {
fn from(v: Vec<String>) -> Self {
Self::Dynamic(v)
}
}
impl From<&'static [&'static str]> for ClassList {
fn from(slice: &'static [&'static str]) -> Self {
Self::Static(slice)
}
}
impl From<&'static str> for ClassList {
fn from(s: &'static str) -> Self {
Self::Single(s)
}
}
impl From<[u64; 4]> for ClassList {
fn from(raw: [u64; 4]) -> Self {
Self::from_mask(raw)
}
}
impl PartialEq<Vec<String>> for ClassList {
fn eq(&self, other: &Vec<String>) -> bool {
self.len() == other.len() && self.iter().zip(other.iter()).all(|(a, b)| a == b.as_str())
}
}
impl PartialEq<ClassList> for Vec<String> {
fn eq(&self, other: &ClassList) -> bool {
other == self
}
}
impl PartialEq<[&str]> for ClassList {
fn eq(&self, other: &[&str]) -> bool {
self.len() == other.len() && self.iter().zip(other.iter()).all(|(a, b)| a == *b)
}
}
impl fmt::Debug for ClassList {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
#[derive(Clone)]
pub enum ClassIter<'a> {
Empty,
Single {
item: Option<&'a str>,
},
Static {
slice: &'a [&'static str],
index: usize,
},
Masked {
mask: ClassBitmask,
current_bit: usize,
tail: &'a [&'static str],
tail_index: usize,
},
Dynamic {
slice: &'a [String],
index: usize,
},
}
impl<'a> Iterator for ClassIter<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::Empty => None,
Self::Single { item } => item.take(),
Self::Static { slice, index } => {
if *index < slice.len() {
let item = slice[*index];
*index += 1;
Some(item)
} else {
None
}
}
Self::Dynamic { slice, index } => {
if *index < slice.len() {
let item = slice[*index].as_str();
*index += 1;
Some(item)
} else {
None
}
}
Self::Masked {
mask,
current_bit,
tail,
tail_index,
} => {
while *current_bit < 256 {
let bit = *current_bit;
*current_bit += 1;
if mask.contains_index(bit) {
return Some(STANDARD_CLASSES[bit]);
}
}
if *tail_index < tail.len() {
let item = tail[*tail_index];
*tail_index += 1;
Some(item)
} else {
None
}
}
}
}
}