Skip to main content

scratch_test_model/attrs/
broadcast_id.rs

1use crate::{attrs::AttributeContentError as A, aux::constants};
2
3#[derive(Debug, PartialEq, PartialOrd, Clone, Hash, derive_getters::Getters)]
4pub struct BroadcastId {
5    human_name: svalue::ARc<str>,
6    id: crate::Id,
7}
8
9const BROADCAST_HUMAN_NAME: A<'static> = A::Other("'human name' not present or not string");
10const BROADCAST_ID: A<'static> = A::Other("'id' not present or not string");
11const BROADCAST_ARRAY: A<'static> = A::Other("value is not an array");
12
13impl super::ParseJsonBlockAttribute for BroadcastId {
14    fn parse_json_block_attr_without_expecting_shadow<'a>(
15        _all_target_blocks: crate::_exports::JsonBlocks<'a>,
16        attribute_value: &'a serde_json::Value,
17    ) -> Result<Self, super::AttributeContentError<'a>>
18    where
19        Self: Sized,
20    {
21        if let Some(array) = attribute_value.as_array() {
22            let offset =
23                if array.first().and_then(|o| o.as_u64()) == Some(constants::BROADCAST_PRIMITIVE) {
24                    1
25                } else {
26                    0
27                };
28
29            let human_name = array
30                .get(offset)
31                .and_then(|s| s.as_str())
32                .ok_or(BROADCAST_HUMAN_NAME)?
33                .into();
34            let id = array
35                .get(offset + 1)
36                .and_then(|s| s.as_str())
37                .ok_or(BROADCAST_ID)?
38                .into();
39            return Ok(Self { human_name, id });
40        }
41        Err(BROADCAST_ARRAY)
42    }
43}