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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use std::cell::RefCell;
use crate::ast::span::Span;
use crate::ast::doc_comment::DocComment;
use crate::ast::decorator::Decorator;
use crate::ast::type_expr::TypeExpr;
use crate::ast::identifier::Identifier;
use crate::ast::reference_space::ReferenceSpace;
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::format::Writer;
use crate::traits::has_availability::HasAvailability;
use crate::traits::info_provider::InfoProvider;
use crate::traits::resolved::Resolve;
use crate::traits::write::Write;

#[derive(Debug, Copy, Clone)]
pub enum FieldHint {
    ModelField,
    InterfaceField,
}

#[derive(Debug, Copy, Clone)]
pub struct ModelPrimitiveFieldSettings {
    pub dropped: bool,
    pub r#virtual: bool,
}

#[derive(Debug, Copy, Clone)]
pub struct ModelRelationSettings {
    pub direct: bool,
}

#[derive(Debug, Copy, Clone)]
pub struct ModelPropertyFieldSettings {
   pub cached: bool,
}

#[derive(Debug, Copy, Clone)]
pub enum FieldClass {
    ModelPrimitiveField(ModelPrimitiveFieldSettings),
    ModelRelation(ModelRelationSettings),
    ModelProperty(ModelPropertyFieldSettings),
    InterfaceField,
    ConfigDeclarationField,
}

impl FieldClass {

    pub fn is_model_relation(&self) -> bool {
        self.as_model_relation().is_some()
    }

    pub fn as_model_relation(&self) -> Option<&ModelRelationSettings> {
        match self {
            FieldClass::ModelRelation(s) => Some(s),
            _ => None,
        }
    }

    pub fn is_model_primitive_field(&self) -> bool {
        self.as_model_primitive_field().is_some()
    }

    pub fn as_model_primitive_field(&self) -> Option<&ModelPrimitiveFieldSettings> {
        match self {
            FieldClass::ModelPrimitiveField(s) => Some(s),
            _ => None,
        }
    }

    pub fn is_model_property(&self) -> bool {
        self.as_model_property().is_some()
    }

    pub fn as_model_property(&self) -> Option<&ModelPropertyFieldSettings> {
        match self {
            FieldClass::ModelProperty(s) => Some(s),
            _ => None,
        }
    }

    pub fn is_interface_field(&self) -> bool {
        match self {
            FieldClass::InterfaceField => true,
            _ => false,
        }
    }

    pub fn is_model_field(&self) -> bool {
        self.is_model_field() ||
        self.is_model_relation() ||
        self.is_model_property()
    }

    pub fn reference_type(&self) -> ReferenceSpace {
        match self {
            FieldClass::ModelPrimitiveField(_) => ReferenceSpace::ModelFieldDecorator,
            FieldClass::ModelRelation(_) => ReferenceSpace::ModelRelationDecorator,
            FieldClass::ModelProperty(_) => ReferenceSpace::ModelPropertyDecorator,
            FieldClass::InterfaceField => ReferenceSpace::InterfaceFieldDecorator,
            FieldClass::ConfigDeclarationField => ReferenceSpace::Default,
        }
    }
}

#[derive(Debug)]
pub struct FieldResolved {
    pub class: FieldClass,
}

declare_container_node!(Field, 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) type_expr: usize,
    pub(crate) resolved: RefCell<Option<FieldResolved>>,
);

impl_container_node_defaults!(Field, named, availability);

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

impl Field {

    node_optional_child_fn!(comment, DocComment);

    node_children_iter_fn!(decorators, DecoratorsIter);

    node_child_fn!(identifier, Identifier);

    node_child_fn!(type_expr, TypeExpr);
}

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

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

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

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