* @brief Select a Native frame parser template (and flip the source to Native). */
| 3171 | * @brief Select a Native frame parser template (and flip the source to Native). |
| 3172 | */ |
| 3173 | API::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 |
nothing calls this directly
no test coverage detected