lewp_css/domain/at_rules/font_face/
font_weight.rs1use {
5 crate::{
6 parsers::{Parse, ParserContext},
7 CustomParseError,
8 },
9 cssparser::{serialize_identifier, ParseError, Parser, ToCss},
10 std::fmt,
11};
12
13#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
16pub enum FontWeight {
17 _100,
18 _200,
19 _300,
20 _400,
21 _500,
22 _600,
23 _700,
24 _800,
25 _900,
26}
27
28impl ToCss for FontWeight {
29 fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
30 use self::FontWeight::*;
31
32 match *self {
33 _100 => serialize_identifier("100", dest),
34 _200 => serialize_identifier("200", dest),
35 _300 => serialize_identifier("300", dest),
36 _400 => serialize_identifier("400", dest),
37 _500 => serialize_identifier("500", dest),
38 _600 => serialize_identifier("600", dest),
39 _700 => serialize_identifier("700", dest),
40 _800 => serialize_identifier("800", dest),
41 _900 => serialize_identifier("900", dest),
42 }
43 }
44}
45
46impl Parse for FontWeight {
47 fn parse<'i, 't>(
48 _: &ParserContext,
49 input: &mut Parser<'i, 't>,
50 ) -> Result<FontWeight, ParseError<'i, CustomParseError<'i>>> {
51 let result = input.r#try(|input| {
52 let ident = input.expect_ident().map_err(|_| ())?;
53 match_ignore_ascii_case! {
54 ident,
55 "normal" => Ok(Self::normal),
56 "bold" => Ok(Self::bold),
57 _ => Err(())
58 }
59 });
60
61 result.or_else(|_| {
62 Self::from_int(input.expect_integer()?).map_err(|()| {
63 ParseError::from(
64 CustomParseError::FontFaceAtRuleFontWeightWasNotAValidIdentifierOrInteger,
65 )
66 })
67 })
68 }
69}
70
71impl FontWeight {
72 pub fn from_int(weight: i32) -> Result<Self, ()> {
75 use self::FontWeight::*;
76
77 match weight {
78 100 => Ok(_100),
79 200 => Ok(_200),
80 300 => Ok(_300),
81 400 => Ok(_400),
82 500 => Ok(_500),
83 600 => Ok(_600),
84 700 => Ok(_700),
85 800 => Ok(_800),
86 900 => Ok(_900),
87 _ => Err(()),
88 }
89 }
90
91 pub fn isExactlyNormal(&self) -> bool {
92 *self == FontWeight::_400
93 }
94
95 pub fn isExactlyBold(&self) -> bool {
96 *self == FontWeight::_700
97 }
98
99 pub fn isBold(&self) -> bool {
100 *self > FontWeight::_500
101 }
102
103 pub fn bolder(self) -> Self {
105 use self::FontWeight::*;
106
107 if self < _400 {
108 _400
109 } else if self < _600 {
110 _700
111 } else {
112 _900
113 }
114 }
115
116 pub fn lighter(self) -> Self {
118 use self::FontWeight::*;
119
120 if self < _600 {
121 _100
122 } else if self < _800 {
123 _400
124 } else {
125 _700
126 }
127 }
128
129 pub const normal: FontWeight = FontWeight::_400;
130
131 pub const bold: FontWeight = FontWeight::_700;
132}