Skip to main content

scratch_test_model/attrs/dropdowns/
direct_dropdown.rs

1use crate::attrs::{
2    AttributeContentError, ParseJsonBlockAttribute, StringAtArrayPosZero, dropdowns::AnyDropdownOf,
3};
4
5/// This is a direct dropdown without a block indirection i. e. extra menu block
6#[derive(Debug, PartialEq, Clone, derive_more::Deref)]
7pub struct DirectDropdownOf<T>(pub(super) AnyDropdownOf<T>);
8
9impl<T: FromDirectDropdownString> ParseJsonBlockAttribute for DirectDropdownOf<T> {
10    fn parse_json_block_attr_without_expecting_shadow<'a>(
11        all_target_blocks: crate::_exports::JsonBlocks<'a>,
12        attribute_value: &'a serde_json::Value,
13    ) -> Result<Self, crate::aux::errors::AttributeContentError<'a>>
14    where
15        Self: Sized,
16    {
17        let value = StringAtArrayPosZero::parse_json_block_attr_without_expecting_shadow(
18            all_target_blocks,
19            attribute_value,
20        )?;
21        if let Some(selection) = T::from_direct_dropdown_string(&value) {
22            Ok(Self(AnyDropdownOf(selection)))
23        } else {
24            Err(AttributeContentError::InvalidOptionForDropdown {
25                invalid: value.take(),
26                hint_list: T::VALID_HINT,
27            })
28        }
29    }
30}
31
32pub(crate) trait FromDirectDropdownString {
33    const VALID_HINT: &'static str;
34
35    fn from_direct_dropdown_string(val: &str) -> Option<Self>
36    where
37        Self: Sized;
38}