doc/commands: introduce commands results to json translation
[nit.git] / src / doc / commands / commands_json.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Translate command results to json
16 module commands_json
17
18 import commands::commands_model
19 import commands::commands_graph
20 import commands::commands_usage
21 import commands::commands_catalog
22
23 import model::model_json
24 import catalog::catalog_json
25 import doc::doc_down
26
27 redef class DocCommand
28 # Return a JSON Serializable representation of `self` results
29 fun to_json: nullable Serializable is abstract
30 end
31
32 # Message handling
33
34 redef class CmdMessage
35 # Return a JSON Serializable representation of `self`
36 fun to_json: nullable Serializable do
37 var obj = new JsonObject
38 obj["status"] = class_name
39 obj["message"] = to_s
40 return obj
41 end
42 end
43
44 redef class CmdEntity
45 redef fun to_json do return mentity
46 end
47
48 redef class CmdList
49 redef fun to_json do
50 var obj = new JsonObject
51 obj["results"] = results
52 obj["page"] = page
53 obj["count"] = count
54 obj["limit"] = limit
55 obj["max"] = max
56 return obj
57 end
58 end
59
60 redef class CmdEntityList
61 redef fun to_json do return super
62 end
63
64 # Model commands
65
66 redef class CmdComment
67 redef fun to_json do
68 var obj = new JsonObject
69 var render = self.render
70 if render != null then
71 obj["documentation"] = render.write_to_string
72 end
73 return obj
74 end
75 end
76
77 redef class CmdCode
78 redef fun to_json do
79 var obj = new JsonObject
80 var node = self.node
81 if node != null then
82 obj["location"] = node.location
83 end
84 var output = render
85 if output != null then
86 obj["code"] = output.write_to_string
87 end
88 return obj
89 end
90 end
91
92 redef class CmdGraph
93 redef fun to_json do
94 var obj = new JsonObject
95 var output = render
96 if output != null then
97 obj["graph"] = output.write_to_string
98 end
99 return obj
100 end
101 end
102
103 redef class CmdMetadata
104 redef fun to_json do return metadata
105 end
106
107 # CmdCatalog
108
109 redef class CmdCatalogStats
110 redef fun to_json do return stats
111 end
112
113 redef class CmdCatalogTags
114 redef fun to_json do return packages_count_by_tags
115 end
116
117 redef class CmdCatalogTag
118 redef fun to_json do
119 var obj = super.as(JsonObject)
120 obj["tag"] = tag
121 return obj
122 end
123 end
124
125 redef class CmdCatalogPerson
126 redef fun to_json do return person
127 end
128
129 redef class CmdCatalogMaintaining
130 redef fun to_json do
131 var obj = new JsonObject
132 obj["person"] = person
133 obj["results"] = results
134 obj["page"] = page
135 obj["count"] = count
136 obj["limit"] = limit
137 obj["max"] = max
138 return obj
139 end
140 end
141
142 redef class CmdCatalogContributing
143 redef fun to_json do
144 var obj = new JsonObject
145 obj["person"] = person
146 obj["results"] = results
147 obj["page"] = page
148 obj["count"] = count
149 obj["limit"] = limit
150 obj["max"] = max
151 return obj
152 end
153 end