From 98edecdb3a4371e5b91abb1877c6770bdf257801 Mon Sep 17 00:00:00 2001 From: Kogia-sima Date: Wed, 16 Dec 2020 19:13:37 +0900 Subject: [PATCH] refactor: suppress use of to_string_lossy --- sailfish-compiler/src/procmacro.rs | 13 +++++++++---- sailfish-compiler/src/resolver.rs | 12 ++++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/sailfish-compiler/src/procmacro.rs b/sailfish-compiler/src/procmacro.rs index 49dec65..112035e 100644 --- a/sailfish-compiler/src/procmacro.rs +++ b/sailfish-compiler/src/procmacro.rs @@ -225,13 +225,18 @@ fn derive_template_impl(tokens: TokenStream) -> Result let report = compile(&*input_file, &*output_file, config) .map_err(|e| syn::Error::new(Span::call_site(), e))?; - let input_file_string = input_file.to_string_lossy(); - let output_file_string = output_file.to_string_lossy(); + let input_file_string = input_file + .to_str() + .unwrap_or_else(|| panic!("Non UTF-8 file name: {:?}", input_file)); + let output_file_string = output_file + .to_str() + .unwrap_or_else(|| panic!("Non UTF-8 file name: {:?}", output_file)); let mut include_bytes_seq = quote! { include_bytes!(#input_file_string); }; for dep in report.deps { - let dep_string = dep.to_string_lossy(); - include_bytes_seq.extend(quote! { include_bytes!(#dep_string); }); + if let Some(dep_string) = dep.to_str() { + include_bytes_seq.extend(quote! { include_bytes!(#dep_string); }); + } } // Generate tokens diff --git a/sailfish-compiler/src/resolver.rs b/sailfish-compiler/src/resolver.rs index 9a1b521..1a5c6c6 100644 --- a/sailfish-compiler/src/resolver.rs +++ b/sailfish-compiler/src/resolver.rs @@ -54,8 +54,16 @@ impl<'h> ResolverImpl<'h> { } else { self.path_stack.last().unwrap().parent().unwrap().join(arg) }; - let absolute_path_str = absolute_path.to_string_lossy(); - return Ok(syn::parse2(quote! { include!(#absolute_path_str) }).unwrap()); + + return if let Some(absolute_path_str) = absolute_path.to_str() { + Ok(syn::parse2(quote! { include!(#absolute_path_str) }).unwrap()) + } else { + let msg = format!( + "cannot include path with non UTF-8 character: {:?}", + absolute_path + ); + Err(make_error!(ErrorKind::AnalyzeError(msg))) + }; } // resolve the template file path