Skip to content

Commit f7d0ee5

Browse files
committed
Refactor to have a length property
To closer match the CSS documentation. And to better know if it can be lifted up by looking at the containing_block_relative property.
1 parent 23462cc commit f7d0ee5

File tree

8 files changed

+308
-97
lines changed

8 files changed

+308
-97
lines changed

Cargo.lock

+29-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ figma-schema = { path = "./figma-schema" }
1515
figma-html = { path = "./figma-html" }
1616
anyhow = "1.0.68"
1717
clap = { version = "4.0.32", features = ["derive"] }
18-
indexmap = { version = "1.9.2", features = ["serde"] }
18+
indexmap = { version = "2.0.0", features = ["serde"] }
1919
serde = { version = "1.0", features = ["derive"] }
2020
serde_json = { version = "1.0", features = ["preserve_order"] }

figma-html/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ keywords = ["Figma"]
1515
figma-schema = { path = "../figma-schema", version = "0.3.0" }
1616
anyhow = "1.0.68"
1717
html-escape = "0.2.13"
18-
indexmap = { version = "1.9.2", features = ["serde"] }
18+
indexmap = { version = "2.0.0", features = ["serde"] }
1919
itertools = "0.10.5"
2020
lightningcss = "1.0.0-alpha.40"
2121
serde = { version = "1.0", features = ["derive"] }

figma-html/src/intermediate_node.rs

+28-34
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use serde::{Deserialize, Serialize};
1111

1212
mod html_formatter;
1313
mod inset;
14-
mod size;
14+
mod length;
1515
pub use html_formatter::{format_css, HtmlFormatter};
1616
pub use inset::Inset;
17-
pub use size::Size;
17+
pub use length::Length;
1818

1919
use super::css_properties::{absolute_bounding_box, fills_color, stroke_color, CssProperties};
2020

@@ -62,27 +62,27 @@ pub enum StrokeStyle {
6262
}
6363

6464
#[derive(Debug, Serialize, Deserialize)]
65-
pub struct FlexContainer {
65+
pub struct FlexContainer<'a> {
6666
pub align_items: AlignItems,
6767
pub direction: FlexDirection,
68-
pub gap: Size,
68+
pub gap: Length<'a>,
6969
#[serde(skip_serializing_if = "Option::is_none")]
7070
pub justify_content: Option<JustifyContent>,
7171
}
7272

