Skip to content

Commit 98edecd

Browse files
committed
refactor: suppress use of to_string_lossy
1 parent 6c7a748 commit 98edecd

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

sailfish-compiler/src/procmacro.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,18 @@ fn derive_template_impl(tokens: TokenStream) -> Result<TokenStream, syn::Error>
225225
let report = compile(&*input_file, &*output_file, config)
226226
.map_err(|e| syn::Error::new(Span::call_site(), e))?;
227227

228-
let input_file_string = input_file.to_string_lossy();
229-
let output_file_string = output_file.to_string_lossy();
228+
let input_file_string = input_file
229+
.to_str()
230+
.unwrap_or_else(|| panic!("Non UTF-8 file name: {:?}", input_file));
231+
let output_file_string = output_file
232+
.to_str()
233+
.unwrap_or_else(|| panic!("Non UTF-8 file name: {:?}", output_file));
230234

231235
let mut include_bytes_seq = quote! { include_bytes!(#input_file_string); };
232236
for dep in report.deps {
233-
let dep_string = dep.to_string_lossy();
234-
include_bytes_seq.extend(quote! { include_bytes!(#dep_string); });
237+
if let Some(dep_string) = dep.to_str() {
238+
include_bytes_seq.extend(quote! { include_bytes!(#dep_string); });
239+
}
235240
}
236241

237242
// Generate tokens

sailfish-compiler/src/resolver.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,16 @@ impl<'h> ResolverImpl<'h> {
5454
} else {
5555
self.path_stack.last().unwrap().parent().unwrap().join(arg)
5656
};
57-
let absolute_path_str = absolute_path.to_string_lossy();
58-
return Ok(syn::parse2(quote! { include!(#absolute_path_str) }).unwrap());
57+
58+
return if let Some(absolute_path_str) = absolute_path.to_str() {
59+
Ok(syn::parse2(quote! { include!(#absolute_path_str) }).unwrap())
60+
} else {
61+
let msg = format!(
62+
"cannot include path with non UTF-8 character: {:?}",
63+
absolute_path
64+
);
65+
Err(make_error!(ErrorKind::AnalyzeError(msg)))
66+
};
5967
}
6068

6169
// resolve the template file path

0 commit comments

Comments
 (0)