Skip to content

Commit

Permalink
internal: refactor render
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Jul 8, 2024
1 parent 7bc7574 commit 6255b01
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 50 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 14 additions & 25 deletions crates/moonbuild/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,17 @@ pub fn n2_run_interface(
output_path: PathBuf,
) -> anyhow::Result<Option<usize>> {
let logger = Arc::new(Mutex::new(vec![]));
let use_fancy = terminal::use_fancy();

let catcher = logger.clone();
let render_and_catch = move |output: &str| {
let raw_content_lines: Vec<&str> = output.split('\n').filter(|it| !it.is_empty()).collect();

for output_content in raw_content_lines {
catcher.lock().unwrap().push(output_content.to_owned());
match serde_json_lenient::from_str::<moonutil::render::MooncDiagnostic>(output_content)
{
Ok(report) => {
report.render();
}
Err(_) => {
println!("{}", output_content);
}
}
}
output
.split('\n')
.filter(|it| !it.is_empty())
.for_each(|content| {
catcher.lock().unwrap().push(content.to_owned());
moonutil::render::MooncDiagnostic::render(content, use_fancy);
});
};

let mut progress = create_progress_console(false, quiet, Some(Box::new(render_and_catch)));
Expand Down Expand Up @@ -118,17 +112,12 @@ pub fn n2_run_interface(
let raw_json = std::fs::read_to_string(&output_path)
.context(format!("failed to open `{}`", output_path.display()))?;

let raw_json_lines: Vec<&str> = raw_json.split('\n').filter(|it| !it.is_empty()).collect();
for line in raw_json_lines {
match serde_json_lenient::from_str::<moonutil::render::MooncDiagnostic>(line) {
Ok(report) => {
report.render();
}
Err(_) => {
println!("{}", line);
}
}
}
raw_json
.split('\n')
.filter(|it| !it.is_empty())
.for_each(|content| {
moonutil::render::MooncDiagnostic::render(content, use_fancy);
});
} else {
let mut output_file = std::fs::File::create(output_path)?;

Expand Down
2 changes: 2 additions & 0 deletions crates/moonutil/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ semver.workspace = true
fs4.workspace = true
petgraph.workspace = true
ariadne.workspace = true
env_logger.workspace = true
log.workspace = true

[dev-dependencies]
expect-test.workspace = true
Expand Down
73 changes: 48 additions & 25 deletions crates/moonutil/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,57 @@ pub struct Position {
}

impl MooncDiagnostic {
pub fn render(&self) {
let (kind, color) = self.get_level_and_color();
pub fn render(content: &str, use_fancy: bool) {
match serde_json_lenient::from_str::<MooncDiagnostic>(content) {
Ok(diagnostic) => {
let (kind, color) = diagnostic.get_level_and_color();

// for no-location diagnostic, like Missing main function in the main package(4067)
if self.location.path.is_empty() {
println!(
"{}",
format!("[{}] {}: {}", self.error_code, kind, self.message).fg(color)
);
} else {
let source_file_path = &self.location.path;
let source_file = std::fs::read_to_string(source_file_path)
.unwrap_or_else(|_| panic!("failed to read {}", source_file_path));
// for no-location diagnostic, like Missing main function in the main package(4067)
if diagnostic.location.path.is_empty() {
println!(
"{}",
format!(
"[{}] {}: {}",
diagnostic.error_code, kind, diagnostic.message
)
.fg(color)
);
} else {
let source_file_path = &diagnostic.location.path;
let source_file = std::fs::read_to_string(source_file_path)
.unwrap_or_else(|_| panic!("failed to read {}", source_file_path));

ariadne::Report::build(kind, source_file_path, self.location.start.offset as usize)
.with_code(self.error_code)
.with_message(&self.message)
.with_label(
ariadne::Label::new((
let mut report_builder = ariadne::Report::build(
kind,
source_file_path,
self.location.start.offset as usize..self.location.end.offset as usize,
))
.with_message((&self.message).fg(color))
.with_color(color),
)
.finish()
.print((source_file_path, ariadne::Source::from(source_file)))
.unwrap();
diagnostic.location.start.offset as usize,
)
.with_code(diagnostic.error_code)
.with_message(&diagnostic.message)
.with_label(
ariadne::Label::new((
source_file_path,
diagnostic.location.start.offset as usize
..diagnostic.location.end.offset as usize,
))
.with_message((&diagnostic.message).fg(color))
.with_color(color),
);

if !use_fancy {
let config = ariadne::Config::default().with_color(false);
report_builder = report_builder.with_config(config);
}

report_builder
.finish()
.print((source_file_path, ariadne::Source::from(source_file)))
.unwrap();
}
}
Err(_) => {
println!("{}", content);
}
}
}

Expand Down

0 comments on commit 6255b01

Please sign in to comment.