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

Method parserSetLanguage

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

* @brief Set the scripting language for a frame parser source. */

Source from the content-addressed store, hash-verified

3038 * @brief Set the scripting language for a frame parser source.
3039 */
3040API::CommandResponse API::Handlers::ProjectHandler::parserSetLanguage(const QString& id,
3041 const QJsonObject& params)
3042{
3043 if (!params.contains(QStringLiteral("language")))
3044 return CommandResponse::makeError(
3045 id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: language"));
3046
3047 const int sourceId = params.contains(Keys::SourceId) ? params.value(Keys::SourceId).toInt() : 0;
3048
3049 const int language = params.value(QStringLiteral("language")).toInt();
3050 if (language != SerialStudio::JavaScript && language != SerialStudio::Lua
3051 && language != SerialStudio::Native)
3052 return CommandResponse::makeError(
3053 id,
3054 ErrorCode::InvalidParam,
3055 QStringLiteral("Invalid language: must be 0 (JavaScript), 1 (Lua) or 2 (Built-In)"));
3056
3057 auto& model = DataModel::ProjectModel::instance();
3058 const auto& sources = model.sources();
3059 const auto it =
3060 std::find_if(sources.begin(), sources.end(), [sourceId](const DataModel::Source& s) {
3061 return s.sourceId == sourceId;
3062 });
3063
3064 if (it == sources.end())
3065 return CommandResponse::makeError(
3066 id, ErrorCode::InvalidParam, QStringLiteral("Unknown sourceId"));
3067
3068 model.updateSourceFrameParserLanguage(sourceId, language);
3069
3070 if (language != SerialStudio::Native || model.frameParserTemplate(sourceId).isEmpty())
3071 DataModel::FrameParser::instance().loadDefaultTemplate(sourceId, true);
3072 else
3073 DataModel::FrameParser::instance().readCode();
3074
3075 QJsonObject result;
3076 result[Keys::SourceId] = sourceId;
3077 result[QStringLiteral("language")] = language;
3078 return CommandResponse::makeSuccess(id, result);
3079}
3080
3081/**
3082 * @brief Get the scripting language for a frame parser source.

Callers

nothing calls this directly

Calls 8

beginMethod · 0.80
isEmptyMethod · 0.80
frameParserTemplateMethod · 0.80
containsMethod · 0.45
valueMethod · 0.45
loadDefaultTemplateMethod · 0.45
readCodeMethod · 0.45

Tested by

no test coverage detected