json_ld_syntax/context/
print.rs

1use super::{definition, term_definition, TermDefinition};
2use crate::{Container, ContextEntry, Nullable};
3use json_syntax::print::{string_literal, Options, PrecomputeSize, Print, PrintWithSize, Size};
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(
9			self.traverse()
10				.filter(|f| f.is_array() || f.is_object())
11				.count(),
12		);
13		self.pre_compute_size(options, &mut sizes);
14		let mut index = 0;
15		self.fmt_with_size(f, options, indent, &sizes, &mut index)
16	}
17}
18
19impl PrecomputeSize for super::Context {
20	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
21		match self {
22			Self::One(context) => context.pre_compute_size(options, sizes),
23			Self::Many(contexts) => {
24				json_syntax::print::pre_compute_array_size(contexts, options, sizes)
25			}
26		}
27	}
28}
29
30impl PrintWithSize for super::Context {
31	fn fmt_with_size(
32		&self,
33		f: &mut fmt::Formatter,
34		options: &Options,
35		indent: usize,
36		sizes: &[Size],
37		index: &mut usize,
38	) -> fmt::Result {
39		match self {
40			Self::One(context) => context.fmt_with_size(f, options, indent, sizes, index),
41			Self::Many(contexts) => {
42				json_syntax::print::print_array(contexts, f, options, indent, sizes, index)
43			}
44		}
45	}
46}
47
48// struct ForMeta<T, M>(T, PhantomData);
49
50// trait IntoForMeta: Sized {
51// 	fn for_meta(self) -> ForMeta<Self, M> {
52// 		ForMeta(self, PhantomData)
53// 	}
54// }
55
56// impl<T> IntoForMeta for T {}
57
58impl PrecomputeSize for ContextEntry {
59	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
60		match self {
61			ContextEntry::Null => Size::Width(4),
62			ContextEntry::IriRef(r) => {
63				Size::Width(json_syntax::print::printed_string_size(r.as_str()))
64			}
65			ContextEntry::Definition(d) => json_syntax::print::pre_compute_object_size(
66				d.iter().map(|entry| {
67					let (key, value) = entry.into_key_value();
68					(key.as_str(), value)
69				}),
70				options,
71				sizes,
72			),
73		}
74	}
75}
76
77impl PrintWithSize for ContextEntry {
78	fn fmt_with_size(
79		&self,
80		f: &mut fmt::Formatter,
81		options: &Options,
82		indent: usize,
83		sizes: &[Size],
84		index: &mut usize,
85	) -> fmt::Result {
86		match self {
87			ContextEntry::Null => write!(f, "null"),
88			ContextEntry::IriRef(r) => string_literal(r.as_str(), f),
89			ContextEntry::Definition(d) => json_syntax::print::print_object(
90				d.iter().map(|entry| {
91					let (key, value) = entry.into_key_value();
92					(key.as_str(), value)
93				}),
94				f,
95				options,
96				indent,
97				sizes,
98				index,
99			),
100		}
101	}
102}
103
104impl<'a> PrecomputeSize for definition::EntryValueRef<'a> {
105	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
106		match self {
107			Self::Base(v) => v.pre_compute_size(options, sizes),
108			Self::Import(v) => Size::Width(json_syntax::print::printed_string_size(v.as_str())),
109			Self::Language(v) => v.pre_compute_size(options, sizes),
110			Self::Direction(v) => v.pre_compute_size(options, sizes),
111			Self::Propagate(v) => v.pre_compute_size(options, sizes),
112			Self::Protected(v) => v.pre_compute_size(options, sizes),
113			Self::Type(v) => v.pre_compute_size(options, sizes),
114			Self::Version(v) => v.pre_compute_size(options, sizes),
115			Self::Vocab(v) => v.pre_compute_size(options, sizes),
116			Self::Definition(v) => v.pre_compute_size(options, sizes),
117		}
118	}
119}
120
121impl<'a> PrintWithSize for definition::EntryValueRef<'a> {
122	fn fmt_with_size(
123		&self,
124		f: &mut fmt::Formatter,
125		options: &Options,
126		indent: usize,
127		sizes: &[Size],
128		index: &mut usize,
129	) -> fmt::Result {
130		match self {
131			Self::Base(v) => v.fmt_with(f, options, indent),
132			Self::Import(v) => string_literal(v.as_str(), f),
133			Self::Language(v) => v.fmt_with(f, options, indent),
134			Self::Direction(v) => v.fmt_with(f, options, indent),
135			Self::Propagate(v) => v.fmt_with(f, options, indent),
136			Self::Protected(v) => v.fmt_with(f, options, indent),
137			Self::Type(v) => v.fmt_with_size(f, options, indent, sizes, index),
138			Self::Version(v) => v.fmt_with(f, options, indent),
139			Self::Vocab(v) => v.fmt_with(f, options, indent),
140			Self::Definition(v) => v.fmt_with_size(f, options, indent, sizes, index),
141		}
142	}
143}
144
145impl PrecomputeSize for definition::Type {
146	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
147		json_syntax::print::pre_compute_object_size(
148			self.iter().map(|entry| (entry.key().as_str(), entry)),
149			options,
150			sizes,
151		)
152	}
153}
154
155impl PrintWithSize for definition::Type {
156	fn fmt_with_size(
157		&self,
158		f: &mut fmt::Formatter,
159		options: &Options,
160		indent: usize,
161		sizes: &[Size],
162		index: &mut usize,
163	) -> fmt::Result {
164		json_syntax::print::print_object(
165			self.iter().map(|entry| (entry.key().as_str(), entry)),
166			f,
167			options,
168			indent,
169			sizes,
170			index,
171		)
172	}
173}
174
175impl PrecomputeSize for definition::ContextTypeEntry {
176	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
177		match self {
178			Self::Container(c) => c.pre_compute_size(options, sizes),
179			Self::Protected(p) => p.pre_compute_size(options, sizes),
180		}
181	}
182}
183
184impl PrintWithSize for definition::ContextTypeEntry {
185	fn fmt_with_size(
186		&self,
187		f: &mut fmt::Formatter,
188		options: &Options,
189		indent: usize,
190		_sizes: &[Size],
191		_index: &mut usize,
192	) -> fmt::Result {
193		match self {
194			Self::Container(c) => c.fmt_with(f, options, indent),
195			Self::Protected(p) => p.fmt_with(f, options, indent),
196		}
197	}
198}
199
200impl PrecomputeSize for definition::TypeContainer {
201	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
202		Size::Width(json_syntax::print::printed_string_size(self.into_str()))
203	}
204}
205
206impl Print for definition::TypeContainer {
207	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
208		string_literal(self.into_str(), f)
209	}
210}
211
212impl PrecomputeSize for definition::Version {
213	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
214		match self {
215			Self::V1_1 => Size::Width(3),
216		}
217	}
218}
219
220impl Print for definition::Version {
221	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
222		match self {
223			Self::V1_1 => write!(f, "1.1"),
224		}
225	}
226}
227
228impl PrecomputeSize for definition::Vocab {
229	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
230		Size::Width(json_syntax::print::printed_string_size(self.as_str()))
231	}
232}
233
234impl Print for definition::Vocab {
235	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
236		string_literal(self.as_str(), f)
237	}
238}
239
240impl<'a> PrecomputeSize for Nullable<&'a definition::Vocab> {
241	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
242		match self {
243			Self::Null => Size::Width(4),
244			Self::Some(v) => v.pre_compute_size(options, sizes),
245		}
246	}
247}
248
249impl<'a> Print for Nullable<&'a definition::Vocab> {
250	fn fmt_with(&self, f: &mut fmt::Formatter, options: &Options, indent: usize) -> fmt::Result {
251		match self {
252			Self::Null => write!(f, "null"),
253			Self::Some(v) => v.fmt_with(f, options, indent),
254		}
255	}
256}
257
258impl<'a> PrecomputeSize for Nullable<&'a TermDefinition> {
259	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
260		match self {
261			Self::Null => Size::Width(4),
262			Self::Some(c) => c.pre_compute_size(options, sizes),
263		}
264	}
265}
266
267impl PrecomputeSize for TermDefinition {
268	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
269		match self {
270			Self::Simple(s) => s.pre_compute_size(options, sizes),
271			Self::Expanded(d) => d.pre_compute_size(options, sizes),
272		}
273	}
274}
275
276impl PrecomputeSize for term_definition::Simple {
277	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
278		Size::Width(json_syntax::print::printed_string_size(self.as_str()))
279	}
280}
281
282impl PrintWithSize for TermDefinition {
283	fn fmt_with_size(
284		&self,
285		f: &mut fmt::Formatter,
286		options: &Options,
287		indent: usize,
288		sizes: &[Size],
289		index: &mut usize,
290	) -> fmt::Result {
291		match self {
292			Self::Simple(i) => i.fmt_with(f, options, indent),
293			Self::Expanded(d) => d.fmt_with_size(f, options, indent, sizes, index),
294		}
295	}
296}
297
298impl Print for term_definition::Simple {
299	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
300		string_literal(self.as_str(), f)
301	}
302}
303
304impl<'a> PrintWithSize for Nullable<&'a TermDefinition> {
305	fn fmt_with_size(
306		&self,
307		f: &mut fmt::Formatter,
308		options: &Options,
309		indent: usize,
310		sizes: &[Size],
311		index: &mut usize,
312	) -> fmt::Result {
313		match self {
314			Self::Null => write!(f, "null"),
315			Self::Some(v) => v.fmt_with_size(f, options, indent, sizes, index),
316		}
317	}
318}
319
320impl PrecomputeSize for term_definition::Expanded {
321	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
322		json_syntax::print::pre_compute_object_size(
323			self.iter().map(|entry| (entry.key().as_str(), entry)),
324			options,
325			sizes,
326		)
327	}
328}
329
330impl PrintWithSize for term_definition::Expanded {
331	fn fmt_with_size(
332		&self,
333		f: &mut fmt::Formatter,
334		options: &Options,
335		indent: usize,
336		sizes: &[Size],
337		index: &mut usize,
338	) -> fmt::Result {
339		json_syntax::print::print_object(
340			self.iter().map(|entry| (entry.key().as_str(), entry)),
341			f,
342			options,
343			indent,
344			sizes,
345			index,
346		)
347	}
348}
349
350impl<'a> PrecomputeSize for term_definition::EntryRef<'a> {
351	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
352		match self {
353			Self::Id(v) => v.pre_compute_size(options, sizes),
354			Self::Type(v) => v.pre_compute_size(options, sizes),
355			Self::Context(v) => v.pre_compute_size(options, sizes),
356			Self::Reverse(v) => v.pre_compute_size(options, sizes),
357			Self::Index(v) => v.pre_compute_size(options, sizes),
358			Self::Language(v) => v.pre_compute_size(options, sizes),
359			Self::Direction(v) => v.pre_compute_size(options, sizes),
360			Self::Container(v) => v.pre_compute_size(options, sizes),
361			Self::Nest(v) => v.pre_compute_size(options, sizes),
362			Self::Prefix(v) => v.pre_compute_size(options, sizes),
363			Self::Propagate(v) => v.pre_compute_size(options, sizes),
364			Self::Protected(v) => v.pre_compute_size(options, sizes),
365		}
366	}
367}
368
369impl<'a> PrintWithSize for term_definition::EntryRef<'a> {
370	fn fmt_with_size(
371		&self,
372		f: &mut fmt::Formatter,
373		options: &Options,
374		indent: usize,
375		sizes: &[Size],
376		index: &mut usize,
377	) -> fmt::Result {
378		match self {
379			Self::Id(v) => v.fmt_with(f, options, indent),
380			Self::Type(v) => v.fmt_with(f, options, indent),
381			Self::Context(v) => v.fmt_with_size(f, options, indent, sizes, index),
382			Self::Reverse(v) => v.fmt_with(f, options, indent),
383			Self::Index(v) => v.fmt_with(f, options, indent),
384			Self::Language(v) => v.fmt_with(f, options, indent),
385			Self::Direction(v) => v.fmt_with(f, options, indent),
386			Self::Container(v) => v.fmt_with_size(f, options, indent, sizes, index),
387			Self::Nest(v) => v.fmt_with(f, options, indent),
388			Self::Prefix(v) => v.fmt_with(f, options, indent),
389			Self::Propagate(v) => v.fmt_with(f, options, indent),
390			Self::Protected(v) => v.fmt_with(f, options, indent),
391		}
392	}
393}
394
395impl PrecomputeSize for term_definition::Id {
396	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
397		Size::Width(json_syntax::print::printed_string_size(self.as_str()))
398	}
399}
400
401impl Print for term_definition::Id {
402	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
403		string_literal(self.as_str(), f)
404	}
405}
406
407impl<'a> PrecomputeSize for Nullable<&'a term_definition::Id> {
408	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
409		match self {
410			Self::Null => Size::Width(4),
411			Self::Some(v) => v.pre_compute_size(options, sizes),
412		}
413	}
414}
415
416impl<'a> Print for Nullable<&'a term_definition::Id> {
417	fn fmt_with(&self, f: &mut fmt::Formatter, options: &Options, indent: usize) -> fmt::Result {
418		match self {
419			Self::Null => write!(f, "null"),
420			Self::Some(v) => v.fmt_with(f, options, indent),
421		}
422	}
423}
424
425impl PrecomputeSize for term_definition::Type {
426	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
427		Size::Width(json_syntax::print::printed_string_size(self.as_str()))
428	}
429}
430
431impl Print for term_definition::Type {
432	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
433		string_literal(self.as_str(), f)
434	}
435}
436
437impl<'a> PrecomputeSize for Nullable<&'a term_definition::Type> {
438	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
439		match self {
440			Self::Null => Size::Width(4),
441			Self::Some(v) => v.pre_compute_size(options, sizes),
442		}
443	}
444}
445
446impl<'a> Print for Nullable<&'a term_definition::Type> {
447	fn fmt_with(&self, f: &mut fmt::Formatter, options: &Options, indent: usize) -> fmt::Result {
448		match self {
449			Self::Null => write!(f, "null"),
450			Self::Some(v) => v.fmt_with(f, options, indent),
451		}
452	}
453}
454
455impl PrecomputeSize for definition::Key {
456	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
457		Size::Width(json_syntax::print::printed_string_size(self.as_str()))
458	}
459}
460
461impl Print for definition::Key {
462	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
463		string_literal(self.as_str(), f)
464	}
465}
466
467impl<'a> PrecomputeSize for definition::EntryKeyRef<'a> {
468	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
469		Size::Width(json_syntax::print::printed_string_size(self.as_str()))
470	}
471}
472
473impl<'a> Print for definition::EntryKeyRef<'a> {
474	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
475		string_literal(self.as_str(), f)
476	}
477}
478
479impl PrecomputeSize for term_definition::Index {
480	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
481		Size::Width(json_syntax::print::printed_string_size(self.as_str()))
482	}
483}
484
485impl Print for term_definition::Index {
486	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
487		string_literal(self.as_str(), f)
488	}
489}
490
491impl PrecomputeSize for term_definition::Nest {
492	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
493		Size::Width(json_syntax::print::printed_string_size(self.as_str()))
494	}
495}
496
497impl Print for term_definition::Nest {
498	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
499		string_literal(self.as_str(), f)
500	}
501}
502
503impl<'a> PrecomputeSize for Nullable<&'a Container> {
504	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
505		match self {
506			Self::Null => Size::Width(4),
507			Self::Some(v) => v.pre_compute_size(options, sizes),
508		}
509	}
510}
511
512impl<'a> PrintWithSize for Nullable<&'a Container> {
513	fn fmt_with_size(
514		&self,
515		f: &mut fmt::Formatter,
516		options: &Options,
517		indent: usize,
518		sizes: &[Size],
519		index: &mut usize,
520	) -> fmt::Result {
521		match self {
522			Self::Null => write!(f, "null"),
523			Self::Some(v) => v.fmt_with_size(f, options, indent, sizes, index),
524		}
525	}
526}
527
528impl PrecomputeSize for Container {
529	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
530		match self {
531			Self::One(c) => c.pre_compute_size(options, sizes),
532			Self::Many(m) => json_syntax::print::pre_compute_array_size(m, options, sizes),
533		}
534	}
535}
536
537impl PrintWithSize for Container {
538	fn fmt_with_size(
539		&self,
540		f: &mut fmt::Formatter,
541		options: &Options,
542		indent: usize,
543		sizes: &[Size],
544		index: &mut usize,
545	) -> fmt::Result {
546		match self {
547			Self::One(c) => c.fmt_with(f, options, indent),
548			Self::Many(m) => json_syntax::print::print_array(m, f, options, indent, sizes, index),
549		}
550	}
551}
552
553impl PrecomputeSize for crate::ContainerKind {
554	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
555		Size::Width(json_syntax::print::printed_string_size(self.as_str()))
556	}
557}
558
559impl Print for crate::ContainerKind {
560	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
561		string_literal(self.as_str(), f)
562	}
563}
564
565impl PrintWithSize for crate::ContainerKind {
566	fn fmt_with_size(
567		&self,
568		f: &mut fmt::Formatter,
569		options: &Options,
570		indent: usize,
571		_sizes: &[Size],
572		_index: &mut usize,
573	) -> fmt::Result {
574		self.fmt_with(f, options, indent)
575	}
576}