json_ld_syntax/
print_ld.rs

1use crate::{Direction, LenientLangTag, LenientLangTagBuf, Nullable};
2use iref::{IriRef, IriRefBuf};
3use json_syntax::print::{
4	printed_string_size, string_literal, Options, PrecomputeSize, Print, Size,
5};
6use std::fmt;
7
8impl PrecomputeSize for Direction {
9	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
10		Size::Width(printed_string_size(self.as_str()))
11	}
12}
13
14impl Print for Direction {
15	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
16		string_literal(self.as_str(), f)
17	}
18}
19
20impl PrecomputeSize for LenientLangTagBuf {
21	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
22		Size::Width(printed_string_size(self.as_str()))
23	}
24}
25
26impl Print for LenientLangTagBuf {
27	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
28		string_literal(self.as_str(), f)
29	}
30}
31
32impl PrecomputeSize for LenientLangTag {
33	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
34		Size::Width(printed_string_size(self.as_str()))
35	}
36}
37
38impl Print for LenientLangTag {
39	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
40		string_literal(self.as_str(), f)
41	}
42}
43
44impl<'a> PrecomputeSize for Nullable<&'a IriRef> {
45	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
46		match self {
47			Self::Null => Size::Width(4),
48			Self::Some(v) => Size::Width(json_syntax::print::printed_string_size(v.as_str())),
49		}
50	}
51}
52
53impl<'a> Print for Nullable<&'a IriRef> {
54	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
55		match self {
56			Self::Null => write!(f, "null"),
57			Self::Some(v) => string_literal(v.as_str(), f),
58		}
59	}
60}
61
62impl PrecomputeSize for Nullable<IriRefBuf> {
63	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
64		match self {
65			Self::Null => Size::Width(4),
66			Self::Some(v) => Size::Width(printed_string_size(v.as_str())),
67		}
68	}
69}
70
71impl Print for Nullable<IriRefBuf> {
72	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
73		match self {
74			Self::Null => write!(f, "null"),
75			Self::Some(v) => string_literal(v.as_str(), f),
76		}
77	}
78}
79
80impl PrecomputeSize for Nullable<bool> {
81	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
82		match self {
83			Self::Null => Size::Width(4),
84			Self::Some(b) => b.pre_compute_size(options, sizes),
85		}
86	}
87}
88
89impl Print for Nullable<bool> {
90	fn fmt_with(&self, f: &mut fmt::Formatter, options: &Options, indent: usize) -> fmt::Result {
91		match self {
92			Self::Null => write!(f, "null"),
93			Self::Some(b) => b.fmt_with(f, options, indent),
94		}
95	}
96}
97
98impl<'a> PrecomputeSize for Nullable<&'a LenientLangTagBuf> {
99	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
100		match self {
101			Self::Null => Size::Width(4),
102			Self::Some(v) => Size::Width(printed_string_size(v.as_str())),
103		}
104	}
105}
106
107impl<'a> Print for Nullable<&'a LenientLangTagBuf> {
108	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
109		match self {
110			Self::Null => write!(f, "null"),
111			Self::Some(t) => string_literal(t.as_str(), f),
112		}
113	}
114}
115
116impl PrecomputeSize for Nullable<crate::Direction> {
117	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
118		match self {
119			Self::Null => Size::Width(4),
120			Self::Some(v) => Size::Width(printed_string_size(v.as_str())),
121		}
122	}
123}
124
125impl Print for Nullable<crate::Direction> {
126	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
127		match self {
128			Self::Null => write!(f, "null"),
129			Self::Some(d) => string_literal(d.as_str(), f),
130		}
131	}
132}
133
134// impl<M> Print
135// 	for crate::Value<M>
136// {
137// 	fn fmt_with(&self, f: &mut fmt::Formatter, options: &Options, indent: usize) -> fmt::Result {
138// 		match self {
139// 			Self::Null => f.write_str("null"),
140// 			Self::Boolean(b) => b.fmt_with(f, options, indent),
141// 			Self::Number(n) => n.fmt_with(f, options, indent),
142// 			Self::String(s) => s.fmt_with(f, options, indent),
143// 			Self::Array(a) => {
144// 				let mut sizes = Vec::with_capacity(self.count(|v| v.is_array() || v.is_object()));
145// 				self.pre_compute_size(options, &mut sizes);
146// 				let mut index = 0;
147// 				a.fmt_with_size(f, options, indent, &sizes, &mut index)
148// 			}
149// 			Self::Object(o) => {
150// 				let mut sizes = Vec::with_capacity(self.count(|v| v.is_array() || v.is_object()));
151// 				self.pre_compute_size(options, &mut sizes);
152// 				let mut index = 0;
153// 				o.fmt_with_size(f, options, indent, &sizes, &mut index)
154// 			}
155// 		}
156// 	}
157// }
158
159// impl<M> PrecomputeSize for crate::Value<M> {
160// 	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
161// 		match self {
162// 			crate::Value::Null => Size::Width(4),
163// 			crate::Value::Boolean(true) => Size::Width(4),
164// 			crate::Value::Boolean(false) => Size::Width(5),
165// 			crate::Value::Number(n) => Size::Width(n.as_str().len()),
166// 			crate::Value::String(s) => Size::Width(printed_string_size(s)),
167// 			crate::Value::Array(a) => pre_compute_array_size(a, options, sizes),
168// 			crate::Value::Object(o) => pre_compute_object_size(
169// 				o.entries().iter()
170// 					.map(|e| (e.key.as_str(), &e.value)),
171// 				options,
172// 				sizes,
173// 			),
174// 		}
175// 	}
176// }
177
178// impl<M> PrintWithSize for crate::Value<M> {
179// 	#[inline(always)]
180// 	fn fmt_with_size(
181// 		&self,
182// 		f: &mut fmt::Formatter,
183// 		options: &Options,
184// 		indent: usize,
185// 		sizes: &[Size],
186// 		index: &mut usize,
187// 	) -> fmt::Result {
188// 		match self {
189// 			Self::Null => f.write_str("null"),
190// 			Self::Boolean(b) => b.fmt_with(f, options, indent),
191// 			Self::Number(n) => n.fmt_with(f, options, indent),
192// 			Self::String(s) => s.fmt_with(f, options, indent),
193// 			Self::Array(a) => a.fmt_with_size(f, options, indent, sizes, index),
194// 			Self::Object(o) => o.fmt_with_size(f, options, indent, sizes, index),
195// 		}
196// 	}
197// }
198
199// impl<M> PrintWithSize for crate::Object<M> {
200// 	#[inline(always)]
201// 	fn fmt_with_size(
202// 		&self,
203// 		f: &mut fmt::Formatter,
204// 		options: &Options,
205// 		indent: usize,
206// 		sizes: &[Size],
207// 		index: &mut usize,
208// 	) -> fmt::Result {
209// 		json_syntax::print::print_object(
210// 			self.entries().iter()
211// 				.map(|e| (e.key.as_str(), &e.value)),
212// 			f,
213// 			options,
214// 			indent,
215// 			sizes,
216// 			index,
217// 		)
218// 	}
219// }