• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024-2025 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    verifier: boolean
22    runtime_verify?: boolean
23    options: IObj | null
24}
25export interface ICodeShare {
26    code: string
27    options: IObj | null
28}
29export interface IShareReq {
30    uuid: string
31}
32export interface ICodeReq {
33    compile: {
34        output: string,
35        error: string,
36        exit_code?: number
37    },
38    disassembly: {
39        output: string,
40        code: string,
41        error: string,
42        exit_code?: number
43    },
44    verifier: {
45        output: string,
46        error: string,
47        exit_code?: number
48    }
49}
50export interface IRunReq {
51    compile: {
52        output: string,
53        error: string,
54        exit_code?: number
55    },
56    disassembly: {
57        output: string,
58        code: string,
59        error: string,
60        exit_code?: number
61    },
62    run: {
63        output: string,
64        error: string,
65        exit_code?: number
66    },
67    verifier: {
68        output: string,
69        error: string,
70        exit_code?: number
71    }
72}
73
74export const codeModel = {
75    fillDefaults: <T extends Record<keyof T, string | number>>(
76        data: Partial<T>,
77        defaults: T
78    ): T => {
79        return Object.keys({...defaults, exit_code: null}).reduce((acc, key) => {
80            const typedKey = key as keyof T;
81            acc[typedKey] = data[typedKey] ?? defaults[typedKey];
82            return acc;
83        }, { ...defaults });
84    },
85    fromApiCompile: (data: ICodeReq): ICodeReq => ({
86        compile: codeModel.fillDefaults(data.compile || {}, { output: '', error: '' }),
87        disassembly: codeModel.fillDefaults(data.disassembly || {}, { output: '', code: '', error: '' }),
88        verifier: codeModel.fillDefaults(data.verifier || {}, { output: '', error: '' }),
89    }),
90    fromApiRun: (data: IRunReq): IRunReq => ({
91        compile: codeModel.fillDefaults(data.compile || {}, { output: '', error: '' }),
92        disassembly: codeModel.fillDefaults(data.disassembly || {}, { output: '', code: '', error: '' }),
93        run: codeModel.fillDefaults(data.run || {}, { output: '', error: '' }),
94        verifier: codeModel.fillDefaults(data.verifier || {}, { output: '', error: '' }),
95    }),
96};
97