Skip to main content

makepad_gen_plugin/builtin/prop/base/
align.rs

1use crate::{builtin::prop::convert_str_to_vec, struct_float_to_tokens, try_from_value_ref_struct};
2use gen_analyzer::value::{Struct, Value};
3use gen_utils::{err_from_to, error::Error};
4use std::{num::ParseFloatError, str::FromStr};
5
6#[derive(Debug, Clone, Default)]
7pub struct Align {
8    pub x: f64,
9    pub y: f64,
10}
11
12impl From<&Align> for toml_edit::Value {
13    fn from(value: &Align) -> Self {
14        let mut table = toml_edit::InlineTable::new();
15        table.insert("x", value.x.into());
16        table.insert("y", value.y.into());
17        toml_edit::Value::InlineTable(table)
18    }
19}
20
21impl TryFrom<&toml_edit::Value> for Align {
22    type Error = Error;
23
24    fn try_from(value: &toml_edit::Value) -> Result<Self, Self::Error> {
25        let table = value
26            .as_inline_table()
27            .ok_or_else(|| Error::from("toml_edit::Item to Align"))?;
28        let x = table
29            .get("x")
30            .and_then(|v| v.as_float())
31            .unwrap_or_default();
32        let y = table
33            .get("y")
34            .and_then(|v| v.as_float())
35            .unwrap_or_default();
36
37        Ok(Align { x, y })
38    }
39}
40
41impl TryFrom<Vec<f64>> for Align {
42    type Error = Error;
43
44    fn try_from(value: Vec<f64>) -> Result<Self, Self::Error> {
45        match value.len() {
46            1 => Ok(Align {
47                x: value[0],
48                y: value[0],
49            }),
50            2 => Ok(Align {
51                x: value[0],
52                y: value[1],
53            }),
54            _ => Err(err_from_to!(
55                "Vec<f64>" => "Makepad Align, params number incorrect need 1 or 2"
56            )),
57        }
58    }
59}
60
61impl TryFrom<&Struct> for Align {
62    type Error = Error;
63
64    fn try_from(value: &Struct) -> Result<Self, Self::Error> {
65        let Struct {
66            fields,
67            is_anonymous,
68            ..
69        } = value;
70
71        if *is_anonymous {
72            let len = fields.len();
73            if len == 0 || len > 4 {
74                return Err(err_from_to!("Struct" => "Vec4, params number incorrect need [1, 2]"));
75            }
76
77            let mut x = None;
78            let mut y = None;
79
80            for (key, value) in fields {
81                match key.as_str() {
82                    "x" => x = Some(value.as_f64()?),
83                    "y" => y = Some(value.as_f64()?),
84                    _ => {}
85                }
86            }
87
88            return Ok(Align {
89                x: x.unwrap_or_default(),
90                y: y.unwrap_or_default(),
91            });
92        }
93
94        Err(err_from_to!("Struct" => "Align"))
95    }
96}
97
98impl TryFrom<f64> for Align {
99    type Error = Error;
100
101    fn try_from(value: f64) -> Result<Self, Self::Error> {
102        Ok(Align { x: value, y: value })
103    }
104}
105
106try_from_value_ref_struct! {
107    Align, "Align", f64
108}
109
110struct_float_to_tokens! {
111    Align{x, y}
112}