-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
import type { ContentParser, ParseParameters } from "./interface.ts";
import type { DocumentContent } from "../types.ts";
import { isJSONCanvas, type JSONCanvas } from "./json_canvas/types.ts";
export class JSONCanvasParseError extends Error {}
export class InvalidJSONCanvasError extends JSONCanvasParseError {
constructor() {
super("The data is not valid JSONCanvas data");
}
}
export class InvalidJSONError extends JSONCanvasParseError {
constructor(cause: unknown) {
super();
const subMessage = cause instanceof Error ? cause.message : String(cause);
this.message = `JSONCanvas data MUST be valid JSON string: ${subMessage}`;
this.cause = cause;
}
}
export type JSONCanvasDocument = DocumentContent<
"json_canvas",
JSONCanvas
>;
export class JSONCanvasParser implements ContentParser {
async parse({ fileReader }: ParseParameters): Promise<JSONCanvasDocument> {
const text = new TextDecoder().decode(await fileReader.read());
let json: unknown;
try {
json = JSON.parse(text);
} catch (err) {
throw new InvalidJSONError(err);
}
if (!isJSONCanvas(json)) {
throw new InvalidJSONCanvasError();
}
return {
kind: "json_canvas",
content: json,
};
}
}