use {
crate::{
parsers::{Parse, ParserContext},
CustomParseError,
},
cssparser::{serialize_identifier, ParseError, Parser, ToCss},
std::fmt,
};
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum FontWeight {
_100,
_200,
_300,
_400,
_500,
_600,
_700,
_800,
_900,
}
impl ToCss for FontWeight {
fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
use self::FontWeight::*;
match *self {
_100 => serialize_identifier("100", dest),
_200 => serialize_identifier("200", dest),
_300 => serialize_identifier("300", dest),
_400 => serialize_identifier("400", dest),
_500 => serialize_identifier("500", dest),
_600 => serialize_identifier("600", dest),
_700 => serialize_identifier("700", dest),
_800 => serialize_identifier("800", dest),
_900 => serialize_identifier("900", dest),
}
}
}
impl Parse for FontWeight {
fn parse<'i, 't>(
_: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<FontWeight, ParseError<'i, CustomParseError<'i>>> {
let result = input.r#try(|input| {
let ident = input.expect_ident().map_err(|_| ())?;
match_ignore_ascii_case! {
ident,
"normal" => Ok(Self::normal),
"bold" => Ok(Self::bold),
_ => Err(())
}
});
result.or_else(|_| {
Self::from_int(input.expect_integer()?).map_err(|()| {
ParseError::from(
CustomParseError::FontFaceAtRuleFontWeightWasNotAValidIdentifierOrInteger,
)
})
})
}
}
impl FontWeight {
pub fn from_int(weight: i32) -> Result<Self, ()> {
use self::FontWeight::*;
match weight {
100 => Ok(_100),
200 => Ok(_200),
300 => Ok(_300),
400 => Ok(_400),
500 => Ok(_500),
600 => Ok(_600),
700 => Ok(_700),
800 => Ok(_800),
900 => Ok(_900),
_ => Err(()),
}
}
pub fn isExactlyNormal(&self) -> bool {
*self == FontWeight::_400
}
pub fn isExactlyBold(&self) -> bool {
*self == FontWeight::_700
}
pub fn isBold(&self) -> bool {
*self > FontWeight::_500
}
pub fn bolder(self) -> Self {
use self::FontWeight::*;
if self < _400 {
_400
} else if self < _600 {
_700
} else {
_900
}
}
pub fn lighter(self) -> Self {
use self::FontWeight::*;
if self < _600 {
_100
} else if self < _800 {
_400
} else {
_700
}
}
pub const normal: FontWeight = FontWeight::_400;
pub const bold: FontWeight = FontWeight::_700;
}