domrs/css/
declarations.rs

1use crate::css::values::CssValue;
2use crate::CssProperty;
3use std::fmt;
4use std::fmt::Display;
5
6#[derive(Debug, Clone)]
7pub struct CssDeclaration {
8  pub(crate) property: CssProperty,
9  pub(crate) value: CssValue,
10}
11
12impl CssDeclaration {
13  /// Creates a new CSS declaration.
14  pub fn new(property: impl Into<CssProperty>, value: impl Into<CssValue>) -> Self {
15    Self {
16      property: property.into(),
17      value: value.into(),
18    }
19  }
20}
21
22impl Display for CssDeclaration {
23  /// Implements [Display] for [CssDeclaration].
24  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25    write!(f, "{}: {};", self.property, self.value)
26  }
27}
28
29#[cfg(test)]
30mod tests {
31  use crate::css::declarations::CssDeclaration;
32  use crate::css::values::CssValue;
33  use crate::{CssNumber, CssProperty, CssUnit};
34
35  #[test]
36  fn display_should_work() {
37    assert_eq!(
38      "width: 1.2346px;",
39      CssDeclaration::new(CssProperty::Width, CssValue::Num1(CssNumber::new(1.23456, 4, CssUnit::Px))).to_string()
40    );
41  }
42}