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
// Copyright (C) 2023 Benjamin Stürz
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//! Embed CSS directly into your Rust code.
//!
//! # Example
//! ``` rust
//! use inline_css::*;
//!
//! let x = 42;
//!
//! let css = css! {
//! h1 {
//! color: red;
//! background-color: #0x00ffff; // not #00ffff
//! padding: 10px;
//! margin-left: 10m; // not 10em
//! margin-right: -4%;
//! }
//! };
//!
//! println!("{css}");
//! ```
//!
//! # How to use
//! Use the [`css! {..}`](crate::css) macro to embed CSS directly into your Rust code.
//! During the compile-time of your Rust program, the content inside of your `css!` invocations
//! will be checked and converted to an instance of [`CSS`](crate::CSS).
//! This makes sure that all invocations of the `css!` macro are guaranteed to be valid CSS at runtime.
//!
//! ## Variants
//! There are 3 macros:
//! ### [`css!`](crate::css)
//! Parse an entire snippet of [CSS](crate::CSS).
//!
//! ``` rust
//! use inline_css::*;
//!
//! let rule: CSS = css! {
//! h1 {
//! color: red;
//! }
//!
//! .button {
//! color: blue;
//! }
//!
//! #text {
//! color: green;
//! }
//! };
//! ```
//!
//! ### [`css_rule!`](crate::css_rule)
//! Parse a single CSS [Rule](crate::Rule).
//!
//! ``` rust
//! use inline_css::*;
//!
//! let rule: Rule = css_rule! {
//! h1 {
//! color: red;
//! }
//! };
//! ```
//! ### [`css_value!`](crate::css_value)
//! Parse a CSS [Value](crate::Value).
//!
//! ``` rust
//! use inline_css::*;
//!
//! let value: Value = css_value! { 10px };
//! ```
//!
//! # Dynamic Data
//! You can include dynamically-generated data into the `css!` macro invocation.
//!
//! Only types implementing `Into<Value>` are supported.
//!
//! ## Example
//! ``` rust
//! use inline_css::*;
//!
//! let x = 42;
//! let css = css! {
//! h1 {
//! attr: {x + 1};
//! }
//! };
//! ```
mod fmt;
#[cfg(feature = "inline-xml")]
mod xml;
#[cfg(test)]
mod tests;
mod impls;
/// Parse an entire snippet of [CSS](crate::CSS).
///
/// See the [crate-level documentation](crate#css).
pub use inline_css_macros::css;
/// Parse a CSS [Rule](crate::Rule).
///
/// See the [crate-level documentation](crate#css_rule).
pub use inline_css_macros::css_rule;
/// Parse a CSS [Value](crate::Value).
///
/// See the [crate-level documentation](crate#css_value).
pub use inline_css_macros::css_value;
/// CSS holds an entire snippet of CSS.
///
/// See the [crate-level documentation](crate#css).
#[derive(Debug, Clone, PartialEq)]
pub struct CSS(pub Vec<Rule>);
/// A single CSS rule.
///
/// See the [crate-level documentation](crate#css_rule).
#[derive(Debug, Clone, PartialEq)]
pub struct Rule {
pub selector: Selector,
pub declarations: Vec<Declaration>,
}
/// A CSS selector.
#[derive(Debug, Clone, PartialEq)]
pub enum Selector {
Simple {
ty: Option<String>,
class: Option<String>,
id: Option<String>,
},
Colon {
name: String,
arg: Option<Value>,
},
Sub {
left: Box<Selector>,
right: Box<Selector>,
},
Gt {
left: Box<Selector>,
right: Box<Selector>,
},
Plus {
left: Box<Selector>,
right: Box<Selector>,
},
Tilde {
left: Box<Selector>,
right: Box<Selector>,
},
Comma {
left: Box<Selector>,
right: Box<Selector>,
},
}
/// A CSS declaration.
#[derive(Debug, Clone, PartialEq)]
pub struct Declaration {
pub name: String,
pub value: Vec<Value>,
}
/// A CSS value.
///
/// See the [crate-level documentation](crate#css_value).
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
ColorCode(u32),
Ident(String),
Int(i32, String),
Float(f32, String),
Function {
name: String,
args: Vec<Value>,
}
}