* @brief Set the scripting language for a frame parser source. */
| 3038 | * @brief Set the scripting language for a frame parser source. |
| 3039 | */ |
| 3040 | API::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. |
nothing calls this directly
no test coverage detected