7373
#[derive(Debug, Serialize, Deserialize)]
74-
pub struct Location {
75-
pub padding: [Size; 4],
74+
pub struct Location<'a> {
75+
pub padding: [Length<'a>; 4],
7676
#[serde(skip_serializing_if = "Option::is_none")]
7777
pub align_self: Option<AlignSelf>,
7878
#[serde(skip_serializing_if = "Option::is_none")]
7979
pub flex_grow: Option<f64>,
8080
#[serde(skip_serializing_if = "Option::is_none")]
8181
pub inset: Option<[Inset; 4]>,
8282
#[serde(skip_serializing_if = "Option::is_none")]
83-
pub height: Option<Size>,
83+
pub height: Option<Length<'a>>,
8484
#[serde(skip_serializing_if = "Option::is_none")]
85-
pub width: Option<Size>,
85+
pub width: Option<Length<'a>>,
8686
}
8787

8888
#[derive(Debug, Serialize, Deserialize)]
@@ -103,11 +103,11 @@ pub struct Appearance {
103103
}
104104

105105
#[derive(Debug, Serialize, Deserialize)]
106-
pub struct FrameAppearance {
106+
pub struct FrameAppearance<'a> {
107107
#[serde(skip_serializing_if = "Option::is_none")]
108108
pub background: Option<String>,
109109
#[serde(skip_serializing_if = "Option::is_none")]
110-
pub border_radius: Option<[Size; 4]>,
110+
pub border_radius: Option<[Length<'a>; 4]>,
111111
#[serde(skip_serializing_if = "Option::is_none")]
112112
pub box_shadow: Option<String>,
113113
#[serde(skip_serializing_if = "Option::is_none")]
@@ -141,10 +141,10 @@ pub struct IntermediateNode<'a> {
141141
#[serde(skip_serializing_if = "Option::is_none")]
142142
pub figma: Option<Figma<'a>>,
143143
#[serde(skip_serializing_if = "Option::is_none")]
144-
pub flex_container: Option<FlexContainer>,
145-
pub location: Location,
144+
pub flex_container: Option<FlexContainer<'a>>,
145+
pub location: Location<'a>,
146146
pub appearance: Appearance,
147-
pub frame_appearance: FrameAppearance,
147+
pub frame_appearance: FrameAppearance<'a>,
148148
pub node_type: IntermediateNodeType<'a>,
149149
#[serde(skip_serializing_if = "Option::is_none")]
150150
pub href: Option<Cow<'a, str>>,
@@ -170,7 +170,7 @@ impl<'a> IntermediateNode<'a> {
170170
Some(CounterAxisAlignItems::Max) => AlignItems::FlexEnd,
171171
Some(CounterAxisAlignItems::Baseline) => AlignItems::Baseline,
172172
};
173-
let gap = Size::Pixels(node.item_spacing.unwrap_or(0.0));
173+
let gap = Length::new_from_option_pixels(node.item_spacing);
174174
let justify_content = match node.primary_axis_align_items {
175175
None => None,
176176
Some(PrimaryAxisAlignItems::Min) => Some(JustifyContent::FlexStart),
@@ -196,10 +196,10 @@ impl<'a> IntermediateNode<'a> {
196196
},
197197
location: Location {
198198
padding: [
199-
Size::Pixels(node.padding_top.unwrap_or(0.0)),
200-
Size::Pixels(node.padding_right.unwrap_or(0.0)),
201-
Size::Pixels(node.padding_bottom.unwrap_or(0.0)),
202-
Size::Pixels(node.padding_left.unwrap_or(0.0)),
199+
Length::new_from_option_pixels(node.padding_top),
200+
Length::new_from_option_pixels(node.padding_right),
201+
Length::new_from_option_pixels(node.padding_bottom),
202+
Length::new_from_option_pixels(node.padding_left),
203203
],
204204
align_self: match (
205205
parent.and_then(|p| p.layout_mode.as_ref()),
@@ -277,7 +277,7 @@ impl<'a> IntermediateNode<'a> {
277277
) if counter_axis_sizing_mode != &Some(AxisSizingMode::Fixed) => None,
278278
_ => absolute_bounding_box(node)
279279
.and_then(|b| b.height)
280-
.map(Size::Pixels),
280+
.map(|h| Length::new_from_option_pixels(Some(h))),
281281
},
282282
width: match (parent, node) {
283283
(
@@ -337,7 +337,7 @@ impl<'a> IntermediateNode<'a> {
337337
) if counter_axis_sizing_mode != &Some(AxisSizingMode::Fixed) => None,
338338
_ => absolute_bounding_box(node)
339339
.and_then(|b| b.width)
340-
.map(Size::Pixels),
340+
.map(|w| Length::new_from_option_pixels(Some(w))),
341341
},
342342
},
343343
appearance: Appearance {
@@ -383,10 +383,10 @@ impl<'a> IntermediateNode<'a> {
383383
.rectangle_corner_radii()
384384
.map(|[top, right, bottom, left]| {
385385
[
386-
Size::Pixels(top),
387-
Size::Pixels(right),
388-
Size::Pixels(bottom),
389-
Size::Pixels(left),
386+
Length::new_from_option_pixels(Some(top)),
387+
Length::new_from_option_pixels(Some(right)),
388+
Length::new_from_option_pixels(Some(bottom)),
389+
Length::new_from_option_pixels(Some(left)),
390390
]
391391
}),
392392
box_shadow: node.box_shadow(),
@@ -508,9 +508,8 @@ impl<'a> IntermediateNode<'a> {
508508
padding: [top, right, bottom, left],
509509
..
510510
} = &self.location;
511-
if (top != &Size::Pixels(0.0) || bottom != &Size::Pixels(0.0)) && height.is_some()
512-
|| (right != &Size::Pixels(0.0) || left != &Size::Pixels(0.0))
513-
&& width.is_some()
511+
if (top != &Length::Zero || bottom != &Length::Zero) && height.is_some()
512+
|| (right != &Length::Zero || left != &Length::Zero) && width.is_some()
514513
{
515514
Some(Cow::Borrowed("border-box"))
516515
} else {
@@ -540,7 +539,7 @@ impl<'a> IntermediateNode<'a> {
540539
(
541540
"gap",
542541
self.flex_container.as_ref().and_then(|c| {
543-
if c.gap == Size::Pixels(0.0) {
542+
if c.gap == Length::Zero {
544543
None
545544
} else {
546545
Some(Cow::Owned(format!("{}", c.gap)))
@@ -610,12 +609,7 @@ impl<'a> IntermediateNode<'a> {
610609
),
611610
("padding", {
612611
let p = &self.location.padding;
613-
if p == &[
614-
Size::Pixels(0.0),
615-
Size::Pixels(0.0),
616-
Size::Pixels(0.0),
617-
Size::Pixels(0.0),
618-
] {
612+
if p == &[Length::Zero, Length::Zero, Length::Zero, Length::Zero] {
619613
None
620614
} else {
621615
Some(Cow::Owned(format!("{} {} {} {}", p[0], p[1], p[2], p[3])))

0 commit comments

Comments
 (0)