MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / parserSetTemplate

Method parserSetTemplate

app/src/API/Handlers/ProjectHandler.cpp:3173–3207  ·  view source on GitHub ↗

* @brief Select a Native frame parser template (and flip the source to Native). */

Source from the content-addressed store, hash-verified

3171 * @brief Select a Native frame parser template (and flip the source to Native).
3172 */
3173API::CommandResponse API::Handlers::ProjectHandler::parserSetTemplate(const QString& id,
3174 const QJsonObject& params)
3175{
3176 if (!params.contains(QStringLiteral("template")))
3177 return CommandResponse::makeError(
3178 id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: template"));
3179
3180 const int sourceId = params.contains(Keys::SourceId) ? params.value(Keys::SourceId).toInt() : 0;
3181 const auto& sources = DataModel::ProjectModel::instance().sources();
3182 const auto it =
3183 std::find_if(sources.begin(), sources.end(), [sourceId](const DataModel::Source& s) {
3184 return s.sourceId == sourceId;
3185 });
3186
3187 if (it == sources.end())
3188 return CommandResponse::makeError(
3189 id, ErrorCode::InvalidParam, QStringLiteral("Unknown sourceId"));
3190
3191 const QString template_id = params.value(QStringLiteral("template")).toString();
3192 const auto template_params = params.value(QStringLiteral("params")).toObject();
3193
3194 QString error;
3195 if (!validateNativeTemplate(template_id, template_params, error))
3196 return CommandResponse::makeError(id, ErrorCode::InvalidParam, error);
3197
3198 applyNativeTemplate(sourceId, template_id, template_params);
3199
3200 const auto& model = DataModel::ProjectModel::instance();
3201 QJsonObject result;
3202 result[Keys::SourceId] = sourceId;
3203 result[QStringLiteral("language")] = static_cast<int>(SerialStudio::Native);
3204 result[QStringLiteral("template")] = template_id;
3205 result[QStringLiteral("params")] = model.frameParserParams(sourceId);
3206 return CommandResponse::makeSuccess(id, result);
3207}
3208
3209/**
3210 * @brief List all groups with basic info

Callers

nothing calls this directly

Calls 6

validateNativeTemplateFunction · 0.85
applyNativeTemplateFunction · 0.85
beginMethod · 0.80
frameParserParamsMethod · 0.80
containsMethod · 0.45
valueMethod · 0.45

Tested by

no test coverage detected