Skip to content

Commit cc2b10f

Browse files
committed
type_checkers: encode
1 parent 31ebd0c commit cc2b10f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/type_checkers/encode.cr

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -----------------------------------------------------------------------
2+
# This file is part of MoonScript
3+
#
4+
# MoonSript is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# MoonSript is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with MoonSript. If not, see <https://www.gnu.org/licenses/>.
16+
#
17+
# Copyright (C) 2025 Krisna Pranav, MoonScript Developers
18+
# -----------------------------------------------------------------------
19+
20+
module MoonScript
21+
class TypeChecker
22+
def check(node : Ast::Encode) : Checkable
23+
expression =
24+
case item = node.expression
25+
when Ast::Record
26+
resolve item, true
27+
else
28+
resolve item
29+
end
30+
31+
error! :encode_complex_type do
32+
snippet "This type cannot be automatically encoded:", expression
33+
34+
block do
35+
text "Only these types and records containing them can"
36+
text "be automatically decoded:"
37+
end
38+
39+
snippet <<-MOON
40+
Map(String, a)
41+
Array(a)
42+
Maybe(a)
43+
String
44+
Number
45+
Object
46+
Time
47+
Bool
48+
MOON
49+
50+
snippet "The encode in question is here:", node
51+
end unless check_decode(expression)
52+
53+
OBJECT
54+
end
55+
end
56+
end

0 commit comments

Comments
 (0)