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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::cell::RefCell;
use indexmap::IndexMap;
use serde::{Serialize};
use crate::ast::doc_comment::DocComment;
use crate::ast::decorator::Decorator;
use crate::ast::field::Field;
use crate::ast::handler::HandlerDeclaration;
use crate::ast::identifier::Identifier;
use crate::ast::span::Span;
use crate::{declare_container_node, impl_container_node_defaults, node_child_fn, node_children_iter, node_children_iter_fn, node_optional_child_fn};
use crate::ast::partial_field::PartialField;
use crate::format::Writer;
use crate::r#type::synthesized_enum::SynthesizedEnum;
use crate::r#type::synthesized_enum_reference::SynthesizedEnumReferenceKind;
use crate::r#type::synthesized_interface_enum::SynthesizedInterfaceEnum;
use crate::r#type::synthesized_interface_enum_reference::SynthesizedInterfaceEnumReferenceKind;
use crate::r#type::synthesized_shape_reference::SynthesizedShapeReferenceKind;
use crate::r#type::Type;
use crate::traits::has_availability::HasAvailability;
use crate::traits::info_provider::InfoProvider;
use crate::traits::resolved::Resolve;
use crate::traits::write::Write;

declare_container_node!(Model, named, availability,
    pub(crate) comment: Option<usize>,
    pub(crate) decorators: Vec<usize>,
    pub(crate) empty_decorator_spans: Vec<Span>,
    pub(crate) identifier: usize,
    pub(crate) fields: Vec<usize>,
    pub(crate) partial_fields: Vec<usize>,
    pub(crate) empty_field_decorator_spans: Vec<Span>,
    pub(crate) unattached_field_decorators: Vec<Decorator>,
    pub(crate) handlers: Vec<usize>,
    pub(crate) resolved: RefCell<Option<ModelResolved>>,
);

impl_container_node_defaults!(Model, named, availability);

node_children_iter!(Model, Decorator, DecoratorsIter, decorators);

node_children_iter!(Model, Field, FieldsIter, fields);

node_children_iter!(Model, PartialField, PartialFieldsIter, partial_fields);

node_children_iter!(Model, HandlerDeclaration, HandlersIter, handlers);

impl Model {

    node_optional_child_fn!(comment, DocComment);

    node_child_fn!(identifier, Identifier);

    node_children_iter_fn!(decorators, DecoratorsIter);

    node_children_iter_fn!(fields, FieldsIter);

    node_children_iter_fn!(partial_fields, PartialFieldsIter);

    node_children_iter_fn!(handlers, HandlersIter);
}

#[derive(Debug, Serialize, Clone)]
pub struct ModelResolved {
    pub enums: IndexMap<SynthesizedEnumReferenceKind, SynthesizedEnum>,
    pub interface_enums: IndexMap<SynthesizedInterfaceEnumReferenceKind, SynthesizedInterfaceEnum>,
    pub shapes: IndexMap<(SynthesizedShapeReferenceKind, Option<String>), Type>,
}

impl ModelResolved {
    
    pub fn new() -> Self {
        Self {
            enums: Default::default(),
            interface_enums: Default::default(),
            shapes: Default::default(),
        }
    }

    pub fn get(&self, key: SynthesizedShapeReferenceKind) -> Option<&Type> {
        self.shapes.get(&(key, None))
    }

    pub fn get_without(&self, key: SynthesizedShapeReferenceKind, without: &str) -> Option<&Type> {
        self.shapes.get(&(key, Some(without.to_owned())))
    }
}

impl Resolve<ModelResolved> for Model {
    fn resolved_ref_cell(&self) -> &RefCell<Option<ModelResolved>> {
        &self.resolved
    }
}

impl InfoProvider for Model {
    fn namespace_skip(&self) -> usize {
        1
    }
}

impl Write for Model {
    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
        writer.write_children(self, self.children.values());
    }

    fn is_block_level_element(&self) -> bool {
        true
    }
}