Skip to main content

style/values/specified/
frequency.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//! Specified frequency values.
6
7use crate::derives::*;
8use cssparser::match_ignore_ascii_case;
9
10/// The unit of a `<frequency>` value.
11#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, PartialOrd, ToShmem)]
12#[repr(u8)]
13pub enum FrequencyUnit {
14    /// `Hz`
15    Hz,
16    /// `kHz`
17    Khz,
18}
19
20impl FrequencyUnit {
21    /// Returns the frequency unit for the given string.
22    #[inline]
23    pub fn from_str(unit: &str) -> Result<Self, ()> {
24        Ok(match_ignore_ascii_case! { unit,
25            "hz" => Self::Hz,
26            "khz" => Self::Khz,
27             _ => return Err(())
28        })
29    }
30
31    /// Returns this unit as a string.
32    #[inline]
33    pub fn as_str(self) -> &'static str {
34        match self {
35            Self::Hz => "hz",
36            Self::Khz => "khz",
37        }
38    }
39}