Skip to content

Commit b3ff80a

Browse files
authored
Merge pull request #4 from wechat-miniprogram/feat-display
feat: dump stylesheet
2 parents 97d159f + 1339540 commit b3ff80a

File tree

5 files changed

+118
-3
lines changed

5 files changed

+118
-3
lines changed

float-pigment-css/src/group.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,14 +685,24 @@ impl StyleSheetGroup {
685685
path: &str,
686686
scope: Option<NonZeroUsize>,
687687
) -> u16 {
688+
self.append_from_resource_with_warnings(res, path, scope).0
689+
}
690+
691+
/// Append a style sheet from the resource, returning its index and warnings like @import not found.
692+
pub fn append_from_resource_with_warnings(
693+
&mut self,
694+
res: &StyleSheetResource,
695+
path: &str,
696+
scope: Option<NonZeroUsize>,
697+
) -> (u16, Vec<Warning>) {
688698
let path = drop_css_extension(path);
689-
let (ss, _warnings) = res.link(path, scope);
699+
let (ss, warnings) = res.link(path, scope);
690700
let ret = self.sheets.len();
691701
if Self::is_invalid_index(ret) {
692702
panic!("The number of stylesheets has reached the maximum limit.")
693703
}
694704
self.sheets.push(ss);
695-
ret as u16
705+
(ret as u16, warnings)
696706
}
697707

698708
/// Replace a style sheet from the resource by its index.

float-pigment-css/src/sheet/font_face.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,52 @@ pub struct FontFace {
1515
pub font_display: Option<FontDisplay>,
1616
}
1717

18+
impl core::fmt::Display for FontFace {
19+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
20+
write!(f, "@font-face {{")?;
21+
write!(f, " font-family: {};", self.font_family)?;
22+
write!(f, " src: ")?;
23+
for src in &self.src {
24+
write!(f, "{}, ", src)?;
25+
}
26+
write!(f, ";")?;
27+
if let Some(fs) = &self.font_style {
28+
write!(f, " font-style: {};", fs)?;
29+
}
30+
if let Some(fw) = &self.font_weight {
31+
write!(f, " font-weight: {};", fw)?;
32+
}
33+
if let Some(fd) = &self.font_display {
34+
write!(f, " font-display: {};", fd)?;
35+
}
36+
write!(f, "}}")
37+
}
38+
}
39+
1840
#[allow(missing_docs)]
1941
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
2042
pub enum FontSrc {
2143
Local(FontFamilyName),
2244
Url(FontUrl),
2345
}
2446

47+
impl core::fmt::Display for FontSrc {
48+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
49+
match self {
50+
Self::Local(ff) => write!(f, r#"local("{}")"#, ff),
51+
Self::Url(url) => {
52+
write!(f, r#"url("{}")"#, url.url)?;
53+
if let Some(formats) = &url.format {
54+
for format in formats {
55+
write!(f, r#" format("{}")"#, format)?;
56+
}
57+
}
58+
Ok(())
59+
}
60+
}
61+
}
62+
}
63+
2564
#[allow(missing_docs)]
2665
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
2766
pub struct FontUrl {
@@ -41,6 +80,18 @@ pub enum FontDisplay {
4180
Optional,
4281
}
4382

83+
impl core::fmt::Display for FontDisplay {
84+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
85+
match self {
86+
Self::Auto => write!(f, "auto"),
87+
Self::Block => write!(f, "block"),
88+
Self::Swap => write!(f, "swap"),
89+
Self::Fallback => write!(f, "fallback"),
90+
Self::Optional => write!(f, "optional"),
91+
}
92+
}
93+
}
94+
4495
impl Default for FontFace {
4596
fn default() -> Self {
4697
Self {

float-pigment-css/src/sheet/keyframes.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ impl KeyFrames {
2121
}
2222
}
2323

24+
impl core::fmt::Display for KeyFrames {
25+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26+
write!(f, "@keyframes {} {{ ", self.ident)?;
27+
for keyframe in &self.keyframes {
28+
write!(f, "{} ", keyframe)?;
29+
}
30+
write!(f, "}}")
31+
}
32+
}
33+
2434
/// The percentage field in a keyframe item.
2535
#[repr(C)]
2636
#[derive(Clone, Debug, Serialize, Deserialize)]
@@ -61,3 +71,32 @@ impl KeyFrameRule {
6171
}
6272
}
6373
}
74+
75+
impl core::fmt::Display for KeyFrameRule {
76+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
77+
write!(
78+
f,
79+
"{} {{ ",
80+
self.keyframe
81+
.iter()
82+
.map(|x| {
83+
match x {
84+
KeyFrame::From => "from".to_owned(),
85+
KeyFrame::To => "to".to_owned(),
86+
KeyFrame::Ratio(ratio) => format!("{:.2}%", ratio * 100.),
87+
}
88+
})
89+
.collect::<Vec<_>>()
90+
.join(", ")
91+
)?;
92+
for prop in &self.properties {
93+
write!(
94+
f,
95+
"{}: {}; ",
96+
prop.get_property_name(),
97+
prop.get_property_value_string()
98+
)?;
99+
}
100+
write!(f, "}}")
101+
}
102+
}

float-pigment-css/src/sheet/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,21 @@ impl core::fmt::Debug for StyleSheet {
462462
}
463463
}
464464

465+
impl core::fmt::Display for StyleSheet {
466+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
467+
for rule in self.rules.iter() {
468+
write!(f, " {}", rule)?;
469+
}
470+
for font_face in self.font_face.iter() {
471+
write!(f, " {}", font_face)?;
472+
}
473+
for keyframes in self.keyframes.iter() {
474+
write!(f, " {}", keyframes)?;
475+
}
476+
Ok(())
477+
}
478+
}
479+
465480
impl StyleSheet {
466481
#[doc(hidden)]
467482
#[allow(clippy::should_implement_trait)]

float-pigment-css/src/sheet/rule.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::*;
55
use crate::property::Property;
66

77
/// A CSS property with some metadata.
8-
#[derive(Clone, Debug)]
8+
#[derive(Clone, Debug, PartialEq)]
99
pub enum PropertyMeta {
1010
/// A single normal property, e.g. `font-size: 16px`.
1111
Normal {

0 commit comments

Comments
 (0)