use super::{AtRule, Comment, CssString, MediaRule, Rule, Value};
use crate::output::CssBuf;
use std::io::{self, Write};
#[derive(Clone, Debug)]
pub enum Item {
Comment(Comment),
Import(Import),
Rule(Rule),
MediaRule(MediaRule),
AtRule(AtRule),
Separator,
}
impl Item {
pub(crate) fn write(&self, buf: &mut CssBuf) -> io::Result<()> {
match self {
Self::Comment(comment) => comment.write(buf),
Self::Import(import) => import.write(buf)?,
Self::Rule(rule) => rule.write(buf)?,
Self::MediaRule(rule) => rule.write(buf)?,
Self::AtRule(atrule) => atrule.write(buf)?,
Self::Separator => buf.opt_nl(),
}
Ok(())
}
}
impl From<Comment> for Item {
fn from(comment: Comment) -> Self {
Self::Comment(comment)
}
}
impl From<Import> for Item {
fn from(import: Import) -> Self {
Self::Import(import)
}
}
impl From<Rule> for Item {
fn from(rule: Rule) -> Self {
Self::Rule(rule)
}
}
impl From<AtRule> for Item {
fn from(value: AtRule) -> Self {
Self::AtRule(value)
}
}
impl From<MediaRule> for Item {
fn from(value: MediaRule) -> Self {
Self::MediaRule(value)
}
}
#[derive(Clone, Debug)]
pub struct Import {
name: CssString,
args: Value,
}
impl Import {
pub fn new(name: CssString, args: Value) -> Self {
Self { name, args }
}
pub(crate) fn write(&self, buf: &mut CssBuf) -> io::Result<()> {
buf.do_indent_no_nl();
write!(buf, "@import {}", self.name)?;
if !self.args.is_null() {
write!(buf, " {}", self.args.format(buf.format()))?;
}
buf.add_one(";\n", ";");
Ok(())
}
}