style/values/computed/
time.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Computed time values.
6
7use crate::derives::*;
8use crate::values::CSSFloat;
9use crate::Zero;
10use std::fmt::{self, Write};
11use style_traits::{CssString, CssWriter, NumericValue, ToCss, ToTyped, TypedValue, UnitValue};
12use thin_vec::ThinVec;
13
14/// A computed `<time>` value.
15#[derive(Animate, Clone, Copy, Debug, MallocSizeOf, PartialEq, PartialOrd, ToResolvedValue)]
16#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
17#[repr(C)]
18pub struct Time {
19    seconds: CSSFloat,
20}
21
22impl Time {
23    /// Creates a time value from a seconds amount.
24    pub fn from_seconds(seconds: CSSFloat) -> Self {
25        Time { seconds }
26    }
27
28    /// Returns the amount of seconds this time represents.
29    #[inline]
30    pub fn seconds(&self) -> CSSFloat {
31        self.seconds
32    }
33}
34
35impl ToCss for Time {
36    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
37    where
38        W: Write,
39    {
40        self.seconds().to_css(dest)?;
41        dest.write_char('s')
42    }
43}
44
45impl ToTyped for Time {
46    fn to_typed(&self, dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
47        dest.push(TypedValue::Numeric(NumericValue::Unit(UnitValue {
48            value: self.seconds(),
49            unit: CssString::from("s"),
50        })));
51        Ok(())
52    }
53}
54
55impl Zero for Time {
56    fn zero() -> Self {
57        Self::from_seconds(0.0)
58    }
59
60    fn is_zero(&self) -> bool {
61        self.seconds == 0.
62    }
63}