1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::cell::RefCell;

use indexmap::{IndexMap, IndexSet};
use serde_json::Value;

use crate::{
    schema::{
        assert_string_schema_for_map, generate_map_schema, generate_set_schema, SchemaGenerator,
        SchemaObject,
    },
    str::ConfigurableString,
    Configurable, GenerateError, Metadata, ToValue,
};

impl<K, V> Configurable for IndexMap<K, V>
where
    K: ConfigurableString + ToValue + std::hash::Hash + Eq + 'static,
    V: Configurable + ToValue + 'static,
{
    fn is_optional() -> bool {
        // A hashmap with required fields would be... an object.  So if you want that, make a struct
        // instead, not a hashmap.
        true
    }

    fn metadata() -> Metadata {
        Metadata::with_transparent(true)
    }

    fn validate_metadata(metadata: &Metadata) -> Result<(), GenerateError> {
        let converted = metadata.convert();
        V::validate_metadata(&converted)
    }

    fn generate_schema(gen: &RefCell<SchemaGenerator>) -> Result<SchemaObject, GenerateError> {
        // Make sure our key type is _truly_ a string schema.
        assert_string_schema_for_map(
            &K::as_configurable_ref(),
            gen,
            std::any::type_name::<Self>(),
        )?;

        generate_map_schema(&V::as_configurable_ref(), gen)
    }
}

impl<K, V> ToValue for IndexMap<K, V>
where
    K: ToString,
    V: ToValue,
{
    fn to_value(&self) -> Value {
        Value::Object(
            self.iter()
                .map(|(k, v)| (k.to_string(), v.to_value()))
                .collect(),
        )
    }
}

impl<V> Configurable for IndexSet<V>
where
    V: Configurable + ToValue + std::hash::Hash + Eq + 'static,
{
    fn metadata() -> Metadata {
        Metadata::with_transparent(true)
    }

    fn validate_metadata(metadata: &Metadata) -> Result<(), GenerateError> {
        let converted = metadata.convert();
        V::validate_metadata(&converted)
    }

    fn generate_schema(gen: &RefCell<SchemaGenerator>) -> Result<SchemaObject, GenerateError> {
        generate_set_schema(&V::as_configurable_ref(), gen)
    }
}

impl<V: ToValue> ToValue for IndexSet<V> {
    fn to_value(&self) -> Value {
        Value::Array(self.iter().map(ToValue::to_value).collect())
    }
}