1use crate::syn::*;
2
3#[derive(Default)]
4pub struct PrintlnWriter(usize);
5
6impl PrintlnWriter {
7 fn indented_println<T: std::fmt::Display>(&self, text: T) {
8 println!("{}{}", " ".repeat(self.0), text);
9 }
10
11 fn with_increased_indentation<R, F: Fn(&mut Self) -> R>(&mut self, f: F) -> R {
12 self.0 += 1;
13 let r = f(self);
14 self.0 -= 1;
15 r
16 }
17}
18
19impl Writer for PrintlnWriter {
20 type Error = core::convert::Infallible;
21
22 fn write_sequence<C: sequence::Constraint, F: Fn(&mut Self) -> Result<(), Self::Error>>(
23 &mut self,
24 f: F,
25 ) -> Result<(), Self::Error> {
26 self.indented_println(format!("Writing sequence {}, tag={:?}", C::NAME, C::TAG));
27 self.with_increased_indentation(|w| f(w))
28 }
29
30 fn write_sequence_of<C: sequenceof::Constraint, T: WritableType>(
31 &mut self,
32 slice: &[T::Type],
33 ) -> Result<(), Self::Error> {
34 self.indented_println(format!(
35 "Writing sequence-of ({}..{}), tag={:?}",
36 C::MIN
37 .map(|v| format!("{}", v))
38 .unwrap_or_else(|| String::from("MIN")),
39 C::MAX
40 .map(|v| format!("{}", v))
41 .unwrap_or_else(|| String::from("MAX")),
42 C::TAG,
43 ));
44 self.with_increased_indentation(|w| {
45 for value in slice {
46 T::write_value(w, value)?;
47 }
48 Ok(())
49 })
50 }
51
52 fn write_set<C: set::Constraint, F: Fn(&mut Self) -> Result<(), Self::Error>>(
53 &mut self,
54 f: F,
55 ) -> Result<(), Self::Error> {
56 self.indented_println(format!("Writing set {}", C::NAME));
57 self.with_increased_indentation(|w| f(w))
58 }
59
60 fn write_set_of<C: setof::Constraint, T: WritableType>(
61 &mut self,
62 slice: &[<T as WritableType>::Type],
63 ) -> Result<(), Self::Error> {
64 self.indented_println(format!(
65 "Writing set-of ({}..{})",
66 C::MIN
67 .map(|v| format!("{}", v))
68 .unwrap_or_else(|| String::from("MIN")),
69 C::MAX
70 .map(|v| format!("{}", v))
71 .unwrap_or_else(|| String::from("MAX")),
72 ));
73 self.with_increased_indentation(|w| {
74 for value in slice {
75 T::write_value(w, value)?;
76 }
77 Ok(())
78 })
79 }
80
81 fn write_enumerated<C: enumerated::Constraint>(
82 &mut self,
83 enumerated: &C,
84 ) -> Result<(), Self::Error> {
85 self.indented_println(format!("Write enumerated {}, tag={:?}", C::NAME, C::TAG));
86 self.with_increased_indentation(|w| {
87 if C::EXTENSIBLE {
88 w.indented_println("extensible");
89 } else {
90 w.indented_println("normal");
91 }
92 w.with_increased_indentation(|w| {
93 w.indented_println(format!(
94 "choice_index {}/{}/{}",
95 enumerated.to_choice_index(),
96 C::STD_VARIANT_COUNT,
97 C::VARIANT_COUNT
98 ));
99 Ok(())
100 })
101 })
102 }
103
104 fn write_choice<C: choice::Constraint>(&mut self, choice: &C) -> Result<(), Self::Error> {
105 self.indented_println(format!("Write choice {}, tag={:?}", C::NAME, C::TAG));
106 self.with_increased_indentation(|w| {
107 if C::EXTENSIBLE {
108 w.indented_println("extensible");
109 } else {
110 w.indented_println("normal");
111 }
112 w.with_increased_indentation(|w| {
113 w.indented_println(format!(
114 "choice_index {}/{}/{}",
115 choice.to_choice_index(),
116 C::STD_VARIANT_COUNT,
117 C::VARIANT_COUNT
118 ));
119 choice.write_content(w)
120 })
121 })
122 }
123
124 fn write_opt<T: WritableType>(&mut self, value: Option<&T::Type>) -> Result<(), Self::Error> {
125 self.indented_println("Writing OPTIONAL");
126 self.with_increased_indentation(|w| {
127 if let Some(value) = value {
128 w.indented_println("Some");
129 w.with_increased_indentation(|w| T::write_value(w, value))
130 } else {
131 w.indented_println("None");
132 Ok(())
133 }
134 })
135 }
136
137 fn write_default<C: default::Constraint<Owned = T::Type>, T: WritableType>(
138 &mut self,
139 value: &T::Type,
140 ) -> Result<(), Self::Error> {
141 self.indented_println(format!("Writing DEFAULT (default: {:?})", C::DEFAULT_VALUE));
142 self.with_increased_indentation(|w| {
143 if C::DEFAULT_VALUE.eq(value) {
144 w.indented_println("None");
145 Ok(())
146 } else {
147 w.indented_println("Some");
148 w.with_increased_indentation(|w| T::write_value(w, value))
149 }
150 })
151 }
152
153 fn write_number<T: numbers::Number, C: numbers::Constraint<T>>(
154 &mut self,
155 value: T,
156 ) -> Result<(), Self::Error> {
157 self.indented_println(format!(
158 "WRITING Integer({}..{}{}), tag={:?}",
159 C::MIN
160 .map(|v| v.to_string())
161 .unwrap_or_else(|| "MIN".to_string()),
162 C::MAX
163 .map(|v| v.to_string())
164 .unwrap_or_else(|| "MAX".to_string()),
165 if C::EXTENSIBLE { ",..." } else { "" },
166 C::TAG,
167 ));
168 self.with_increased_indentation(|w| w.indented_println(value.to_i64().to_string()));
169 Ok(())
170 }
171
172 fn write_utf8string<C: utf8string::Constraint>(
173 &mut self,
174 value: &str,
175 ) -> Result<(), Self::Error> {
176 self.indented_println(format!(
177 "Writing Utf8String({}..{}), tag={:?}",
178 C::MIN
179 .map(|v| format!("{}", v))
180 .unwrap_or_else(|| String::from("MIN")),
181 C::MAX
182 .map(|v| format!("{}", v))
183 .unwrap_or_else(|| String::from("MAX")),
184 C::TAG
185 ));
186 self.with_increased_indentation(|w| w.indented_println(format!("{:?}", value)));
187 Ok(())
188 }
189
190 fn write_ia5string<C: ia5string::Constraint>(
191 &mut self,
192 value: &str,
193 ) -> Result<(), Self::Error> {
194 self.indented_println(format!(
195 "Writing Ia5String({}..{}), tag={:?}",
196 C::MIN
197 .map(|v| format!("{}", v))
198 .unwrap_or_else(|| String::from("MIN")),
199 C::MAX
200 .map(|v| format!("{}", v))
201 .unwrap_or_else(|| String::from("MAX")),
202 C::TAG
203 ));
204 self.with_increased_indentation(|w| w.indented_println(format!("{:?}", value)));
205 Ok(())
206 }
207
208 fn write_numeric_string<C: numericstring::Constraint>(
209 &mut self,
210 value: &str,
211 ) -> Result<(), Self::Error> {
212 self.indented_println(format!(
213 "Writing NumericString({}..{}), tag={:?}",
214 C::MIN
215 .map(|v| format!("{}", v))
216 .unwrap_or_else(|| String::from("MIN")),
217 C::MAX
218 .map(|v| format!("{}", v))
219 .unwrap_or_else(|| String::from("MAX")),
220 C::TAG
221 ));
222 self.with_increased_indentation(|w| w.indented_println(format!("{:?}", value)));
223 Ok(())
224 }
225
226 fn write_printable_string<C: printablestring::Constraint>(
227 &mut self,
228 value: &str,
229 ) -> Result<(), Self::Error> {
230 self.indented_println(format!(
231 "Writing PrintableString({}..{}), tag={:?}",
232 C::MIN
233 .map(|v| format!("{}", v))
234 .unwrap_or_else(|| String::from("MIN")),
235 C::MAX
236 .map(|v| format!("{}", v))
237 .unwrap_or_else(|| String::from("MAX")),
238 C::TAG
239 ));
240 self.with_increased_indentation(|w| w.indented_println(format!("{:?}", value)));
241 Ok(())
242 }
243
244 fn write_visible_string<C: visiblestring::Constraint>(
245 &mut self,
246 value: &str,
247 ) -> Result<(), Self::Error> {
248 self.indented_println(format!(
249 "Writing VisibleString({}..{}), tag={:?}",
250 C::MIN
251 .map(|v| format!("{}", v))
252 .unwrap_or_else(|| String::from("MIN")),
253 C::MAX
254 .map(|v| format!("{}", v))
255 .unwrap_or_else(|| String::from("MAX")),
256 C::TAG
257 ));
258 self.with_increased_indentation(|w| w.indented_println(format!("{:?}", value)));
259 Ok(())
260 }
261
262 fn write_octet_string<C: octetstring::Constraint>(
263 &mut self,
264 value: &[u8],
265 ) -> Result<(), Self::Error> {
266 self.indented_println(format!(
267 "WRITING OctetString({}..{}), tag={:?}",
268 C::MIN
269 .map(|v| format!("{}", v))
270 .unwrap_or_else(|| String::from("MIN")),
271 C::MAX
272 .map(|v| format!("{}", v))
273 .unwrap_or_else(|| String::from("MAX")),
274 C::TAG,
275 ));
276 self.with_increased_indentation(|w| w.indented_println(format!("{:02x?}", value)));
277 Ok(())
278 }
279
280 fn write_bit_string<C: bitstring::Constraint>(
281 &mut self,
282 value: &[u8],
283 bit_len: u64,
284 ) -> Result<(), Self::Error> {
285 self.indented_println(format!(
286 "WRITING BitString({}..{}), tag={:?}, bits={}",
287 C::MIN
288 .map(|v| format!("{}", v))
289 .unwrap_or_else(|| String::from("MIN")),
290 C::MAX
291 .map(|v| format!("{}", v))
292 .unwrap_or_else(|| String::from("MAX")),
293 C::TAG,
294 bit_len,
295 ));
296 self.with_increased_indentation(|w| w.indented_println(format!("{:02x?}", value)));
297 Ok(())
298 }
299
300 fn write_boolean<C: boolean::Constraint>(&mut self, value: bool) -> Result<(), Self::Error> {
301 self.indented_println(format!("WRITING Boolean, tag={:?}", C::TAG));
302 self.with_increased_indentation(|w| w.indented_println(value.to_string()));
303 Ok(())
304 }
305
306 fn write_null<C: null::Constraint>(&mut self, _value: &Null) -> Result<(), Self::Error> {
307 self.indented_println(format!("WRITING NULL, tag={:?}", C::TAG));
308 Ok(())
309 }
310}