forked from DioxusLabs/dioxus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.rs
202 lines (178 loc) · 4.38 KB
/
metadata.rs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#[derive(Debug, Clone, PartialEq, Eq)]
/// Information about a hook call or function.
pub struct HookInfo {
/// The name of the hook, e.g. `use_state`.
pub name: String,
/// The span of the hook, e.g. `use_state(cx, || 0)`.
pub span: Span,
/// The span of the name, e.g. `use_state`.
pub name_span: Span,
}
impl HookInfo {
pub const fn new(span: Span, name_span: Span, name: String) -> Self {
Self {
span,
name_span,
name,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConditionalInfo {
If(IfInfo),
Match(MatchInfo),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IfInfo {
/// The span of the `if` statement, e.g. `if true { ... }`.
pub span: Span,
/// The span of the `if true` part only.
pub head_span: Span,
}
impl IfInfo {
pub const fn new(span: Span, head_span: Span) -> Self {
Self { span, head_span }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MatchInfo {
/// The span of the `match` statement, e.g. `match true { ... }`.
pub span: Span,
/// The span of the `match true` part only.
pub head_span: Span,
}
impl MatchInfo {
pub const fn new(span: Span, head_span: Span) -> Self {
Self { span, head_span }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// Information about one of the possible loop types.
pub enum AnyLoopInfo {
For(ForInfo),
While(WhileInfo),
Loop(LoopInfo),
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// Information about a `for` loop.
pub struct ForInfo {
pub span: Span,
pub head_span: Span,
}
impl ForInfo {
pub const fn new(span: Span, head_span: Span) -> Self {
Self { span, head_span }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// Information about a `while` loop.
pub struct WhileInfo {
pub span: Span,
pub head_span: Span,
}
impl WhileInfo {
pub const fn new(span: Span, head_span: Span) -> Self {
Self { span, head_span }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// Information about a `loop` loop.
pub struct LoopInfo {
pub span: Span,
}
impl LoopInfo {
pub const fn new(span: Span) -> Self {
Self { span }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// Information about a closure.
pub struct ClosureInfo {
pub span: Span,
}
impl ClosureInfo {
pub const fn new(span: Span) -> Self {
Self { span }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// Information about a component function.
pub struct ComponentInfo {
pub span: Span,
pub name: String,
pub name_span: Span,
}
impl ComponentInfo {
pub const fn new(span: Span, name: String, name_span: Span) -> Self {
Self {
span,
name,
name_span,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// Information about a non-component, non-hook function.
pub struct FnInfo {
pub span: Span,
pub name: String,
pub name_span: Span,
}
impl FnInfo {
pub const fn new(span: Span, name: String, name_span: Span) -> Self {
Self {
span,
name,
name_span,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// A span of text in a source code file.
pub struct Span {
pub source_text: Option<String>,
pub start: LineColumn,
pub end: LineColumn,
}
impl Span {
pub fn new_from_str(source_text: &str, start: LineColumn) -> Self {
let mut lines = source_text.lines();
let first_line = lines.next().unwrap_or_default();
let mut end = LineColumn {
line: start.line,
column: start.column + first_line.len(),
};
for line in lines {
end.line += 1;
end.column = line.len();
}
Self {
source_text: Some(source_text.to_string()),
start,
end,
}
}
}
impl From<proc_macro2::Span> for Span {
fn from(span: proc_macro2::Span) -> Self {
Self {
source_text: span.source_text(),
start: span.start().into(),
end: span.end().into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// A location in a source code file.
pub struct LineColumn {
pub line: usize,
pub column: usize,
}
impl From<proc_macro2::LineColumn> for LineColumn {
fn from(lc: proc_macro2::LineColumn) -> Self {
Self {
line: lc.line,
column: lc.column,
}
}
}