@@ -154,9 +154,9 @@ mod templateexpression;
154154use parseresult:: show_errors;
155155use std:: env;
156156use std:: error:: Error ;
157- use std:: fmt:: { self , Debug , Display } ;
157+ use std:: fmt:: { self , Debug , Display , Write as _ } ;
158158use std:: fs:: { create_dir_all, read_dir, File } ;
159- use std:: io:: { self , Read , Write } ;
159+ use std:: io:: { self , Read } ;
160160use std:: path:: { Path , PathBuf } ;
161161use template:: template;
162162
@@ -211,7 +211,7 @@ pub use staticfiles::StaticFiles;
211211///
212212/// [cargo]: https://doc.rust-lang.org/cargo/
213213pub struct Ructe {
214- f : Vec < u8 > ,
214+ f : String ,
215215 outdir : PathBuf ,
216216}
217217
@@ -241,32 +241,32 @@ impl Ructe {
241241 ///
242242 /// [cargo]: https://doc.rust-lang.org/cargo/
243243 pub fn new ( outdir : PathBuf ) -> Result < Ructe > {
244- let mut f = Vec :: with_capacity ( 512 ) ;
244+ let mut f = String :: with_capacity ( 512 ) ;
245245 let outdir = outdir. join ( "templates" ) ;
246246 create_dir_all ( & outdir) ?;
247- f. write_all ( b "pub mod templates {\n ") ?;
247+ f. write_str ( "pub mod templates {\n " ) ?;
248248 write_if_changed (
249249 & outdir. join ( "_utils.rs" ) ,
250- include_bytes ! ( concat!(
250+ include_str ! ( concat!(
251251 env!( "CARGO_MANIFEST_DIR" ) ,
252252 "/src/templates/utils.rs"
253253 ) ) ,
254254 ) ?;
255- f. write_all (
256- b "#[doc(hidden)]\n mod _utils;\n \
257- #[doc(inline)]\n pub use self::_utils::*;\n \n ",
255+ f. write_str (
256+ "#[doc(hidden)]\n mod _utils;\n \
257+ #[doc(inline)]\n pub use self::_utils::*;\n \n ",
258258 ) ?;
259259 if cfg ! ( feature = "warp03" ) {
260260 write_if_changed (
261261 & outdir. join ( "_utils_warp03.rs" ) ,
262- include_bytes ! ( concat!(
262+ include_str ! ( concat!(
263263 env!( "CARGO_MANIFEST_DIR" ) ,
264264 "/src/templates/utils_warp03.rs"
265265 ) ) ,
266266 ) ?;
267- f. write_all (
268- b "#[doc(hidden)]\n mod _utils_warp03;\n \
269- #[doc(inline)]\n pub use self::_utils_warp03::*;\n \n ",
267+ f. write_str (
268+ "#[doc(hidden)]\n mod _utils_warp03;\n \
269+ #[doc(inline)]\n pub use self::_utils_warp03::*;\n \n ",
270270 ) ?;
271271 }
272272 Ok ( Ructe { f, outdir } )
@@ -320,7 +320,7 @@ impl Ructe {
320320 ///
321321 /// [`StaticFile`]: templates::StaticFile
322322 pub fn statics ( & mut self ) -> Result < StaticFiles > {
323- self . f . write_all ( b "pub mod statics;") ?;
323+ self . f . write_str ( "pub mod statics;" ) ?;
324324 StaticFiles :: for_template_dir (
325325 & self . outdir ,
326326 & PathBuf :: from ( get_env ( "CARGO_MANIFEST_DIR" ) ?) ,
@@ -330,27 +330,24 @@ impl Ructe {
330330
331331impl Drop for Ructe {
332332 fn drop ( & mut self ) {
333- let _ = self . f . write_all ( b "}\n ") ;
333+ let _ = self . f . write_str ( "}\n " ) ;
334334 let _ =
335335 write_if_changed ( & self . outdir . join ( "../templates.rs" ) , & self . f ) ;
336336 }
337337}
338338
339- fn write_if_changed ( path : & Path , content : & [ u8 ] ) -> io :: Result < ( ) > {
340- use std:: fs:: { read , write} ;
341- if let Ok ( old) = read ( path) {
339+ fn write_if_changed ( path : & Path , content : & str ) -> Result < ( ) > {
340+ use std:: fs:: { read_to_string , write} ;
341+ if let Ok ( old) = read_to_string ( path) {
342342 if old == content {
343343 return Ok ( ( ) ) ;
344344 }
345345 }
346- write ( path, content)
346+ write ( path, content. as_bytes ( ) ) ?;
347+ Ok ( ( ) )
347348}
348349
349- fn handle_entries (
350- f : & mut impl Write ,
351- indir : & Path ,
352- outdir : & Path ,
353- ) -> Result < ( ) > {
350+ fn handle_entries ( f : & mut String , indir : & Path , outdir : & Path ) -> Result < ( ) > {
354351 println ! ( "cargo:rerun-if-changed={}" , indir. display( ) ) ;
355352 for entry in read_dir ( indir) ? {
356353 let entry = entry?;
@@ -359,11 +356,11 @@ fn handle_entries(
359356 if let Some ( filename) = entry. file_name ( ) . to_str ( ) {
360357 let outdir = outdir. join ( filename) ;
361358 create_dir_all ( & outdir) ?;
362- let mut modrs = Vec :: with_capacity ( 512 ) ;
363- modrs. write_all (
364- b "#[allow(clippy::useless_attribute, unused)]\n \
365- use super::{Html,ToHtml};\n ",
366- ) ? ;
359+ let mut modrs = String :: with_capacity ( 512 ) ;
360+ modrs. push_str (
361+ "#[allow(clippy::useless_attribute, unused)]\n \
362+ use super::{Html,ToHtml};\n ",
363+ ) ;
367364 handle_entries ( & mut modrs, & path, & outdir) ?;
368365 write_if_changed ( & outdir. join ( "mod.rs" ) , & modrs) ?;
369366 writeln ! ( f, "pub mod {filename};\n " ) ?;
@@ -391,17 +388,13 @@ fn handle_entries(
391388 Ok ( ( ) )
392389}
393390
394- fn handle_template (
395- name : & str ,
396- path : & Path ,
397- outdir : & Path ,
398- ) -> io:: Result < bool > {
391+ fn handle_template ( name : & str , path : & Path , outdir : & Path ) -> Result < bool > {
399392 let mut input = File :: open ( path) ?;
400393 let mut buf = Vec :: new ( ) ;
401394 input. read_to_end ( & mut buf) ?;
402395 match template ( & buf) {
403396 Ok ( ( _, t) ) => {
404- let mut data = Vec :: new ( ) ;
397+ let mut data = String :: new ( ) ;
405398 t. write_rust ( & mut data, name) ?;
406399 write_if_changed (
407400 & outdir. join ( format ! ( "template_{name}.rs" ) ) ,
@@ -429,16 +422,19 @@ pub enum RucteError {
429422 Io ( io:: Error ) ,
430423 /// Error resolving a given environment variable.
431424 Env ( String , env:: VarError ) ,
432- /// Error bundling a sass stylesheet as css.
425+ /// A build-time formatting error in Ructe
426+ Fmt ( fmt:: Error ) ,
433427 #[ cfg( feature = "sass" ) ]
428+ /// Error bundling a sass stylesheet as css.
434429 Sass ( rsass:: Error ) ,
435430}
436431
437432impl Error for RucteError {
438433 fn source ( & self ) -> Option < & ( dyn Error + ' static ) > {
439434 match & self {
440- RucteError :: Io ( e) => Some ( e) ,
441- RucteError :: Env ( _, e) => Some ( e) ,
435+ Self :: Io ( e) => Some ( e) ,
436+ Self :: Env ( _, e) => Some ( e) ,
437+ Self :: Fmt ( e) => Some ( e) ,
442438 #[ cfg( feature = "sass" ) ]
443439 RucteError :: Sass ( e) => Some ( e) ,
444440 }
@@ -455,6 +451,7 @@ impl Debug for RucteError {
455451 match self {
456452 RucteError :: Io ( err) => Display :: fmt ( err, out) ,
457453 RucteError :: Env ( var, err) => write ! ( out, "{var:?}: {err}" ) ,
454+ Self :: Fmt ( err) => Display :: fmt ( err, out) ,
458455 #[ cfg( feature = "sass" ) ]
459456 RucteError :: Sass ( err) => Debug :: fmt ( err, out) ,
460457 }
@@ -466,6 +463,11 @@ impl From<io::Error> for RucteError {
466463 RucteError :: Io ( e)
467464 }
468465}
466+ impl From < fmt:: Error > for RucteError {
467+ fn from ( value : fmt:: Error ) -> Self {
468+ Self :: Fmt ( value)
469+ }
470+ }
469471
470472#[ cfg( feature = "sass" ) ]
471473impl From < rsass:: Error > for RucteError {
@@ -475,4 +477,4 @@ impl From<rsass::Error> for RucteError {
475477}
476478
477479/// A result where the error type is a [`RucteError`].
478- pub type Result < T > = std:: result:: Result < T , RucteError > ;
480+ pub type Result < T , E = RucteError > = std:: result:: Result < T , E > ;
0 commit comments