lewp_css/domain/at_rules/media/
media_type.rs

1// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.
3
4use {
5    crate::CustomParseError,
6    cssparser::{CowRcStr, ToCss},
7    std::fmt,
8    MediaType::*,
9};
10
11/// <https://drafts.csswg.org/mediaqueries/#media-types>
12#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
13pub enum MediaType {
14    print,
15    screen,
16    speech,
17}
18
19impl ToCss for MediaType {
20    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
21        let identifier = match *self {
22            print => "print",
23            screen => "screen",
24            speech => "speech",
25        };
26
27        dest.write_str(identifier)
28    }
29}
30
31impl MediaType {
32    pub(crate) fn parse<'i>(
33        ident: &CowRcStr<'i>,
34    ) -> Result<Self, CustomParseError<'i>> {
35        // From https://drafts.csswg.org/mediaqueries/#mq-syntax: "the <media-type> production does not include the keywords not, or, and, and only".
36        match_ignore_ascii_case! {
37            &*ident,
38
39            "print" => Ok(print),
40
41            "screen" => Ok(screen),
42
43            "speech" => Ok(speech),
44
45            "aural" => Ok(speech),
46
47            "tty" | "tv" | "projection" | "handheld" | "braille" | "embossed" | "3d-glasses" => Err(CustomParseError::DeprecatedMediaType(ident.clone())),
48
49            "not" | "or" | "and" | "only" => Err(CustomParseError::InvalidMediaType(ident.clone())),
50
51            _ => Err(CustomParseError::UnrecognisedMediaType(ident.clone())),
52        }
53    }
54}