Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use strong-xml instead of serde and generalize frontend #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 56 additions & 59 deletions Cargo.lock

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

11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ exclude = [
[features]
default = ["textwrap"]

[dependencies.serde]
version = "1"
features = ["derive"]
[dependencies.strong-xml]
version = "0.6"

[dependencies.serde-xml-rs]
version = "0.5"
default-features = false
[dependencies.strum]
version = "0.24"
features = ["derive"]

[dependencies.colored]
version = "2"
Expand Down
37 changes: 19 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,20 @@ mod valgrind;
use colored::Colorize as _;
use std::env;
use std::process;
use valgrind::xml;

/// Nicely format the errors in the valgrind output, if there are any.
fn display_error(errors: &[valgrind::xml::Error]) {
fn display_error(errors: &[xml::Error]) {
// format the output in a helpful manner
for error in errors {
eprintln!(
"{:>12} leaked {} in {} block{}",
"Error".red().bold(),
bytesize::to_string(error.resources.bytes as _, true),
error.resources.blocks,
if error.resources.blocks == 1 { "" } else { "s" }
);
let mut info = Some("Info".cyan().bold());
error
.stack_trace
.frames
.iter()
.for_each(|frame| eprintln!("{:>12} at {}", info.take().unwrap_or_default(), frame));
eprintln!("{:>12} {}", "Error".red().bold(), error.description);
display_backtrace(&error.stack_trace);
for extra in &error.extra {
match extra {
xml::ErrorExtra::AuxWhat(s) => eprintln!("{:>12} {}", "Info".cyan().bold(), s),
xml::ErrorExtra::StackTrace(stack) => display_backtrace(stack),
}
}
}

let total: usize = errors.iter().map(|error| error.resources.bytes).sum();
Expand All @@ -52,6 +48,14 @@ fn display_error(errors: &[valgrind::xml::Error]) {
);
}

/// Display a backtrace provided by valgrind.
fn display_backtrace(backtrace: &xml::Stack) {
backtrace
.frames
.iter()
.for_each(|frame| eprintln!("{:>12} at {}", "", frame));
}

fn main() {
panic::replace_hook();

Expand Down Expand Up @@ -90,10 +94,7 @@ fn main() {
let command = env::args_os().skip(1);

let exit_code = match valgrind::execute(command) {
Ok(valgrind::xml::Output {
errors: Some(errors),
..
}) => {
Ok(xml::Output { errors, .. }) if !errors.is_empty() => {
display_error(&errors);
127
}
Expand Down
30 changes: 8 additions & 22 deletions src/valgrind/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

pub mod xml;

use serde::Deserialize;
use std::net::{SocketAddr, TcpListener};
use std::process::Command;
use std::{env, fmt, io::Read};
use std::{ffi::OsStr, process::Stdio};
use strong_xml::{XmlError, XmlRead};

/// Error type for valgrind-execution-related failures.
#[derive(Debug)]
Expand All @@ -27,7 +27,7 @@ pub enum Error {
///
/// This variant contains the inner deserialization error and the output of
/// valgrind.
MalformedOutput(serde_xml_rs::Error, Vec<u8>),
MalformedOutput(XmlError, Vec<u8>),
}

impl std::error::Error for Error {}
Expand Down Expand Up @@ -85,26 +85,12 @@ where
listener
.read_to_end(&mut output)
.map_err(|_| Error::SocketConnection)?;
let xml: xml::Output = xml::Output::deserialize(
&mut serde_xml_rs::Deserializer::new_from_reader(&*output)
.non_contiguous_seq_elements(true),
)
.map(|output_: xml::Output| {
let mut output = output_;
if let Some(err) = output.errors {
let new_err: Vec<xml::Error> = err
.into_iter()
.filter(|e| e.resources.bytes > 0 || e.resources.blocks > 0)
.collect();
if new_err.is_empty() {
output.errors = None;
} else {
output.errors = Some(new_err);
}
}
output
})
.map_err(|e| Error::MalformedOutput(e, output))?;
let output_str = match std::str::from_utf8(&output) {
Ok(s) => s,
Err(e) => return Err(Error::MalformedOutput(XmlError::Utf8(e), output))?,
};
let xml: xml::Output =
xml::Output::from_str(output_str).map_err(|e| Error::MalformedOutput(e, output))?;
Ok(xml)
});

Expand Down
Loading