1use super::{TermDefinition, definition, term_definition};
2use crate::{Container, ContextEntry, Nullable};
3use jstrict::print::{Options, PrecomputeSize, Print, PrintWithSize, Size, string_literal};
4use std::fmt;
5
6impl Print for super::Context {
7 fn fmt_with(&self, f: &mut fmt::Formatter, options: &Options, indent: usize) -> fmt::Result {
8 let mut sizes = Vec::with_capacity(self.traverse().filter(|f| f.is_array() || f.is_object()).count());
9 self.pre_compute_size(options, &mut sizes);
10 let mut index = 0;
11 self.fmt_with_size(f, options, indent, &sizes, &mut index)
12 }
13}
14
15impl PrecomputeSize for super::Context {
16 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
17 match self {
18 Self::One(context) => context.pre_compute_size(options, sizes),
19 Self::Many(contexts) => jstrict::print::pre_compute_array_size(contexts, options, sizes),
20 }
21 }
22}
23
24impl PrintWithSize for super::Context {
25 fn fmt_with_size(&self, f: &mut fmt::Formatter, options: &Options, indent: usize, sizes: &[Size], index: &mut usize) -> fmt::Result {
26 match self {
27 Self::One(context) => context.fmt_with_size(f, options, indent, sizes, index),
28 Self::Many(contexts) => jstrict::print::print_array(contexts, f, options, indent, sizes, index),
29 }
30 }
31}
32
33impl PrecomputeSize for ContextEntry {
34 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
35 match self {
36 ContextEntry::Null => Size::Width(4),
37 ContextEntry::IriRef(r) => Size::Width(jstrict::print::printed_string_size(r.as_str())),
38 ContextEntry::Definition(d) => jstrict::print::pre_compute_object_size(
39 d.iter().map(|entry| {
40 let (key, value) = entry.into_key_value();
41 (key.as_str(), value)
42 }),
43 options,
44 sizes,
45 ),
46 }
47 }
48}
49
50impl PrintWithSize for ContextEntry {
51 fn fmt_with_size(&self, f: &mut fmt::Formatter, options: &Options, indent: usize, sizes: &[Size], index: &mut usize) -> fmt::Result {
52 match self {
53 ContextEntry::Null => write!(f, "null"),
54 ContextEntry::IriRef(r) => string_literal(r.as_str(), f),
55 ContextEntry::Definition(d) => jstrict::print::print_object(
56 d.iter().map(|entry| {
57 let (key, value) = entry.into_key_value();
58 (key.as_str(), value)
59 }),
60 f,
61 options,
62 indent,
63 sizes,
64 index,
65 ),
66 }
67 }
68}
69
70impl PrecomputeSize for definition::EntryValueRef<'_> {
71 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
72 match self {
73 Self::Base(v) => v.pre_compute_size(options, sizes),
74 Self::Import(v) => Size::Width(jstrict::print::printed_string_size(v.as_str())),
75 Self::Language(v) => v.pre_compute_size(options, sizes),
76 Self::Direction(v) => v.pre_compute_size(options, sizes),
77 Self::Propagate(v) => v.pre_compute_size(options, sizes),
78 Self::Protected(v) => v.pre_compute_size(options, sizes),
79 Self::Type(v) => v.pre_compute_size(options, sizes),
80 Self::Version(v) => v.pre_compute_size(options, sizes),
81 Self::Vocab(v) => v.pre_compute_size(options, sizes),
82 Self::Definition(v) => v.pre_compute_size(options, sizes),
83 }
84 }
85}
86
87impl PrintWithSize for definition::EntryValueRef<'_> {
88 fn fmt_with_size(&self, f: &mut fmt::Formatter, options: &Options, indent: usize, sizes: &[Size], index: &mut usize) -> fmt::Result {
89 match self {
90 Self::Base(v) => v.fmt_with(f, options, indent),
91 Self::Import(v) => string_literal(v.as_str(), f),
92 Self::Language(v) => v.fmt_with(f, options, indent),
93 Self::Direction(v) => v.fmt_with(f, options, indent),
94 Self::Propagate(v) => v.fmt_with(f, options, indent),
95 Self::Protected(v) => v.fmt_with(f, options, indent),
96 Self::Type(v) => v.fmt_with_size(f, options, indent, sizes, index),
97 Self::Version(v) => v.fmt_with(f, options, indent),
98 Self::Vocab(v) => v.fmt_with(f, options, indent),
99 Self::Definition(v) => v.fmt_with_size(f, options, indent, sizes, index),
100 }
101 }
102}
103
104impl PrecomputeSize for definition::Type {
105 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
106 jstrict::print::pre_compute_object_size(self.iter().map(|entry| (entry.key().as_str(), entry)), options, sizes)
107 }
108}
109
110impl PrintWithSize for definition::Type {
111 fn fmt_with_size(&self, f: &mut fmt::Formatter, options: &Options, indent: usize, sizes: &[Size], index: &mut usize) -> fmt::Result {
112 jstrict::print::print_object(self.iter().map(|entry| (entry.key().as_str(), entry)), f, options, indent, sizes, index)
113 }
114}
115
116impl PrecomputeSize for definition::ContextTypeEntry {
117 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
118 match self {
119 Self::Container(c) => c.pre_compute_size(options, sizes),
120 Self::Protected(p) => p.pre_compute_size(options, sizes),
121 }
122 }
123}
124
125impl PrintWithSize for definition::ContextTypeEntry {
126 fn fmt_with_size(&self, f: &mut fmt::Formatter, options: &Options, indent: usize, _sizes: &[Size], _index: &mut usize) -> fmt::Result {
127 match self {
128 Self::Container(c) => c.fmt_with(f, options, indent),
129 Self::Protected(p) => p.fmt_with(f, options, indent),
130 }
131 }
132}
133
134impl PrecomputeSize for definition::TypeContainer {
135 fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
136 Size::Width(jstrict::print::printed_string_size(self.into_str()))
137 }
138}
139
140impl Print for definition::TypeContainer {
141 fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
142 string_literal(self.into_str(), f)
143 }
144}
145
146impl PrecomputeSize for definition::Version {
147 fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
148 match self {
149 Self::V1_1 => Size::Width(3),
150 }
151 }
152}
153
154impl Print for definition::Version {
155 fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
156 match self {
157 Self::V1_1 => write!(f, "1.1"),
158 }
159 }
160}
161
162impl PrecomputeSize for definition::Vocab {
163 fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
164 Size::Width(jstrict::print::printed_string_size(self.as_str()))
165 }
166}
167
168impl Print for definition::Vocab {
169 fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
170 string_literal(self.as_str(), f)
171 }
172}
173
174impl PrecomputeSize for Nullable<&definition::Vocab> {
175 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
176 match self {
177 Self::Null => Size::Width(4),
178 Self::Some(v) => v.pre_compute_size(options, sizes),
179 }
180 }
181}
182
183impl Print for Nullable<&definition::Vocab> {
184 fn fmt_with(&self, f: &mut fmt::Formatter, options: &Options, indent: usize) -> fmt::Result {
185 match self {
186 Self::Null => write!(f, "null"),
187 Self::Some(v) => v.fmt_with(f, options, indent),
188 }
189 }
190}
191
192impl PrecomputeSize for Nullable<&TermDefinition> {
193 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
194 match self {
195 Self::Null => Size::Width(4),
196 Self::Some(c) => c.pre_compute_size(options, sizes),
197 }
198 }
199}
200
201impl PrecomputeSize for TermDefinition {
202 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
203 match self {
204 Self::Simple(s) => s.pre_compute_size(options, sizes),
205 Self::Expanded(d) => d.pre_compute_size(options, sizes),
206 }
207 }
208}
209
210impl PrecomputeSize for term_definition::Simple {
211 fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
212 Size::Width(jstrict::print::printed_string_size(self.as_str()))
213 }
214}
215
216impl PrintWithSize for TermDefinition {
217 fn fmt_with_size(&self, f: &mut fmt::Formatter, options: &Options, indent: usize, sizes: &[Size], index: &mut usize) -> fmt::Result {
218 match self {
219 Self::Simple(i) => i.fmt_with(f, options, indent),
220 Self::Expanded(d) => d.fmt_with_size(f, options, indent, sizes, index),
221 }
222 }
223}
224
225impl Print for term_definition::Simple {
226 fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
227 string_literal(self.as_str(), f)
228 }
229}
230
231impl PrintWithSize for Nullable<&TermDefinition> {
232 fn fmt_with_size(&self, f: &mut fmt::Formatter, options: &Options, indent: usize, sizes: &[Size], index: &mut usize) -> fmt::Result {
233 match self {
234 Self::Null => write!(f, "null"),
235 Self::Some(v) => v.fmt_with_size(f, options, indent, sizes, index),
236 }
237 }
238}
239
240impl PrecomputeSize for term_definition::Expanded {
241 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
242 jstrict::print::pre_compute_object_size(self.iter().map(|entry| (entry.key().as_str(), entry)), options, sizes)
243 }
244}
245
246impl PrintWithSize for term_definition::Expanded {
247 fn fmt_with_size(&self, f: &mut fmt::Formatter, options: &Options, indent: usize, sizes: &[Size], index: &mut usize) -> fmt::Result {
248 jstrict::print::print_object(self.iter().map(|entry| (entry.key().as_str(), entry)), f, options, indent, sizes, index)
249 }
250}
251
252impl PrecomputeSize for term_definition::EntryRef<'_> {
253 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
254 match self {
255 Self::Id(v) => v.pre_compute_size(options, sizes),
256 Self::Type(v) => v.pre_compute_size(options, sizes),
257 Self::Context(v) => v.pre_compute_size(options, sizes),
258 Self::Reverse(v) => v.pre_compute_size(options, sizes),
259 Self::Index(v) => v.pre_compute_size(options, sizes),
260 Self::Language(v) => v.pre_compute_size(options, sizes),
261 Self::Direction(v) => v.pre_compute_size(options, sizes),
262 Self::Container(v) => v.pre_compute_size(options, sizes),
263 Self::Nest(v) => v.pre_compute_size(options, sizes),
264 Self::Prefix(v) => v.pre_compute_size(options, sizes),
265 Self::Propagate(v) => v.pre_compute_size(options, sizes),
266 Self::Protected(v) => v.pre_compute_size(options, sizes),
267 }
268 }
269}
270
271impl PrintWithSize for term_definition::EntryRef<'_> {
272 fn fmt_with_size(&self, f: &mut fmt::Formatter, options: &Options, indent: usize, sizes: &[Size], index: &mut usize) -> fmt::Result {
273 match self {
274 Self::Id(v) => v.fmt_with(f, options, indent),
275 Self::Type(v) => v.fmt_with(f, options, indent),
276 Self::Context(v) => v.fmt_with_size(f, options, indent, sizes, index),
277 Self::Reverse(v) => v.fmt_with(f, options, indent),
278 Self::Index(v) => v.fmt_with(f, options, indent),
279 Self::Language(v) => v.fmt_with(f, options, indent),
280 Self::Direction(v) => v.fmt_with(f, options, indent),
281 Self::Container(v) => v.fmt_with_size(f, options, indent, sizes, index),
282 Self::Nest(v) => v.fmt_with(f, options, indent),
283 Self::Prefix(v) => v.fmt_with(f, options, indent),
284 Self::Propagate(v) => v.fmt_with(f, options, indent),
285 Self::Protected(v) => v.fmt_with(f, options, indent),
286 }
287 }
288}
289
290impl PrecomputeSize for term_definition::Id {
291 fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
292 Size::Width(jstrict::print::printed_string_size(self.as_str()))
293 }
294}
295
296impl Print for term_definition::Id {
297 fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
298 string_literal(self.as_str(), f)
299 }
300}
301
302impl PrecomputeSize for Nullable<&term_definition::Id> {
303 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
304 match self {
305 Self::Null => Size::Width(4),
306 Self::Some(v) => v.pre_compute_size(options, sizes),
307 }
308 }
309}
310
311impl Print for Nullable<&term_definition::Id> {
312 fn fmt_with(&self, f: &mut fmt::Formatter, options: &Options, indent: usize) -> fmt::Result {
313 match self {
314 Self::Null => write!(f, "null"),
315 Self::Some(v) => v.fmt_with(f, options, indent),
316 }
317 }
318}
319
320impl PrecomputeSize for term_definition::Type {
321 fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
322 Size::Width(jstrict::print::printed_string_size(self.as_str()))
323 }
324}
325
326impl Print for term_definition::Type {
327 fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
328 string_literal(self.as_str(), f)
329 }
330}
331
332impl PrecomputeSize for Nullable<&term_definition::Type> {
333 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
334 match self {
335 Self::Null => Size::Width(4),
336 Self::Some(v) => v.pre_compute_size(options, sizes),
337 }
338 }
339}
340
341impl Print for Nullable<&term_definition::Type> {
342 fn fmt_with(&self, f: &mut fmt::Formatter, options: &Options, indent: usize) -> fmt::Result {
343 match self {
344 Self::Null => write!(f, "null"),
345 Self::Some(v) => v.fmt_with(f, options, indent),
346 }
347 }
348}
349
350impl PrecomputeSize for definition::Key {
351 fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
352 Size::Width(jstrict::print::printed_string_size(self.as_str()))
353 }
354}
355
356impl Print for definition::Key {
357 fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
358 string_literal(self.as_str(), f)
359 }
360}
361
362impl PrecomputeSize for definition::EntryKeyRef<'_> {
363 fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
364 Size::Width(jstrict::print::printed_string_size(self.as_str()))
365 }
366}
367
368impl Print for definition::EntryKeyRef<'_> {
369 fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
370 string_literal(self.as_str(), f)
371 }
372}
373
374impl PrecomputeSize for term_definition::Index {
375 fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
376 Size::Width(jstrict::print::printed_string_size(self.as_str()))
377 }
378}
379
380impl Print for term_definition::Index {
381 fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
382 string_literal(self.as_str(), f)
383 }
384}
385
386impl PrecomputeSize for term_definition::Nest {
387 fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
388 Size::Width(jstrict::print::printed_string_size(self.as_str()))
389 }
390}
391
392impl Print for term_definition::Nest {
393 fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
394 string_literal(self.as_str(), f)
395 }
396}
397
398impl PrecomputeSize for Nullable<&Container> {
399 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
400 match self {
401 Self::Null => Size::Width(4),
402 Self::Some(v) => v.pre_compute_size(options, sizes),
403 }
404 }
405}
406
407impl PrintWithSize for Nullable<&Container> {
408 fn fmt_with_size(&self, f: &mut fmt::Formatter, options: &Options, indent: usize, sizes: &[Size], index: &mut usize) -> fmt::Result {
409 match self {
410 Self::Null => write!(f, "null"),
411 Self::Some(v) => v.fmt_with_size(f, options, indent, sizes, index),
412 }
413 }
414}
415
416impl PrecomputeSize for Container {
417 fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
418 match self {
419 Self::One(c) => c.pre_compute_size(options, sizes),
420 Self::Many(m) => jstrict::print::pre_compute_array_size(m, options, sizes),
421 }
422 }
423}
424
425impl PrintWithSize for Container {
426 fn fmt_with_size(&self, f: &mut fmt::Formatter, options: &Options, indent: usize, sizes: &[Size], index: &mut usize) -> fmt::Result {
427 match self {
428 Self::One(c) => c.fmt_with(f, options, indent),
429 Self::Many(m) => jstrict::print::print_array(m, f, options, indent, sizes, index),
430 }
431 }
432}
433
434impl PrecomputeSize for crate::ContainerKind {
435 fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
436 Size::Width(jstrict::print::printed_string_size(self.as_str()))
437 }
438}
439
440impl Print for crate::ContainerKind {
441 fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
442 string_literal(self.as_str(), f)
443 }
444}
445
446impl PrintWithSize for crate::ContainerKind {
447 fn fmt_with_size(&self, f: &mut fmt::Formatter, options: &Options, indent: usize, _sizes: &[Size], _index: &mut usize) -> fmt::Result {
448 self.fmt_with(f, options, indent)
449 }
450}