@@ -89,11 +89,12 @@ impl TemplatedJson {
8989 /// # Errors
9090 ///
9191 /// Returns an error if the underlying generator fails.
92- fn generate_line ( & mut self , rng : & mut impl Rng ) -> Result < ( ) , Error > {
92+ pub fn generate_line ( & mut self , rng : & mut impl Rng ) -> Result < & str , Error > {
9393 self . ctx . fill ( None ) ;
9494 self . line_buf . clear ( ) ;
9595 self . generator
96- . generate ( rng, & mut self . ctx , & self . definitions , & mut self . line_buf )
96+ . generate ( rng, & mut self . ctx , & self . definitions , & mut self . line_buf ) ?;
97+ Ok ( & self . line_buf . as_str ( ) )
9798 }
9899}
99100
@@ -106,12 +107,12 @@ impl crate::Serialize for TemplatedJson {
106107 let mut bytes_remaining = max_bytes;
107108 loop {
108109 self . generate_line ( & mut rng) ?;
109- let line_length = self . line_buf . as_str ( ) . len ( ) + 1 ; // +1 for the trailing newline
110- let Some ( remainder) = bytes_remaining. checked_sub ( line_length) else {
110+ self . line_buf . push_char ( '\n' ) ;
111+ let line = self . line_buf . as_str ( ) ;
112+ let Some ( remainder) = bytes_remaining. checked_sub ( line. len ( ) ) else {
111113 break ;
112114 } ;
113- self . line_buf . push_char ( '\n' ) ;
114- writer. write_all ( self . line_buf . as_str ( ) . as_bytes ( ) ) ?;
115+ writer. write_all ( line. as_bytes ( ) ) ?;
115116 bytes_remaining = remainder;
116117 }
117118 Ok ( ( ) )
0 commit comments