Skip to content

Commit 8ce9bbd

Browse files
committed
Remove quick_xml::Result from public api
The quick_xml::Writer will never return anything but io errors, so the returned error is "unwrapped" to the underlying `std::io::Result` and returned directly
1 parent e16fe77 commit 8ce9bbd

File tree

5 files changed

+102
-77
lines changed

5 files changed

+102
-77
lines changed

src/bin/flamegraph.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<'a> Opt {
309309

310310
const PALETTE_MAP_FILE: &str = "palette.map"; // default name for the palette map file
311311

312-
fn main() -> quick_xml::Result<()> {
312+
fn main() -> io::Result<()> {
313313
let opt = Opt::parse();
314314

315315
// Initialize logger
@@ -344,8 +344,6 @@ fn main() -> quick_xml::Result<()> {
344344
}
345345

346346
save_consistent_palette_if_needed(&palette_map, PALETTE_MAP_FILE)
347-
.map_err(std::sync::Arc::new)
348-
.map_err(quick_xml::Error::Io)
349347
}
350348

351349
fn fetch_consistent_palette_if_needed(

src/flamegraph/merge.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ fn flow<'a, LI, TI>(
109109
pub(super) fn frames<'a, I>(
110110
lines: I,
111111
suppress_sort_check: bool,
112-
) -> quick_xml::Result<(Vec<TimedFrame<'a>>, usize, usize, usize)>
112+
) -> io::Result<(Vec<TimedFrame<'a>>, usize, usize, usize)>
113113
where
114114
I: IntoIterator<Item = &'a str>,
115115
{
@@ -128,9 +128,9 @@ where
128128
if !suppress_sort_check {
129129
if let Some(prev_line) = prev_line {
130130
if prev_line > line {
131-
return Err(quick_xml::Error::Io(
132-
io::Error::new(io::ErrorKind::InvalidData, "unsorted input lines detected")
133-
.into(),
131+
return Err(io::Error::new(
132+
io::ErrorKind::InvalidData,
133+
"unsorted input lines detected",
134134
));
135135
}
136136
}

src/flamegraph/mod.rs

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl Rectangle {
393393
///
394394
/// [differential flame graph]: http://www.brendangregg.com/blog/2014-11-09/differential-flame-graphs.html
395395
#[allow(clippy::cognitive_complexity)]
396-
pub fn from_lines<'a, I, W>(opt: &mut Options<'_>, lines: I, writer: W) -> quick_xml::Result<()>
396+
pub fn from_lines<'a, I, W>(opt: &mut Options<'_>, lines: I, writer: W) -> io::Result<()>
397397
where
398398
I: IntoIterator<Item = &'a str>,
399399
W: Write,
@@ -498,10 +498,12 @@ where
498498
extra: None,
499499
},
500500
)?;
501-
svg.write_event(Event::End(BytesEnd::new("svg")))?;
502-
svg.write_event(Event::Eof)?;
503-
return Err(quick_xml::Error::Io(
504-
io::Error::new(io::ErrorKind::InvalidData, "No stack counts found").into(),
501+
svg.write_event(Event::End(BytesEnd::new("svg")))
502+
.map_err(to_io_err)?;
503+
svg.write_event(Event::Eof).map_err(to_io_err)?;
504+
return Err(io::Error::new(
505+
io::ErrorKind::InvalidData,
506+
"No stack counts found",
505507
));
506508
}
507509

@@ -561,7 +563,8 @@ where
561563
("x", &container_x),
562564
("width", &container_width),
563565
("total_samples", &format!("{}", timemax)),
564-
])))?;
566+
])))
567+
.map_err(to_io_err)?;
565568

566569
// draw frames
567570
let mut samples_txt_buffer = num_format::Buffer::default();
@@ -644,9 +647,12 @@ where
644647
&buffer[info],
645648
)?;
646649

