|
| 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 MoonJson |
| 22 | + class Parser |
| 23 | + def parse_application_meta : Hash(String, String) |
| 24 | + meta = {} of String => String |
| 25 | + |
| 26 | + @parser.read_object do |key| |
| 27 | + value = |
| 28 | + case key |
| 29 | + when "keywords" |
| 30 | + parse_application_meta_keywords |
| 31 | + else |
| 32 | + parse_application_meta_value |
| 33 | + end |
| 34 | + |
| 35 | + meta[key] = value |
| 36 | + end |
| 37 | + |
| 38 | + meta |
| 39 | + rescue JSON::ParseException |
| 40 | + error! :application_meta_invalid do |
| 41 | + block do |
| 42 | + text "The" |
| 43 | + bold "meta field" |
| 44 | + text "of the" |
| 45 | + bold "application object should be string" |
| 46 | + end |
| 47 | + |
| 48 | + snippet snippet_data |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + def parse_application_meta_value : String |
| 53 | + @parser.read_string |
| 54 | + rescue JSON::ParseException |
| 55 | + error! :application_meta_value_invalid do |
| 56 | + block do |
| 57 | + text "The" |
| 58 | + bold "value" |
| 59 | + text "of a" |
| 60 | + bold "meta field should be string" |
| 61 | + end |
| 62 | + |
| 63 | + snippet snippet_data |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def parse_application_meta_keywords : String |
| 68 | + keywords = %w[] |
| 69 | + |
| 70 | + @parser.read_array do |
| 71 | + keywords << parse_application_meta_keyword |
| 72 | + end |
| 73 | + |
| 74 | + keywords.join(',') |
| 75 | + rescue JSON::ParseException |
| 76 | + error! :application_meta_keywords_invalid do |
| 77 | + block do |
| 78 | + text "The" |
| 79 | + bold "keywords field" |
| 80 | + text "of the" |
| 81 | + bold "meta object should be an array." |
| 82 | + end |
| 83 | + |
| 84 | + snippet snippet_data |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + def parse_application_meta_keyword : String |
| 89 | + @parser.read_string |
| 90 | + rescue JSON::ParseException |
| 91 | + error! :application_meta_keyword_invalid do |
| 92 | + block do |
| 93 | + text "A" |
| 94 | + bold "keyword" |
| 95 | + text "should be a string" |
| 96 | + end |
| 97 | + |
| 98 | + snippet snippet_data |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | +end |
0 commit comments