|
| 1 | +import { type Fetcher } from "swr"; |
| 2 | +import { type ReplOutput } from "../repl"; |
| 3 | + |
| 4 | +const WANDBOX = "https://wandbox.org"; |
| 5 | +// https://github.com/melpon/wandbox/blob/ajax/kennel2/API.rst <- 古いけど、説明と例がある |
| 6 | +// https://github.com/melpon/wandbox/blob/master/feline/src/types.rs |
| 7 | +/* RustのVec<u8>はバイト配列ですが、serialize_with = "serialize_utf8"という指定があるため、 |
| 8 | +JSONにシリアライズされる際にはUTF-8文字列に変換されると解釈し、TypeScriptの型はstringとします。 |
| 9 | +by gemini |
| 10 | +*/ |
| 11 | +export interface SwitchOption { |
| 12 | + name: string; |
| 13 | + "display-flags": string; |
| 14 | + "display-name": string; |
| 15 | +} |
| 16 | +export interface SwitchSingle { |
| 17 | + type: "single"; |
| 18 | + name: string; |
| 19 | + "display-name": string; |
| 20 | + "display-flags": string; |
| 21 | + default: boolean; |
| 22 | +} |
| 23 | +export interface SwitchSelect { |
| 24 | + type: "select"; |
| 25 | + name: string; |
| 26 | + options: SwitchOption[]; |
| 27 | + default: string; |
| 28 | +} |
| 29 | +/** |
| 30 | + * Rustの 'Switch' enum に対応するディスクリミネேテッドユニオン型です。 |
| 31 | + * 'type' プロパティの値によって `SwitchSingle` か `SwitchSelect` かを判別できます。 |
| 32 | + */ |
| 33 | +export type Switch = SwitchSingle | SwitchSelect; |
| 34 | +export interface CompilerInfo { |
| 35 | + name: string; |
| 36 | + version: string; |
| 37 | + language: string; |
| 38 | + "display-name": string; |
| 39 | + templates: string[]; |
| 40 | + "compiler-option-raw": boolean; |
| 41 | + "runtime-option-raw": boolean; |
| 42 | + "display-compile-command": string; |
| 43 | + switches: Switch[]; |
| 44 | +} |
| 45 | +interface Code { |
| 46 | + file: string; |
| 47 | + code: string; |
| 48 | +} |
| 49 | +export interface CompileParameter { |
| 50 | + compiler: string; |
| 51 | + code: string; |
| 52 | + codes?: Code[]; |
| 53 | + options?: string; |
| 54 | + stdin?: string; |
| 55 | + "compiler-option-raw"?: string; |
| 56 | + "runtime-option-raw"?: string; |
| 57 | + github_user?: string; |
| 58 | + title?: string; |
| 59 | + description?: string; |
| 60 | + save?: boolean; |
| 61 | + created_at?: number; |
| 62 | + is_private?: boolean; |
| 63 | + "compiler-info"?: CompilerInfo; |
| 64 | +} |
| 65 | +export interface CompileResult { |
| 66 | + status: string; |
| 67 | + signal: string; |
| 68 | + compiler_output: string; |
| 69 | + compiler_error: string; |
| 70 | + compiler_message: string; |
| 71 | + program_output: string; |
| 72 | + program_error: string; |
| 73 | + program_message: string; |
| 74 | + permlink: string; |
| 75 | + url: string; |
| 76 | +} |
| 77 | + |
| 78 | +export const compilerInfoFetcher: Fetcher<CompilerInfo[]> = () => |
| 79 | + fetch(new URL("/api/list.json", WANDBOX)).then( |
| 80 | + (res) => res.json() as Promise<CompilerInfo[]> |
| 81 | + ); |
| 82 | + |
| 83 | +interface CompileProps { |
| 84 | + compilerName: string; |
| 85 | + compilerOptions: string[]; |
| 86 | + compilerOptionsRaw: string[]; |
| 87 | + codes: Code[]; |
| 88 | +} |
| 89 | +export interface CompileResultWithOutput extends CompileResult { |
| 90 | + compilerOutput: ReplOutput[]; |
| 91 | + compilerError: ReplOutput[]; |
| 92 | + programOutput: ReplOutput[]; |
| 93 | + programError: ReplOutput[]; |
| 94 | +} |
| 95 | + |
| 96 | +export async function compileAndRun( |
| 97 | + options: CompileProps |
| 98 | +): Promise<CompileResultWithOutput> { |
| 99 | + const result: CompileResult = await fetch( |
| 100 | + new URL("/api/compile.json", WANDBOX), |
| 101 | + { |
| 102 | + method: "post", |
| 103 | + headers: { |
| 104 | + "Content-Type": "application/json", |
| 105 | + }, |
| 106 | + body: JSON.stringify({ |
| 107 | + compiler: options.compilerName, |
| 108 | + code: "", |
| 109 | + codes: options.codes, |
| 110 | + options: options.compilerOptions.join(","), |
| 111 | + stdin: "", |
| 112 | + "compiler-option-raw": options.compilerOptionsRaw.join("\n"), |
| 113 | + "runtime-option-raw": "", |
| 114 | + save: false, |
| 115 | + is_private: true, |
| 116 | + } satisfies CompileParameter), |
| 117 | + } |
| 118 | + ).then((res) => res.json()); |
| 119 | + return { |
| 120 | + ...result, |
| 121 | + compilerOutput: result.compiler_output.trim() |
| 122 | + ? result.compiler_output |
| 123 | + .trim() |
| 124 | + .split("\n") |
| 125 | + .map((line) => ({ type: "stdout" as const, message: line })) |
| 126 | + : [], |
| 127 | + compilerError: result.compiler_error.trim() |
| 128 | + ? result.compiler_error |
| 129 | + .trim() |
| 130 | + .split("\n") |
| 131 | + .map((line) => ({ type: "error" as const, message: line })) |
| 132 | + : [], |
| 133 | + programOutput: result.program_output.trim() |
| 134 | + ? result.program_output |
| 135 | + .trim() |
| 136 | + .split("\n") |
| 137 | + .map((line) => ({ type: "stdout" as const, message: line })) |
| 138 | + : [], |
| 139 | + programError: result.program_error.trim() |
| 140 | + ? result.program_error |
| 141 | + .trim() |
| 142 | + .split("\n") |
| 143 | + .map((line) => ({ type: "error" as const, message: line })) |
| 144 | + : [], |
| 145 | + }; |
| 146 | +} |
0 commit comments