647-
svg.write_event(Event::Start(BytesStart::new("title")))?;
648-
svg.write_event(Event::Text(BytesText::new(title)))?;
649-
svg.write_event(Event::End(BytesEnd::new("title")))?;
650+
svg.write_event(Event::Start(BytesStart::new("title")))
651+
.map_err(to_io_err)?;
652+
svg.write_event(Event::Text(BytesText::new(title)))
653+
.map_err(to_io_err)?;
654+
svg.write_event(Event::End(BytesEnd::new("title")))
655+
.map_err(to_io_err)?;
650656

651657
// select the color of the rectangle
652658
let color = if frame.location.function == "--" {
@@ -723,15 +729,17 @@ where
723729

724730
buffer.clear();
725731
if has_href {
726-
svg.write_event(cache_a_end.borrow())?;
732+
svg.write_event(cache_a_end.borrow()).map_err(to_io_err)?;
727733
} else {
728-
svg.write_event(cache_g_end.borrow())?;
734+
svg.write_event(cache_g_end.borrow()).map_err(to_io_err)?;
729735
}
730736
}
731737

732-
svg.write_event(Event::End(BytesEnd::new("svg")))?;
733-
svg.write_event(Event::End(BytesEnd::new("svg")))?;
734-
svg.write_event(Event::Eof)?;
738+
svg.write_event(Event::End(BytesEnd::new("svg")))
739+
.map_err(to_io_err)?;
740+
svg.write_event(Event::End(BytesEnd::new("svg")))
741+
.map_err(to_io_err)?;
742+
svg.write_event(Event::Eof).map_err(to_io_err)?;
735743

