use derive_more::{Add, FromStr, Sub};
use serde::{Deserialize, Serialize};
use std::{
fmt::{Debug, Display},
hash::{Hash, Hasher},
};
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Serialize, Deserialize, Add, Sub, FromStr)]
#[serde(transparent)]
pub struct FontWeight(pub f32);
impl Display for FontWeight {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<f32> for FontWeight {
fn from(weight: f32) -> Self {
FontWeight(weight)
}
}
impl Default for FontWeight {
#[inline]
fn default() -> FontWeight {
FontWeight::NORMAL
}
}
impl Hash for FontWeight {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u32(u32::from_be_bytes(self.0.to_be_bytes()));
}
}
impl Eq for FontWeight {}
impl FontWeight {
pub const THIN: FontWeight = FontWeight(100.0);
pub const EXTRA_LIGHT: FontWeight = FontWeight(200.0);
pub const LIGHT: FontWeight = FontWeight(300.0);
pub const NORMAL: FontWeight = FontWeight(400.0);
pub const MEDIUM: FontWeight = FontWeight(500.0);
pub const SEMIBOLD: FontWeight = FontWeight(600.0);
pub const BOLD: FontWeight = FontWeight(700.0);
pub const EXTRA_BOLD: FontWeight = FontWeight(800.0);
pub const BLACK: FontWeight = FontWeight(900.0);
pub const ALL: [FontWeight; 9] = [
Self::THIN,
Self::EXTRA_LIGHT,
Self::LIGHT,
Self::NORMAL,
Self::MEDIUM,
Self::SEMIBOLD,
Self::BOLD,
Self::EXTRA_BOLD,
Self::BLACK,
];
}
impl schemars::JsonSchema for FontWeight {
fn schema_name() -> std::borrow::Cow<'static, str> {
"FontWeight".into()
}
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
use schemars::json_schema;
json_schema!({
"type": "number",
"minimum": Self::THIN,
"maximum": Self::BLACK,
"default": Self::default(),
"description": "Font weight value between 100 (thin) and 900 (black)"
})
}
}