Skip to main content

jsonld_syntax/
print_ld.rs

1use crate::{Direction, LenientLangTag, LenientLangTagBuf, Nullable};
2use iri_rs::IriRefBuf;
3use jstrict::print::{Options, PrecomputeSize, Print, Size, printed_string_size, string_literal};
4use std::fmt;
5
6impl PrecomputeSize for Direction {
7    fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
8        Size::Width(printed_string_size(self.as_str()))
9    }
10}
11
12impl Print for Direction {
13    fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
14        string_literal(self.as_str(), f)
15    }
16}
17
18impl PrecomputeSize for LenientLangTagBuf {
19    fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
20        Size::Width(printed_string_size(self.as_str()))
21    }
22}
23
24impl Print for LenientLangTagBuf {
25    fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
26        string_literal(self.as_str(), f)
27    }
28}
29
30impl PrecomputeSize for LenientLangTag {
31    fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
32        Size::Width(printed_string_size(self.as_str()))
33    }
34}
35
36impl Print for LenientLangTag {
37    fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
38        string_literal(self.as_str(), f)
39    }
40}
41
42impl PrecomputeSize for Nullable<&IriRefBuf> {
43    fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
44        match self {
45            Self::Null => Size::Width(4),
46            Self::Some(v) => Size::Width(jstrict::print::printed_string_size(v.as_str())),
47        }
48    }
49}
50
51impl Print for Nullable<&IriRefBuf> {
52    fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
53        match self {
54            Self::Null => write!(f, "null"),
55            Self::Some(v) => string_literal(v.as_str(), f),
56        }
57    }
58}
59
60impl PrecomputeSize for Nullable<IriRefBuf> {
61    fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
62        match self {
63            Self::Null => Size::Width(4),
64            Self::Some(v) => Size::Width(printed_string_size(v.as_str())),
65        }
66    }
67}
68
69impl Print for Nullable<IriRefBuf> {
70    fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
71        match self {
72            Self::Null => write!(f, "null"),
73            Self::Some(v) => string_literal(v.as_str(), f),
74        }
75    }
76}
77
78impl PrecomputeSize for Nullable<bool> {
79    fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
80        match self {
81            Self::Null => Size::Width(4),
82            Self::Some(b) => b.pre_compute_size(options, sizes),
83        }
84    }
85}
86
87impl Print for Nullable<bool> {
88    fn fmt_with(&self, f: &mut fmt::Formatter, options: &Options, indent: usize) -> fmt::Result {
89        match self {
90            Self::Null => write!(f, "null"),
91            Self::Some(b) => b.fmt_with(f, options, indent),
92        }
93    }
94}
95
96impl PrecomputeSize for Nullable<&LenientLangTagBuf> {
97    fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
98        match self {
99            Self::Null => Size::Width(4),
100            Self::Some(v) => Size::Width(printed_string_size(v.as_str())),
101        }
102    }
103}
104
105impl Print for Nullable<&LenientLangTagBuf> {
106    fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
107        match self {
108            Self::Null => write!(f, "null"),
109            Self::Some(t) => string_literal(t.as_str(), f),
110        }
111    }
112}
113
114impl PrecomputeSize for Nullable<crate::Direction> {
115    fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
116        match self {
117            Self::Null => Size::Width(4),
118            Self::Some(v) => Size::Width(printed_string_size(v.as_str())),
119        }
120    }
121}
122
123impl Print for Nullable<crate::Direction> {
124    fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
125        match self {
126            Self::Null => write!(f, "null"),
127            Self::Some(d) => string_literal(d.as_str(), f),
128        }
129    }
130}