736744
svg.into_inner().flush()?;
737745
Ok(())
@@ -745,7 +753,7 @@ fn write_container_start<'a, W: Write>(
745753
cache_g: &mut Event<'_>,
746754
frame: &merge::TimedFrame<'_>,
747755
mut title: &'a str,
748-
) -> quick_xml::Result<(bool, &'a str)> {
756+
) -> io::Result<(bool, &'a str)> {
749757
let frame_attributes = opt
750758
.func_frameattrs
751759
.frameattrs_for_func(frame.location.function);
@@ -754,18 +762,18 @@ fn write_container_start<'a, W: Write>(
754762
if let Some(frame_attributes) = frame_attributes {
755763
if frame_attributes.attrs.contains_key("xlink:href") {
756764
write_container_attributes(cache_a, frame_attributes);
757-
svg.write_event(cache_a.borrow())?;
765+
svg.write_event(cache_a.borrow()).map_err(to_io_err)?;
758766
has_href = true;
759767
} else {
760768
write_container_attributes(cache_g, frame_attributes);
761-
svg.write_event(cache_g.borrow())?;
769+
svg.write_event(cache_g.borrow()).map_err(to_io_err)?;
762770
}
763771
if let Some(ref t) = frame_attributes.title {
764772
title = t.as_str();
765773
}
766774
} else if let Event::Start(ref mut c) = cache_g {
767775
c.clear_attributes();
768-
svg.write_event(cache_g.borrow())?;
776+
svg.write_event(cache_g.borrow()).map_err(to_io_err)?;
769777
}
770778

771779
Ok((has_href, title))
@@ -779,10 +787,10 @@ fn write_container_start<'a, W: Write>(
779787
cache_g: &mut Event<'_>,
780788
_frame: &merge::TimedFrame<'_>,
781789
title: &'a str,
782-
) -> quick_xml::Result<(bool, &'a str)> {
790+
) -> io::Result<(bool, &'a str)> {
783791
if let Event::Start(ref mut c) = cache_g {
784792
c.clear_attributes();
785-
svg.write_event(cache_g.borrow())?;
793+
svg.write_event(cache_g.borrow()).map_err(to_io_err)?;
786794
}
787795

788796
Ok((false, title))
@@ -809,7 +817,7 @@ fn write_container_attributes(event: &mut Event<'_>, frame_attributes: &FrameAtt
809817
/// See [`from_lines`] for the expected format of each line.
810818
///
811819
/// The resulting flame graph will be written out to `writer` in SVG format.
812-
pub fn from_reader<R, W>(opt: &mut Options<'_>, reader: R, writer: W) -> quick_xml::Result<()>
820+
pub fn from_reader<R, W>(opt: &mut Options<'_>, reader: R, writer: W) -> io::Result<()>
813821
where
814822
R: Read,
815823
W: Write,
@@ -822,18 +830,15 @@ where
822830
/// See [`from_lines`] for the expected format of each line.
823831
///
824832
/// The resulting flame graph will be written out to `writer` in SVG format.
825-
pub fn from_readers<R, W>(opt: &mut Options<'_>, readers: R, writer: W) -> quick_xml::Result<()>
833+
pub fn from_readers<R, W>(opt: &mut Options<'_>, readers: R, writer: W) -> io::Result<()>
826834
where
827835
R: IntoIterator,
828836
R::Item: Read,
829837
W: Write,
830838
{
831839
let mut input = String::new();
832840
for mut reader in readers {
833-
reader
834-
.read_to_string(&mut input)
835-
.map_err(std::sync::Arc::new)
836-
.map_err(quick_xml::Error::Io)?;
841+
reader.read_to_string(&mut input)?;
837842
}
838843
from_lines(opt, input.lines(), writer)
839844
}
@@ -842,19 +847,13 @@ where
842847
/// and write the result to provided `writer`.
843848
///
844849
/// If files is empty, STDIN will be used as input.
845-
pub fn from_files<W: Write>(
846-
opt: &mut Options<'_>,
847-
files: &[PathBuf],
848-
writer: W,
849-
) -> quick_xml::Result<()> {
850+
pub fn from_files<W: Write>(opt: &mut Options<'_>, files: &[PathBuf], writer: W) -> io::Result<()> {
850851
if files.is_empty() || files.len() == 1 && files[0].to_str() == Some("-") {
851852
let stdin = io::stdin();
852853
let r = BufReader::with_capacity(128 * 1024, stdin.lock());
853854
from_reader(opt, r, writer)
854855
} else if files.len() == 1 {
855-
let r = File::open(&files[0])
856-
.map_err(std::sync::Arc::new)
857-
.map_err(quick_xml::Error::Io)?;
856+
let r = File::open(&files[0])?;
858857
from_reader(opt, r, writer)
859858
} else {
860859
let stdin = io::stdin();
@@ -868,9 +867,7 @@ pub fn from_files<W: Write>(
868867
stdin_added = true;
869868
}
870869
} else {
871-
let r = File::open(infile)
872-
.map_err(std::sync::Arc::new)
873-
.map_err(quick_xml::Error::Io)?;
870+
let r = File::open(infile)?;
874871
readers.push(Box::new(r));
875872
}
876873
}
@@ -896,7 +893,7 @@ fn filled_rectangle<W: Write>(
896893
rect: &Rectangle,
897894
color: Color,
898895
cache_rect: &mut Event<'_>,
899-
) -> quick_xml::Result<()> {
896+
) -> io::Result<()> {
900897
let x = write!(buffer, "{:.4}%", rect.x1_pct);
901898
let y = write_usize(buffer, rect.y1);
902899
let width = write!(buffer, "{:.4}%", rect.width_pct());
@@ -920,13 +917,20 @@ fn filled_rectangle<W: Write>(
920917
} else {
921918
unreachable!("cache wrapper was of wrong type: {:?}", cache_rect);
922919
}
923-
svg.write_event(cache_rect.borrow())
920+
svg.write_event(cache_rect.borrow()).map_err(to_io_err)
924921
}
925922

926923
fn write_usize(buffer: &mut StrStack, value: usize) -> usize {
927924
buffer.push(itoa::Buffer::new().format(value))
928925
}
929926

927+
fn to_io_err(err: quick_xml::Error) -> io::Error {
928+
let quick_xml::Error::Io(err) = err else {
929+
unreachable!("quick_xml write operations can only ever return std::io errors")
930+
};
931+
std::sync::Arc::into_inner(err).expect("Error value to be present when given by quick_xml")
932+
}
933+
930934
#[cfg(test)]
931935
mod tests {
932936
use super::{Direction, Options};

0 commit comments

Comments
 (0)