teo_parser/ast/
handler.rs

1use serde::Serialize;
2use crate::ast::doc_comment::DocComment;
3use crate::ast::decorator::Decorator;
4use crate::ast::type_expr::{TypeExpr};
5use crate::ast::identifier::Identifier;
6use crate::ast::span::Span;
7use crate::{declare_container_node, impl_container_node_defaults, node_child_fn, node_children_iter, node_children_iter_fn, node_optional_child_fn};
8use crate::ast::empty_decorator::EmptyDecorator;
9use crate::format::Writer;
10use crate::traits::has_availability::HasAvailability;
11use crate::traits::info_provider::InfoProvider;
12use crate::traits::write::Write;
13
14declare_container_node!(HandlerGroupDeclaration, named, availability,
15    pub(crate) comment: Option<usize>,
16    pub(crate) identifier: usize,
17    pub(crate) handler_declarations: Vec<usize>,
18    pub(crate) decorators: Vec<usize>,
19    pub(crate) empty_decorators: Vec<usize>,
20    pub(crate) unattached_decorators: Vec<usize>,
21);
22
23impl_container_node_defaults!(HandlerGroupDeclaration, named, availability);
24
25node_children_iter!(HandlerGroupDeclaration, HandlerDeclaration, HandlerDeclarationsIter, handler_declarations);
26
27node_children_iter!(HandlerGroupDeclaration, Decorator, GroupDecoratorsIter, decorators);
28
29node_children_iter!(HandlerGroupDeclaration, Decorator, GroupUnattachedDecoratorsIter, unattached_decorators);
30
31node_children_iter!(HandlerGroupDeclaration, EmptyDecorator, GroupEmptyDecoratorsIter, empty_decorators);
32
33impl HandlerGroupDeclaration {
34
35    node_optional_child_fn!(comment, DocComment);
36
37    node_child_fn!(identifier, Identifier);
38
39    node_children_iter_fn!(handler_declarations, HandlerDeclarationsIter);
40
41    node_children_iter_fn!(decorators, GroupDecoratorsIter);
42
43    node_children_iter_fn!(empty_decorators, GroupEmptyDecoratorsIter);
44
45    node_children_iter_fn!(unattached_decorators, GroupUnattachedDecoratorsIter);
46}
47
48impl InfoProvider for HandlerGroupDeclaration {
49    fn namespace_skip(&self) -> usize {
50        1
51    }
52}
53
54declare_container_node!(HandlerDeclaration, named, availability,
55    pub(crate) comment: Option<usize>,
56    pub(crate) decorators: Vec<usize>,
57    pub(crate) empty_decorators: Vec<usize>,
58    pub(crate) identifier: usize,
59    pub(crate) input_type: Option<usize>,
60    pub(crate) output_type: usize,
61    pub input_format: HandlerInputFormat,
62    pub nonapi: bool,
63    pub inside_group: bool,
64);
65
66impl_container_node_defaults!(HandlerDeclaration, named, availability);
67
68node_children_iter!(HandlerDeclaration, Decorator, DecoratorsIter, decorators);
69
70node_children_iter!(HandlerDeclaration, EmptyDecorator, EmptyDecoratorsIter, empty_decorators);
71
72impl HandlerDeclaration {
73
74    node_optional_child_fn!(comment, DocComment);
75
76    node_children_iter_fn!(decorators, DecoratorsIter);
77
78    node_children_iter_fn!(empty_decorators, EmptyDecoratorsIter);
79
80    node_child_fn!(identifier, Identifier);
81
82    node_optional_child_fn!(input_type, TypeExpr);
83
84    node_child_fn!(output_type, TypeExpr);
85}
86
87#[derive(Debug, Clone, Copy, Serialize)]
88pub enum HandlerInputFormat {
89    Json,
90    Form,
91}
92
93impl HandlerInputFormat {
94
95    pub fn is_json(&self) -> bool {
96        match self {
97            HandlerInputFormat::Json => true,
98            _ => false,
99        }
100    }
101
102    pub fn is_form(&self) -> bool {
103        match self {
104            HandlerInputFormat::Form => true,
105            _ => false,
106        }
107    }
108}
109
110impl InfoProvider for HandlerDeclaration {
111    fn namespace_skip(&self) -> usize {
112        if self.inside_group {
113            2
114        } else {
115            1
116        }
117    }
118}
119
120impl Write for HandlerGroupDeclaration {
121    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
122        writer.write_children(self, self.children.values())
123    }
124
125    fn is_block_level_element(&self) -> bool {
126        true
127    }
128}
129
130impl Write for HandlerDeclaration {
131    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
132        writer.write_children(self, self.children.values())
133    }
134
135    fn is_block_level_element(&self) -> bool {
136        true
137    }
138}