Skip to main content

scratch_test_model/attrs/
color.rs

1use crate::{
2    attrs::{AttributeContentError, ParseJsonBlockAttribute},
3    aux::constants,
4};
5
6#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, derive_getters::Getters)]
7pub struct Color {
8    hex: svalue::ARc<str>,
9}
10
11impl ParseJsonBlockAttribute for Color {
12    fn parse_json_block_attr_without_expecting_shadow<'a>(
13        _all_target_blocks: crate::_exports::JsonBlocks<'a>,
14        attribute_value: &'a serde_json::Value,
15    ) -> Result<Self, crate::aux::errors::AttributeContentError<'a>>
16    where
17        Self: Sized,
18    {
19        if let Some(array) = attribute_value.as_array() {
20            if let Some(code) = array.first().and_then(|x| x.as_u64()) {
21                if constants::COLOR_PICKER_PRIMITIVE == code {
22                    if let Some(hex) = array.get(1).and_then(|v| v.as_str()) {
23                        Ok(Self { hex: hex.into() })
24                    } else {
25                        Err(AttributeContentError::Other(
26                            "value is invalid hex code for color picker",
27                        ))
28                    }
29                } else {
30                    Err(AttributeContentError::Other(
31                        "numeric code invalid for color picker",
32                    ))
33                }
34            } else {
35                Err(AttributeContentError::Other(
36                    "expected numeric code for color picker",
37                ))
38            }
39        } else {
40            Err(AttributeContentError::ExpectedArray {
41                value: attribute_value,
42                specified_length: Some(2),
43            })
44        }
45    }
46}