1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { IObj } from '../store/slices/options'; 17 18export interface ICodeFetch { 19 code: string 20 disassemble: boolean 21 options: IObj | null 22} 23export interface ICodeReq { 24 compile: { 25 output: string, 26 error: string, 27 exit_code: number 28 }, 29 disassembly: { 30 output: string, 31 code: string, 32 error: string, 33 exit_code: number 34 } 35} 36export interface IRunReq { 37 compile: { 38 output: string, 39 error: string, 40 exit_code: number 41 }, 42 disassembly: { 43 output: string, 44 code: string, 45 error: string, 46 exit_code: number 47 }, 48 run: { 49 output: string, 50 error: string, 51 exit_code: number 52 }, 53} 54 55export const codeModel = { 56 fillDefaults: <T extends Record<keyof T, string | number>>( 57 data: Partial<T>, 58 defaults: T 59 ): T => { 60 return Object.keys(defaults).reduce((acc, key) => { 61 const typedKey = key as keyof T; 62 acc[typedKey] = data[typedKey] ?? defaults[typedKey]; 63 return acc; 64 }, { ...defaults }); 65 }, 66 fromApiCompile: (data: ICodeReq): ICodeReq => ({ 67 compile: codeModel.fillDefaults(data.compile || {}, { output: '', error: '', exit_code: -1 }), 68 disassembly: codeModel.fillDefaults(data.disassembly || {}, { output: '', code: '', error: '', exit_code: -1 }), 69 }), 70 fromApiRun: (data: IRunReq): IRunReq => ({ 71 compile: codeModel.fillDefaults(data.compile || {}, { output: '', error: '', exit_code: -1 }), 72 disassembly: codeModel.fillDefaults(data.disassembly || {}, { output: '', code: '', error: '', exit_code: -1 }), 73 run: codeModel.fillDefaults(data.run || {}, { output: '', error: '', exit_code: -1 }), 74 }), 75}; 76