-
Notifications
You must be signed in to change notification settings - Fork 48
/
IonPrettyTextWriter.ts
184 lines (166 loc) · 5.5 KB
/
IonPrettyTextWriter.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*!
* Copyright 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import { CharCodes } from "./IonText";
import { State, TextWriter } from "./IonTextWriter";
import { IonType } from "./IonType";
import { IonTypes } from "./IonTypes";
import { Writeable } from "./IonWriteable";
type Serializer<T> = (value: T) => void;
/*
* This class and functionality carry no guarantees of correctness or support.
* Do not rely on this functionality for more than front end formatting.
*/
export class PrettyTextWriter extends TextWriter {
private indentCount: number = 0;
constructor(writeable: Writeable, private readonly indentSize: number = 2) {
super(writeable);
}
writeFieldName(fieldName: string): void {
if (this.currentContainer.containerType !== IonTypes.STRUCT) {
throw new Error("Cannot write field name outside of a struct");
}
if (this.currentContainer.state !== State.STRUCT_FIELD) {
throw new Error("Expecting a struct value");
}
if (!this.currentContainer.clean) {
this.writeable.writeByte(CharCodes.COMMA);
this.writePrettyNewLine(0);
}
this.writePrettyIndent(0);
this.writeSymbolToken(fieldName);
this.writeable.writeByte(CharCodes.COLON);
this.writeable.writeByte(CharCodes.SPACE);
this.currentContainer.state = State.VALUE;
}
writeNull(type: IonType): void {
if (type === undefined || type === null) {
type = IonTypes.NULL;
}
this.handleSeparator();
this.writePrettyValue();
this.writeAnnotations();
this._writeNull(type);
if (this.currentContainer.containerType === IonTypes.STRUCT) {
this.currentContainer.state = State.STRUCT_FIELD;
}
}
stepOut(): void {
const currentContainer = this.containerContext.pop();
if (!currentContainer || !currentContainer.containerType) {
throw new Error("Can't step out when not in a container");
} else if (
currentContainer.containerType === IonTypes.STRUCT &&
currentContainer.state === State.VALUE
) {
throw new Error("Expecting a struct value");
}
if (!currentContainer.clean) {
this.writePrettyNewLine(0);
}
this.writePrettyIndent(-1);
switch (currentContainer.containerType) {
case IonTypes.LIST:
this.writeable.writeByte(CharCodes.RIGHT_BRACKET);
break;
case IonTypes.SEXP:
this.writeable.writeByte(CharCodes.RIGHT_PARENTHESIS);
break;
case IonTypes.STRUCT:
this.writeable.writeByte(CharCodes.RIGHT_BRACE);
break;
default:
throw new Error("Unexpected container type");
}
}
_serializeValue<T>(type: IonType, value: T, serialize: Serializer<T>) {
if (this.currentContainer.state === State.STRUCT_FIELD) {
throw new Error("Expecting a struct field");
}
if (value === null) {
this.writeNull(type);
return;
}
this.handleSeparator();
this.writePrettyValue();
this.writeAnnotations();
serialize(value);
if (this.currentContainer.containerType === IonTypes.STRUCT) {
this.currentContainer.state = State.STRUCT_FIELD;
}
}
writeContainer(type: IonType, openingCharacter: number): void {
if (
this.currentContainer.containerType === IonTypes.STRUCT &&
this.currentContainer.state === State.VALUE
) {
this.currentContainer.state = State.STRUCT_FIELD;
}
this.handleSeparator();
this.writePrettyValue();
this.writeAnnotations();
this.writeable.writeByte(openingCharacter);
this.writePrettyNewLine(1);
this._stepIn(type);
}
handleSeparator(): void {
if (this.depth() === 0) {
if (this.currentContainer.clean) {
this.currentContainer.clean = false;
} else {
this.writeable.writeByte(CharCodes.LINE_FEED);
}
} else {
if (this.currentContainer.clean) {
this.currentContainer.clean = false;
} else {
switch (this.currentContainer.containerType) {
case IonTypes.LIST:
this.writeable.writeByte(CharCodes.COMMA);
this.writePrettyNewLine(0);
break;
case IonTypes.SEXP:
this.writeable.writeByte(CharCodes.SPACE);
this.writePrettyNewLine(0);
break;
default:
// no op
}
}
}
}
private writePrettyValue(): void {
if (
this.depth() > 0 &&
this.currentContainer.containerType &&
this.currentContainer.containerType !== IonTypes.STRUCT
) {
this.writePrettyIndent(0);
}
}
private writePrettyNewLine(incrementValue: number): void {
this.indentCount = this.indentCount + incrementValue;
if (this.indentSize && this.indentSize > 0) {
this.writeable.writeByte(CharCodes.LINE_FEED);
}
}
private writePrettyIndent(incrementValue: number): void {
this.indentCount = this.indentCount + incrementValue;
if (this.indentSize && this.indentSize > 0) {
for (let i = 0; i < this.indentCount * this.indentSize; i++) {
this.writeable.writeByte(CharCodes.SPACE);
}
}
}
}