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
use super::{
    BodyItem, Comment, CustomProperty, Import, Item, MediaRule, Property,
    Rule, Value,
};
use crate::output::CssBuf;
use std::io::{self, Write};

/// An `@something` rule in css.
///
/// Note that some well-known at rules (`@media`, `@keyframes`, ...)
/// probably should have their own types.
#[derive(Clone, Debug)]
pub struct AtRule {
    name: String,
    args: Value,
    // Some<[]> outputs "{}", None outputs ";".
    body: Option<Vec<AtRuleBodyItem>>,
}

impl AtRule {
    pub(crate) fn new(
        name: String,
        args: Value,
        body: Option<Vec<AtRuleBodyItem>>,
    ) -> Self {
        Self { name, args, body }
    }
    pub(crate) fn no_body(&self) -> bool {
        self.body.is_none()
    }
    pub(crate) fn write(&self, buf: &mut CssBuf) -> io::Result<()> {
        buf.do_indent_no_nl();
        write!(buf, "@{}", self.name)?;
        if !self.args.is_null() {
            write!(buf, " {}", self.args.format(buf.format()))?;
        }
        if let Some(body) = &self.body {
            if let [AtRuleBodyItem::Comment(c)] = &body[..] {
                buf.add_one(" { ", "{");
                c.write(buf);
                buf.pop_nl();
                buf.add_one(" }\n", "}");
            } else {
                buf.start_block();
                for item in body {
                    item.write(buf)?;
                }
                buf.end_block();
            }
        } else {
            buf.add_one(";\n", ";");
        }
        Ok(())
    }
}

/// Something that may exist in the body of an [`AtRule`].
#[derive(Clone, Debug)]
pub enum AtRuleBodyItem {
    /// An `@import` statement.
    Import(Import),
    /// A comment
    Comment(Comment),
    /// A rule
    Rule(Rule),
    /// A raw property.
    Property(Property),
    /// A custom property declaration with a name and a value.
    CustomProperty(CustomProperty),
    /// An `@media` rule.
    MediaRule(MediaRule),
    /// An `@` rule.
    AtRule(AtRule),
}

impl AtRuleBodyItem {
    pub(crate) fn write(&self, buf: &mut CssBuf) -> io::Result<()> {
        match self {
            Self::Import(import) => import.write(buf)?,
            Self::Comment(comment) => comment.write(buf),
            Self::Rule(rule) => rule.write(buf)?,
            Self::Property(property) => property.write(buf),
            Self::CustomProperty(cp) => cp.write(buf),
            Self::MediaRule(rule) => rule.write(buf)?,
            Self::AtRule(rule) => rule.write(buf)?,
        }
        Ok(())
    }
}
impl From<Rule> for AtRuleBodyItem {
    fn from(rule: Rule) -> Self {
        Self::Rule(rule)
    }
}
impl From<Comment> for AtRuleBodyItem {
    fn from(rule: Comment) -> Self {
        Self::Comment(rule)
    }
}
impl From<Import> for AtRuleBodyItem {
    fn from(rule: Import) -> Self {
        Self::Import(rule)
    }
}
impl From<Property> for AtRuleBodyItem {
    fn from(rule: Property) -> Self {
        Self::Property(rule)
    }
}
impl From<AtRule> for AtRuleBodyItem {
    fn from(rule: AtRule) -> Self {
        Self::AtRule(rule)
    }
}
impl From<MediaRule> for AtRuleBodyItem {
    fn from(rule: MediaRule) -> Self {
        Self::MediaRule(rule)
    }
}
impl From<BodyItem> for AtRuleBodyItem {
    fn from(value: BodyItem) -> Self {
        match value {
            BodyItem::Import(i) => Self::Import(i),
            BodyItem::Property(p) => Self::Property(p),
            BodyItem::CustomProperty(p) => Self::CustomProperty(p),
            BodyItem::Comment(c) => Self::Comment(c),
            BodyItem::ARule(r) => Self::AtRule(r),
        }
    }
}

impl TryFrom<Item> for AtRuleBodyItem {
    type Error = &'static str;

    fn try_from(value: Item) -> Result<Self, Self::Error> {
        match value {
            Item::Comment(x) => Ok(x.into()),
            Item::Import(x) => Ok(x.into()),
            Item::Rule(x) => Ok(x.into()),
            Item::MediaRule(x) => Ok(x.into()),
            Item::AtRule(x) => Ok(x.into()),
            Item::Separator => Err("separator not supported here"),
        }
    }
}