1
1
mirror of https://github.com/tweag/nickel.git synced 2024-09-21 08:31:15 +03:00

apply review to src/programs.rs

This commit is contained in:
Guillaume Desforges 2023-01-18 22:32:21 +01:00
parent bdb9afc599
commit 201455755f

View File

@ -163,29 +163,25 @@ impl<EC: EvalCache> Program<EC> {
report(self.vm.import_resolver_mut(), error, self.color_opt) report(self.vm.import_resolver_mut(), error, self.color_opt)
} }
/// Wrapper for [`report`]. /// Build an error report as a string and return it.
pub fn report_as_str<E>(&mut self, error: E) -> String pub fn report_as_str<E>(&mut self, error: E) -> String
where where
E: ToDiagnostic<FileId>, E: ToDiagnostic<FileId>,
{ {
let contracts_id = self.vm.import_resolver().id_of("<stdlib/contract.ncl>"); let cache = self.vm.import_resolver_mut();
let diagnostics = let contracts_id = cache.id_of("<stdlib/contract.ncl>");
error.to_diagnostic(self.vm.import_resolver_mut().files_mut(), contracts_id); let diagnostics = error.to_diagnostic(cache.files_mut(), contracts_id);
let mut buffer = Ansi::new(Cursor::new(Vec::new())); let mut buffer = Ansi::new(Cursor::new(Vec::new()));
let config = codespan_reporting::term::Config::default(); let config = codespan_reporting::term::Config::default();
// write to `buffer` // write to `buffer`
diagnostics diagnostics
.iter() .iter()
.try_for_each(|d| { .try_for_each(|d| {
codespan_reporting::term::emit( codespan_reporting::term::emit(&mut buffer, &config, cache.files_mut(), d)
&mut buffer,
&config,
self.vm.import_resolver_mut().files_mut(),
d,
)
}) })
// safe because writing to a cursor in memory // safe because writing to a cursor in memory
.unwrap(); .unwrap();
// unwrap(): emit() should only print valid utf8 to the the buffer
String::from_utf8(buffer.into_inner().into_inner()).unwrap() String::from_utf8(buffer.into_inner().into_inner()).unwrap()
} }