@@ -20,87 +20,6 @@ use std::{
2020/// Number of space used to indent lines when no alignement is required.
2121pub ( crate ) const INDENTATION_SIZE : usize = 2 ;
2222
23- /// A string representing one line of a description or match explanation.
24- #[ derive( Debug ) ]
25- struct Fragment ( Cow < ' static , str > ) ;
26-
27- impl Fragment {
28- fn render ( & self , f : & mut dyn Write ) -> Result {
29- write ! ( f, "{}" , self . 0 )
30- }
31- }
32-
33- /// A sequence of [`Fragment`] or a nested [`List`].
34- ///
35- /// This may be rendered with a prefix specified by the [`Decoration`] of the containing [`List`].
36- /// In this case, all lines are indented to align with the first character of the first line of the
37- /// block.
38- #[ derive( Debug ) ]
39- enum Block {
40- /// A block of text.
41- ///
42- /// Each constituent [`Fragment`] contains one line of text. The lines are indented
43- /// uniformly to the current indentation of this block when rendered.
44- Literal ( Vec < Fragment > ) ,
45-
46- /// A nested [`List`].
47- ///
48- /// The [`List`] is rendered recursively at the next level of indentation.
49- Nested ( List ) ,
50- }
51-
52- impl Block {
53- fn nested ( inner : List ) -> Self {
54- Self :: Nested ( inner)
55- }
56-
57- fn render ( & self , f : & mut dyn Write , indentation : usize , prefix : Cow < ' static , str > ) -> Result {
58- match self {
59- Self :: Literal ( fragments) => {
60- if fragments. is_empty ( ) {
61- return Ok ( ( ) ) ;
62- }
63-
64- write ! ( f, "{:indentation$}{prefix}" , "" ) ?;
65- fragments[ 0 ] . render ( f) ?;
66- let block_indentation = indentation + prefix. as_ref ( ) . len ( ) ;
67- for fragment in & fragments[ 1 ..] {
68- writeln ! ( f) ?;
69- write ! ( f, "{:block_indentation$}" , "" ) ?;
70- fragment. render ( f) ?;
71- }
72- Ok ( ( ) )
73- }
74- Self :: Nested ( inner) => inner. render_with_prefix (
75- f,
76- indentation + INDENTATION_SIZE . saturating_sub ( prefix. len ( ) ) ,
77- prefix,
78- ) ,
79- }
80- }
81- }
82-
83- impl From < String > for Block {
84- fn from ( value : String ) -> Self {
85- Block :: Literal ( value. lines ( ) . map ( |v| Fragment ( v. to_string ( ) . into ( ) ) ) . collect ( ) )
86- }
87- }
88-
89- impl From < & ' static str > for Block {
90- fn from ( value : & ' static str ) -> Self {
91- Block :: Literal ( value. lines ( ) . map ( |v| Fragment ( v. into ( ) ) ) . collect ( ) )
92- }
93- }
94-
95- impl From < Cow < ' static , str > > for Block {
96- fn from ( value : Cow < ' static , str > ) -> Self {
97- match value {
98- Cow :: Borrowed ( value) => value. into ( ) ,
99- Cow :: Owned ( value) => value. into ( ) ,
100- }
101- }
102- }
103-
10423/// A list of [`Block`] possibly rendered with a [`Decoration`].
10524///
10625/// This is the top-level renderable component, corresponding to the description or match
@@ -244,6 +163,87 @@ impl FromIterator<List> for List {
244163 }
245164}
246165
166+ /// A sequence of [`Fragment`] or a nested [`List`].
167+ ///
168+ /// This may be rendered with a prefix specified by the [`Decoration`] of the containing [`List`].
169+ /// In this case, all lines are indented to align with the first character of the first line of the
170+ /// block.
171+ #[ derive( Debug ) ]
172+ enum Block {
173+ /// A block of text.
174+ ///
175+ /// Each constituent [`Fragment`] contains one line of text. The lines are indented
176+ /// uniformly to the current indentation of this block when rendered.
177+ Literal ( Vec < Fragment > ) ,
178+
179+ /// A nested [`List`].
180+ ///
181+ /// The [`List`] is rendered recursively at the next level of indentation.
182+ Nested ( List ) ,
183+ }
184+
185+ impl Block {
186+ fn nested ( inner : List ) -> Self {
187+ Self :: Nested ( inner)
188+ }
189+
190+ fn render ( & self , f : & mut dyn Write , indentation : usize , prefix : Cow < ' static , str > ) -> Result {
191+ match self {
192+ Self :: Literal ( fragments) => {
193+ if fragments. is_empty ( ) {
194+ return Ok ( ( ) ) ;
195+ }
196+
197+ write ! ( f, "{:indentation$}{prefix}" , "" ) ?;
198+ fragments[ 0 ] . render ( f) ?;
199+ let block_indentation = indentation + prefix. as_ref ( ) . len ( ) ;
200+ for fragment in & fragments[ 1 ..] {
201+ writeln ! ( f) ?;
202+ write ! ( f, "{:block_indentation$}" , "" ) ?;
203+ fragment. render ( f) ?;
204+ }
205+ Ok ( ( ) )
206+ }
207+ Self :: Nested ( inner) => inner. render_with_prefix (
208+ f,
209+ indentation + INDENTATION_SIZE . saturating_sub ( prefix. len ( ) ) ,
210+ prefix,
211+ ) ,
212+ }
213+ }
214+ }
215+
216+ impl From < String > for Block {
217+ fn from ( value : String ) -> Self {
218+ Block :: Literal ( value. lines ( ) . map ( |v| Fragment ( v. to_string ( ) . into ( ) ) ) . collect ( ) )
219+ }
220+ }
221+
222+ impl From < & ' static str > for Block {
223+ fn from ( value : & ' static str ) -> Self {
224+ Block :: Literal ( value. lines ( ) . map ( |v| Fragment ( v. into ( ) ) ) . collect ( ) )
225+ }
226+ }
227+
228+ impl From < Cow < ' static , str > > for Block {
229+ fn from ( value : Cow < ' static , str > ) -> Self {
230+ match value {
231+ Cow :: Borrowed ( value) => value. into ( ) ,
232+ Cow :: Owned ( value) => value. into ( ) ,
233+ }
234+ }
235+ }
236+
237+ /// A string representing one line of a description or match explanation.
238+ #[ derive( Debug ) ]
239+ struct Fragment ( Cow < ' static , str > ) ;
240+
241+ impl Fragment {
242+ fn render ( & self , f : & mut dyn Write ) -> Result {
243+ write ! ( f, "{}" , self . 0 )
244+ }
245+ }
246+
247247/// The decoration which appears on [`Block`] of a [`List`] when rendered.
248248#[ derive( Debug , Default ) ]
249249enum Decoration {
0 commit comments