KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcm_data.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2021 Andrew Lutsenko, anlutsenko at gmail dot com
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "pcm_data.h"
22
23#include <wx/log.h>
24
25static const wxChar tracePcm[] = wxT( "KICAD_PCM" );
26
27
28template <typename T>
29void to_optional( const json& j, const char* key, std::optional<T>& dest )
30{
31 if( j.contains( key ) )
32 {
33 T tmp;
34 j.at( key ).get_to( tmp );
35 dest.emplace( tmp );
36 }
37}
38
39
40void to_json( json& j, const PACKAGE_VERSION& v )
41{
42 j = json{ { "version", v.version },
43 { "status", v.status },
44 { "kicad_version", v.kicad_version } };
45
46 if( v.version_epoch )
47 j["version_epoch"] = *v.version_epoch;
48
49 if( v.download_url )
50 j["download_url"] = *v.download_url;
51
52 if( v.download_sha256 )
53 j["download_sha256"] = *v.download_sha256;
54
55 if( v.download_size )
56 j["download_size"] = *v.download_size;
57
58 if( v.install_size )
59 j["install_size"] = *v.install_size;
60
61 if( v.runtime )
62 j["runtime"] = *v.runtime;
63
64 if( v.platforms.size() > 0 )
65 nlohmann::to_json( j["platforms"], v.platforms );
66
67 if( v.kicad_version_max )
68 j["kicad_version_max"] = *v.kicad_version_max;
69
70 if( v.keep_on_update.size() > 0 )
71 nlohmann::to_json( j["keep_on_update"], v.keep_on_update );
72}
73
74
75void from_json( const json& j, PACKAGE_VERSION& v )
76{
77 j.at( "version" ).get_to( v.version );
78 j.at( "status" ).get_to( v.status );
79 j.at( "kicad_version" ).get_to( v.kicad_version );
80
81 to_optional( j, "version_epoch", v.version_epoch );
82 to_optional( j, "download_url", v.download_url );
83 to_optional( j, "download_sha256", v.download_sha256 );
84 to_optional( j, "download_size", v.download_size );
85 to_optional( j, "install_size", v.install_size );
86 to_optional( j, "kicad_version_max", v.kicad_version_max );
87 to_optional( j, "runtime", v.runtime );
88
89 if( j.contains( "platforms" ) )
90 j.at( "platforms" ).get_to( v.platforms );
91
92 if( j.contains( "keep_on_update" ) )
93 j.at( "keep_on_update" ).get_to( v.keep_on_update );
94}
95
96
97void to_json( json& j, const PCM_PACKAGE& p )
98{
99 j = json{ { "name", p.name },
100 { "description", p.description },
101 { "description_full", p.description_full },
102 { "identifier", p.identifier },
103 { "type", p.type },
104 { "author", p.author },
105 { "license", p.license },
106 { "resources", p.resources },
107 { "versions", p.versions } };
108
109 if( p.maintainer )
110 j["maintainer"] = *p.maintainer;
111
112 if( p.tags.size() > 0 )
113 j["tags"] = p.tags;
114
115 if( p.keep_on_update.size() > 0 )
116 j["keep_on_update"] = p.keep_on_update;
117}
118
119
120void from_json( const json& j, PCM_PACKAGE& p )
121{
122 j.at( "name" ).get_to( p.name );
123 j.at( "description" ).get_to( p.description );
124 j.at( "description_full" ).get_to( p.description_full );
125 j.at( "identifier" ).get_to( p.identifier );
126 j.at( "type" ).get_to( p.type );
127
128 if( p.type == PT_INVALID )
129 {
130 wxLogTrace( tracePcm, wxS( "Skipping package '%s' with unrecognized type '%s'" ),
131 p.identifier, wxString( j.at( "type" ).get<std::string>() ) );
132 }
133
134 j.at( "author" ).get_to( p.author );
135 j.at( "license" ).get_to( p.license );
136 j.at( "resources" ).get_to( p.resources );
137 j.at( "versions" ).get_to( p.versions );
138
139 to_optional( j, "maintainer", p.maintainer );
140 to_optional( j, "category", p.category );
141
142 if( p.type == PT_PLUGIN && p.category && p.category.value() == PC_FAB )
143 p.type = PT_FAB;
144
145 if( j.contains( "tags" ) )
146 j.at( "tags" ).get_to( p.tags );
147
148 if( j.contains( "keep_on_update" ) )
149 j.at( "keep_on_update" ).get_to( p.keep_on_update );
150}
151
152
154{
155 j = json{ { "url", r.url }, { "update_timestamp", r.update_timestamp } };
156
157 if( r.sha256 )
158 j["sha256"] = *r.sha256;
159}
160
161
163{
164 j.at( "url" ).get_to( r.url );
165 j.at( "update_timestamp" ).get_to( r.update_timestamp );
166
167 to_optional( j, "sha256", r.sha256 );
168}
169
170
171void to_json( json& j, const PCM_REPOSITORY& r )
172{
173 j = json{ { "name", r.name }, { "packages", r.packages } };
174
175 if( r.resources )
176 j["resources"] = *r.resources;
177
178 if( r.manifests )
179 j["manifests"] = *r.manifests;
180
181 if( r.maintainer )
182 j["maintainer"] = *r.maintainer;
183}
184
185
186void from_json( const json& j, PCM_REPOSITORY& r )
187{
188 j.at( "name" ).get_to( r.name );
189 j.at( "packages" ).get_to( r.packages );
190
191 to_optional( j, "resources", r.resources );
192 to_optional( j, "manifests", r.manifests );
193 to_optional( j, "maintainer", r.maintainer );
194}
195
196
198{
199 j = json{ { "package", e.package },
200 { "current_version", e.current_version },
201 { "repository_id", e.repository_id },
202 { "repository_name", e.repository_name },
203 { "install_timestamp", e.install_timestamp },
204 { "pinned", e.pinned } };
205}
206
207
209{
210 j.at( "package" ).get_to( e.package );
211 j.at( "current_version" ).get_to( e.current_version );
212 j.at( "repository_id" ).get_to( e.repository_id );
213 j.at( "repository_name" ).get_to( e.repository_name );
214 j.at( "install_timestamp" ).get_to( e.install_timestamp );
215
216 e.pinned = false;
217 if( j.contains( "pinned" ) )
218 j.at( "pinned" ).get_to( e.pinned );
219}
nlohmann::json json
Definition gerbview.cpp:50
static const wxChar tracePcm[]
Flag to enable PCM debugging output.
Definition pcm.cpp:59
void to_json(json &j, const PACKAGE_VERSION &v)
Definition pcm_data.cpp:40
void to_optional(const json &j, const char *key, std::optional< T > &dest)
Definition pcm_data.cpp:29
void from_json(const json &j, PACKAGE_VERSION &v)
Definition pcm_data.cpp:75
@ PC_FAB
Definition pcm_data.h:56
@ PT_INVALID
Definition pcm_data.h:43
@ PT_PLUGIN
Definition pcm_data.h:44
@ PT_FAB
Definition pcm_data.h:45
< Package version metadataPackage metadata
Definition pcm_data.h:92
std::optional< uint64_t > download_size
Definition pcm_data.h:97
std::optional< uint64_t > install_size
Definition pcm_data.h:98
wxString version
Definition pcm_data.h:93
PCM_PACKAGE_VERSION_STATUS status
Definition pcm_data.h:99
std::optional< wxString > kicad_version_max
Definition pcm_data.h:102
std::optional< wxString > download_url
Definition pcm_data.h:95
std::optional< PCM_PACKAGE_RUNTIME > runtime
Definition pcm_data.h:104
std::optional< wxString > download_sha256
Definition pcm_data.h:96
std::optional< int > version_epoch
Definition pcm_data.h:94
std::vector< std::string > platforms
Definition pcm_data.h:100
wxString kicad_version
Definition pcm_data.h:101
std::vector< std::string > keep_on_update
Definition pcm_data.h:103
Definition pcm_data.h:159
wxString repository_name
Definition pcm_data.h:163
PCM_PACKAGE package
Definition pcm_data.h:160
uint64_t install_timestamp
Definition pcm_data.h:164
wxString repository_id
Definition pcm_data.h:162
wxString current_version
Definition pcm_data.h:161
bool pinned
Definition pcm_data.h:165
Repository reference to a resource.
Definition pcm_data.h:114
wxString description
Definition pcm_data.h:116
wxString description_full
Definition pcm_data.h:117
wxString identifier
Definition pcm_data.h:118
wxString license
Definition pcm_data.h:123
std::vector< std::string > tags
Definition pcm_data.h:125
STRING_MAP resources
Definition pcm_data.h:124
std::optional< PCM_CONTACT > maintainer
Definition pcm_data.h:122
wxString name
Definition pcm_data.h:115
std::vector< PACKAGE_VERSION > versions
Definition pcm_data.h:127
std::optional< PCM_PLUGIN_CATEGORY > category
Definition pcm_data.h:120
PCM_PACKAGE_TYPE type
Definition pcm_data.h:119
std::vector< std::string > keep_on_update
Definition pcm_data.h:126
PCM_CONTACT author
Definition pcm_data.h:121
Package installation entry.
Definition pcm_data.h:142
PCM_RESOURCE_REFERENCE packages
Definition pcm_data.h:144
std::optional< PCM_CONTACT > maintainer
Definition pcm_data.h:147
wxString name
Definition pcm_data.h:143
std::optional< PCM_RESOURCE_REFERENCE > manifests
Definition pcm_data.h:146
std::optional< PCM_RESOURCE_REFERENCE > resources
Definition pcm_data.h:145
Repository metadata.
Definition pcm_data.h:133
std::optional< wxString > sha256
Definition pcm_data.h:135
uint64_t update_timestamp
Definition pcm_data.h:136