Skip to content

Commit 628a8ce

Browse files
authored
Remove failure (#644)
1 parent 1595ab9 commit 628a8ce

File tree

25 files changed

+175
-450
lines changed

25 files changed

+175
-450
lines changed

Cargo.lock

Lines changed: 27 additions & 82 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

analyze/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ edition = "2018"
1313
path = "./analyze.rs"
1414

1515
[dependencies]
16+
anyhow = "1.0"
1617
twiggy-ir = { version = "=0.7.0", path = "../ir" }
1718
twiggy-opt = { version = "=0.7.0", path = "../opt", default-features = false }
1819
twiggy-traits = { version = "=0.7.0", path = "../traits" }

analyze/analyses/diff.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
use std::cmp;
2-
use std::collections::{HashMap, HashSet};
3-
use std::io;
4-
1+
use crate::formats::json;
2+
use crate::formats::table::{Align, Table};
3+
use anyhow::anyhow;
54
use csv;
65
use regex;
76
use serde::{self, ser::SerializeStruct};
8-
9-
use crate::formats::json;
10-
use crate::formats::table::{Align, Table};
7+
use std::cmp;
8+
use std::collections::{HashMap, HashSet};
9+
use std::io;
1110
use twiggy_ir as ir;
1211
use twiggy_opt as opt;
1312
use twiggy_traits as traits;
@@ -52,7 +51,7 @@ impl serde::Serialize for DiffEntry {
5251

5352
impl traits::Emit for Diff {
5453
#[cfg(feature = "emit_text")]
55-
fn emit_text(&self, _items: &ir::Items, dest: &mut dyn io::Write) -> Result<(), traits::Error> {
54+
fn emit_text(&self, _items: &ir::Items, dest: &mut dyn io::Write) -> anyhow::Result<()> {
5655
let mut table = Table::with_header(vec![
5756
(Align::Right, "Delta Bytes".into()),
5857
(Align::Left, "Item".to_string()),
@@ -68,7 +67,7 @@ impl traits::Emit for Diff {
6867
}
6968

7069
#[cfg(feature = "emit_json")]
71-
fn emit_json(&self, _items: &ir::Items, dest: &mut dyn io::Write) -> Result<(), traits::Error> {
70+
fn emit_json(&self, _items: &ir::Items, dest: &mut dyn io::Write) -> anyhow::Result<()> {
7271
let mut arr = json::array(dest)?;
7372

7473
for entry in &self.deltas {
@@ -81,7 +80,7 @@ impl traits::Emit for Diff {
8180
}
8281

8382
#[cfg(feature = "emit_csv")]
84-
fn emit_csv(&self, _items: &ir::Items, dest: &mut dyn io::Write) -> Result<(), traits::Error> {
83+
fn emit_csv(&self, _items: &ir::Items, dest: &mut dyn io::Write) -> anyhow::Result<()> {
8584
let mut wtr = csv::Writer::from_writer(dest);
8685

8786
for entry in &self.deltas {
@@ -98,7 +97,7 @@ pub fn diff(
9897
old_items: &mut ir::Items,
9998
new_items: &mut ir::Items,
10099
opts: &opt::Diff,
101-
) -> Result<Box<dyn traits::Emit>, traits::Error> {
100+
) -> anyhow::Result<Box<dyn traits::Emit>> {
102101
let max_items = opts.max_items() as usize;
103102

104103
// Given a set of items, create a HashMap of the items' names and sizes.
@@ -116,26 +115,23 @@ pub fn diff(
116115
// Given an item name, create a `DiffEntry` object representing the
117116
// change in size, or an error if the name could not be found in
118117
// either of the item collections.
119-
let get_item_delta = |name: String| -> Result<DiffEntry, traits::Error> {
118+
let get_item_delta = |name: String| -> anyhow::Result<DiffEntry> {
120119
let old_size = old_sizes.get::<str>(&name);
121120
let new_size = new_sizes.get::<str>(&name);
122121
let delta: i64 = match (old_size, new_size) {
123122
(Some(old_size), Some(new_size)) => new_size - old_size,
124123
(Some(old_size), None) => -old_size,
125124
(None, Some(new_size)) => *new_size,
126125
(None, None) => {
127-
return Err(traits::Error::with_msg(format!(
128-
"Could not find item with name `{}`",
129-
name
130-
)));
126+
return Err(anyhow!("Could not find item with name `{}`", name));
131127
}
132128
};
133129
Ok(DiffEntry { name, delta })
134130
};
135131

136132
// Given a result returned by `get_item_delta`, return false if the result
137133
// represents an unchanged item. Ignore errors, these are handled separately.
138-
let unchanged_items_filter = |res: &Result<DiffEntry, traits::Error>| -> bool {
134+
let unchanged_items_filter = |res: &anyhow::Result<DiffEntry>| -> bool {
139135
if let Ok(DiffEntry { delta: 0, .. }) = res {
140136
false
141137
} else {
@@ -169,7 +165,7 @@ pub fn diff(
169165
.into_iter()
170166
.map(get_item_delta)
171167
.filter(unchanged_items_filter)
172-
.collect::<Result<Vec<_>, traits::Error>>()?;
168+
.collect::<anyhow::Result<Vec<_>>>()?;
173169
deltas.sort();
174170

175171
// Create an entry to summarize the diff rows that will be truncated.

analyze/analyses/dominators/emit.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::formats::table::{Align, Table};
1515

1616
impl traits::Emit for DominatorTree {
1717
#[cfg(feature = "emit_text")]
18-
fn emit_text(&self, items: &ir::Items, dest: &mut dyn io::Write) -> Result<(), traits::Error> {
18+
fn emit_text(&self, items: &ir::Items, dest: &mut dyn io::Write) -> anyhow::Result<()> {
1919
let mut table = Table::with_header(vec![
2020
(Align::Right, "Retained Bytes".to_string()),
2121
(Align::Right, "Retained %".to_string()),
@@ -93,14 +93,14 @@ impl traits::Emit for DominatorTree {
9393
}
9494

9595
#[cfg(feature = "emit_json")]
96-
fn emit_json(&self, items: &ir::Items, dest: &mut dyn io::Write) -> Result<(), traits::Error> {
96+
fn emit_json(&self, items: &ir::Items, dest: &mut dyn io::Write) -> anyhow::Result<()> {
9797
fn recursive_add_children(
9898
items: &ir::Items,
9999
opts: &opt::Dominators,
100100
dominator_tree: &BTreeMap<ir::Id, Vec<ir::Id>>,
101101
id: ir::Id,
102102
obj: &mut json::Object,
103-
) -> Result<(), traits::Error> {
103+
) -> anyhow::Result<()> {
104104
add_json_item(items, id, obj)?;
105105

106106
if let Some(children) = dominator_tree.get(&id) {
@@ -145,14 +145,14 @@ impl traits::Emit for DominatorTree {
145145
}
146146

147147
#[cfg(feature = "emit_csv")]
148-
fn emit_csv(&self, items: &ir::Items, dest: &mut dyn io::Write) -> Result<(), traits::Error> {
148+
fn emit_csv(&self, items: &ir::Items, dest: &mut dyn io::Write) -> anyhow::Result<()> {
149149
fn recursive_add_children(
150150
items: &ir::Items,
151151
opts: &opt::Dominators,
152152
dominator_tree: &BTreeMap<ir::Id, Vec<ir::Id>>,
153153
id: ir::Id,
154154
wtr: &mut csv::Writer<&mut dyn io::Write>,
155-
) -> Result<(), traits::Error> {
155+
) -> anyhow::Result<()> {
156156
add_csv_item(items, id, wtr)?;
157157
if let Some(children) = dominator_tree.get(&id) {
158158
let mut children = children.to_vec();
@@ -214,11 +214,7 @@ fn add_text_item(items: &ir::Items, depth: u32, id: ir::Id, table: &mut Table) {
214214
}
215215

216216
#[cfg(feature = "emit_json")]
217-
fn add_json_item(
218-
items: &ir::Items,
219-
id: ir::Id,
220-
obj: &mut json::Object,
221-
) -> Result<(), traits::Error> {
217+
fn add_json_item(items: &ir::Items, id: ir::Id, obj: &mut json::Object) -> anyhow::Result<()> {
222218
let item = &items[id];
223219

224220
obj.field("name", item.name())?;
@@ -253,7 +249,7 @@ fn add_csv_item(
253249
items: &ir::Items,
254250
id: ir::Id,
255251
wtr: &mut csv::Writer<&mut dyn io::Write>,
256-
) -> Result<(), traits::Error> {
252+
) -> anyhow::Result<()> {
257253
let item = &items[id];
258254
let (shallow_size, shallow_size_percent) = (
259255
item.size(),

analyze/analyses/dominators/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct UnreachableItemsSummary {
2727
pub fn dominators(
2828
items: &mut ir::Items,
2929
opts: &opt::Dominators,
30-
) -> Result<Box<dyn traits::Emit>, traits::Error> {
30+
) -> anyhow::Result<Box<dyn traits::Emit>> {
3131
items.compute_dominator_tree();
3232
items.compute_dominators();
3333
items.compute_retained_sizes();

0 commit comments

Comments
 (0)