json_ld_syntax/
print_ld.rs1use 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