lewp_css/domain/at_rules/font_face/
font_language_override.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    super::OpenTypeLanguageTag,
6    crate::CustomParseError,
7    cssparser::{serialize_identifier, ParseError, Parser, ToCss},
8    std::fmt,
9};
10
11#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
12pub enum FontLanguageOverride {
13    normal,
14    Override(OpenTypeLanguageTag),
15}
16
17impl ToCss for FontLanguageOverride {
18    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
19        use self::FontLanguageOverride::*;
20
21        match *self {
22            normal => serialize_identifier("normal", dest),
23            Override(openTypeLanguageTag) => openTypeLanguageTag.to_css(dest),
24        }
25    }
26}
27
28impl FontLanguageOverride {
29    /// Parse a font-family value
30    pub(crate) fn parse<'i, 't>(
31        input: &mut Parser<'i, 't>,
32    ) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
33        use self::FontLanguageOverride::*;
34
35        if let Ok(value) = input.r#try(|input| match input.expect_string() {
36            Err(_) => Err(()),
37            Ok(value) => match OpenTypeLanguageTag::parse(value) {
38                Err(_) => Err(()),
39                Ok(openTypeLanguageTag) => Ok(Override(openTypeLanguageTag)),
40            },
41        }) {
42            return Ok(value);
43        }
44
45        let identifier = input.expect_ident()?.clone();
46        match_ignore_ascii_case! {
47            &identifier,
48
49            "normal" => Ok(normal),
50
51            _ => Err(ParseError::from(CustomParseError::InvalidFontLanguageOverrideIdentifier(identifier.clone()))),
52        }
53    }
54}