diff --git a/float-pigment-css/src/group.rs b/float-pigment-css/src/group.rs index 947bfc4..64286a8 100644 --- a/float-pigment-css/src/group.rs +++ b/float-pigment-css/src/group.rs @@ -685,14 +685,24 @@ impl StyleSheetGroup { path: &str, scope: Option, ) -> u16 { + self.append_from_resource_with_warnings(res, path, scope).0 + } + + /// Append a style sheet from the resource, returning its index and warnings like @import not found. + pub fn append_from_resource_with_warnings( + &mut self, + res: &StyleSheetResource, + path: &str, + scope: Option, + ) -> (u16, Vec) { let path = drop_css_extension(path); - let (ss, _warnings) = res.link(path, scope); + let (ss, warnings) = res.link(path, scope); let ret = self.sheets.len(); if Self::is_invalid_index(ret) { panic!("The number of stylesheets has reached the maximum limit.") } self.sheets.push(ss); - ret as u16 + (ret as u16, warnings) } /// Replace a style sheet from the resource by its index. diff --git a/float-pigment-css/src/sheet/font_face.rs b/float-pigment-css/src/sheet/font_face.rs index f5189d2..88c3b83 100644 --- a/float-pigment-css/src/sheet/font_face.rs +++ b/float-pigment-css/src/sheet/font_face.rs @@ -15,6 +15,28 @@ pub struct FontFace { pub font_display: Option, } +impl core::fmt::Display for FontFace { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "@font-face {{")?; + write!(f, " font-family: {};", self.font_family)?; + write!(f, " src: ")?; + for src in &self.src { + write!(f, "{}, ", src)?; + } + write!(f, ";")?; + if let Some(fs) = &self.font_style { + write!(f, " font-style: {};", fs)?; + } + if let Some(fw) = &self.font_weight { + write!(f, " font-weight: {};", fw)?; + } + if let Some(fd) = &self.font_display { + write!(f, " font-display: {};", fd)?; + } + write!(f, "}}") + } +} + #[allow(missing_docs)] #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)] pub enum FontSrc { @@ -22,6 +44,23 @@ pub enum FontSrc { Url(FontUrl), } +impl core::fmt::Display for FontSrc { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + Self::Local(ff) => write!(f, r#"local("{}")"#, ff), + Self::Url(url) => { + write!(f, r#"url("{}")"#, url.url)?; + if let Some(formats) = &url.format { + for format in formats { + write!(f, r#" format("{}")"#, format)?; + } + } + Ok(()) + } + } + } +} + #[allow(missing_docs)] #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)] pub struct FontUrl { @@ -41,6 +80,18 @@ pub enum FontDisplay { Optional, } +impl core::fmt::Display for FontDisplay { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + Self::Auto => write!(f, "auto"), + Self::Block => write!(f, "block"), + Self::Swap => write!(f, "swap"), + Self::Fallback => write!(f, "fallback"), + Self::Optional => write!(f, "optional"), + } + } +} + impl Default for FontFace { fn default() -> Self { Self { diff --git a/float-pigment-css/src/sheet/keyframes.rs b/float-pigment-css/src/sheet/keyframes.rs index 71ecb40..7f731c6 100644 --- a/float-pigment-css/src/sheet/keyframes.rs +++ b/float-pigment-css/src/sheet/keyframes.rs @@ -21,6 +21,16 @@ impl KeyFrames { } } +impl core::fmt::Display for KeyFrames { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "@keyframes {} {{ ", self.ident)?; + for keyframe in &self.keyframes { + write!(f, "{} ", keyframe)?; + } + write!(f, "}}") + } +} + /// The percentage field in a keyframe item. #[repr(C)] #[derive(Clone, Debug, Serialize, Deserialize)] @@ -61,3 +71,32 @@ impl KeyFrameRule { } } } + +impl core::fmt::Display for KeyFrameRule { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!( + f, + "{} {{ ", + self.keyframe + .iter() + .map(|x| { + match x { + KeyFrame::From => "from".to_owned(), + KeyFrame::To => "to".to_owned(), + KeyFrame::Ratio(ratio) => format!("{:.2}%", ratio * 100.), + } + }) + .collect::>() + .join(", ") + )?; + for prop in &self.properties { + write!( + f, + "{}: {}; ", + prop.get_property_name(), + prop.get_property_value_string() + )?; + } + write!(f, "}}") + } +} diff --git a/float-pigment-css/src/sheet/mod.rs b/float-pigment-css/src/sheet/mod.rs index fd7375f..d28846a 100644 --- a/float-pigment-css/src/sheet/mod.rs +++ b/float-pigment-css/src/sheet/mod.rs @@ -462,6 +462,21 @@ impl core::fmt::Debug for StyleSheet { } } +impl core::fmt::Display for StyleSheet { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + for rule in self.rules.iter() { + write!(f, " {}", rule)?; + } + for font_face in self.font_face.iter() { + write!(f, " {}", font_face)?; + } + for keyframes in self.keyframes.iter() { + write!(f, " {}", keyframes)?; + } + Ok(()) + } +} + impl StyleSheet { #[doc(hidden)] #[allow(clippy::should_implement_trait)] diff --git a/float-pigment-css/src/sheet/rule.rs b/float-pigment-css/src/sheet/rule.rs index 2b48339..d02d02e 100644 --- a/float-pigment-css/src/sheet/rule.rs +++ b/float-pigment-css/src/sheet/rule.rs @@ -5,7 +5,7 @@ use super::*; use crate::property::Property; /// A CSS property with some metadata. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub enum PropertyMeta { /// A single normal property, e.g. `font-size: 16px`. Normal {