refactor: suppress use of to_string_lossy

This commit is contained in:
Kogia-sima 2020-12-16 19:13:37 +09:00
parent 6c7a74823f
commit 98edecdb3a
2 changed files with 19 additions and 6 deletions

View File

@ -225,13 +225,18 @@ fn derive_template_impl(tokens: TokenStream) -> Result<TokenStream, syn::Error>
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

View File

@ -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