1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
use std::{collections::BTreeSet, fmt::Debug};
use crate::{systems::instruction::TailwindInstruction, *};
mod methods;
mod setter;
///
#[derive(Debug)]
pub struct TailwindBuilder {
///
pub obfuscate: bool,
///
pub preflight: PreflightSystem,
/// All dynamic color properties
///
/// Only determined when packing
pub palettes: PaletteSystem,
/// All dynamic break points
///
/// Only determined when packing
pub screens: BreakPointSystem,
/// All dynamically registered font properties
///
/// Only determined when packing
pub fonts: FontSystem,
pub(crate) objects: BTreeSet<CssInstance>,
pub(crate) bundles: BTreeSet<CssBundle>,
}
impl TailwindBuilder {
/// ## Trace mode
///
///
/// # Returns
/// **Not all instructions can be inline, if not, it will fall back to trace mode**
///
/// - Anonymous style sheets, which can be placed inside `style` tags
///
/// ## Example
/// - input
/// ```html
/// <div class="p-auto px-px pt-2 pb-2">Test</div>
/// ```
/// - output
/// ```html
/// <div class="p-auto px-px pt-2 pb-2">Test</div>
/// <style> {} </style>
/// ```
#[inline]
pub fn trace(&mut self, style: &str) -> Result<String> {
let out = try_trace(self, style)?;
Ok(out.as_traced())
}
/// ## Inline mode
///
///
/// # Returns
/// **Not all instructions can be inline, if not, it will fall back to trace mode**
///
/// - Anonymous style sheets, which can be placed inside `style` tags
///
/// ## Example
/// - input
/// ```html
/// <div class="p-auto px-px pt-2 pb-2">Test</div>
/// ```
/// - output
/// ```html
/// <div class="p-auto px-px pt-2 pb-2">Test</div>
/// <style> {} </style>
/// ```
#[inline]
pub fn inline(&mut self, style: &str) -> Result<(String, String)> {
let out = try_inline(self, style, CssInlineMode::Inline)?;
Ok(out.as_inlined())
}
/// ## Inline mode
///
///
/// # Returns
/// **Not all instructions can be inline, if not, it will fall back to trace mode**
///
/// - Anonymous style sheets, which can be placed inside `style` tags
///
/// ## Example
/// - input
/// ```html
/// <div class="p-auto px-px pt-2 pb-2">Test</div>
/// ```
/// - output
/// ```html
/// <div class="p-auto px-px pt-2 pb-2">Test</div>
/// <style> {} </style>
/// ```
#[inline]
pub fn scope(&mut self, style: &str) -> Result<String> {
let out = try_inline(self, style, CssInlineMode::Scoped)?;
Ok(out.as_scope())
}
/// ## Inline mode
///
///
/// # Returns
/// **Not all instructions can be inline, if not, it will fall back to trace mode**
///
/// - Anonymous style sheets, which can be placed inside `style` tags
///
/// ## Example
/// - input
/// ```html
/// <div class="p-auto px-px pt-2 pb-2">Test</div>
/// ```
/// - output
/// ```html
/// <div class="p-auto px-px pt-2 pb-2">Test</div>
/// <style> {} </style>
/// ```
#[inline]
pub fn data_key(&mut self, style: &str) -> Result<(String, String)> {
let out = try_inline(self, style, CssInlineMode::DataKey)?;
Ok(out.as_dataset())
}
/// ## Inline mode
///
///
/// # Returns
/// **Not all instructions can be inline, if not, it will fall back to trace mode**
///
/// - Anonymous style sheets, which can be placed inside `style` tags
///
/// ## Example
/// - input
/// ```html
/// <div class="p-auto px-px pt-2 pb-2">Test</div>
/// ```
/// - output
/// ```html
/// <div class="p-auto px-px pt-2 pb-2">Test</div>
/// <style> {} </style>
/// ```
#[inline]
pub fn data_value(&mut self, style: &str) -> Result<(String, String)> {
let out = try_inline(self, style, CssInlineMode::DataValue)?;
Ok(out.as_dataset())
}
/// Bundle all used stylesheets
pub fn bundle(&self) -> Result<String> {
let mut out = String::with_capacity(1024 * 10);
if !self.preflight.disable {
out.push_str(&self.preflight.to_string());
}
for item in &self.objects {
item.write_css(&mut out)?;
}
for item in &self.bundles {
item.write_css(&mut out)?;
}
Ok(out)
}
}
fn parse_tailwind(input: &str) -> Result<Vec<TailwindInstruction>> {
let styles = tailwind_ast::parse_tailwind(input)?;
Ok(styles.into_iter().map(TailwindInstruction::from).collect())
}
fn try_trace(tw: &mut TailwindBuilder, style: &str) -> Result<CssBundle> {
let parsed = parse_tailwind(style)?;
let mut out = CssBundle::default();
for item in parsed {
let i = CssInstance::new(&*item.get_instance()?, tw);
out.add_trace(&i);
tw.objects.insert(i);
}
Ok(out)
}
fn try_inline(tw: &mut TailwindBuilder, style: &str, mode: CssInlineMode) -> Result<CssBundle> {
let parsed = parse_tailwind(style)?;
let mut out = CssBundle::default();
for item in parsed {
let i = CssInstance::new(&*item.get_instance()?, tw);
match &i.inlineable {
true => out.add_inline(i),
false => {
out.add_trace(&i);
tw.objects.insert(i);
},
};
}
out.set_mode(mode);
tw.bundles.insert(out.to_owned());
Ok(out)
}