use std::borrow::Cow;
use serde::{Deserialize, Serialize};
use super::{RuleBlockContent, Selector, StyleContext, ToStyleStr};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Block {
pub condition: Cow<'static, [Selector]>,
pub content: Cow<'static, [RuleBlockContent]>,
}
impl Block {
fn cond_str(&self, ctx: &mut StyleContext<'_>) -> Option<String> {
if self.condition.is_empty() {
return None;
}
let mut cond = "".to_string();
for (index, sel) in self.condition.iter().enumerate() {
sel.write_style(&mut cond, ctx);
if index < self.condition.len() - 1 {
cond.push_str(", ");
}
}
Some(cond)
}
}
impl ToStyleStr for Block {
fn write_style(&self, w: &mut String, ctx: &mut StyleContext<'_>) {
let cond_s = self.cond_str(ctx);
let mut block_ctx = ctx.with_block_condition(cond_s);
for attr in self.content.iter() {
attr.write_style(w, &mut block_ctx);
}
block_ctx.finish(w);
}
}