Skip to content

Commit b4027e0

Browse files
committed
cleanup
1 parent 9906467 commit b4027e0

File tree

5 files changed

+65
-79
lines changed

5 files changed

+65
-79
lines changed

src/bin/iggy.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,20 @@ fn run() -> Result<()> {
182182
println!("scenfit: {}\n", scenfit);
183183
}
184184
if opt.mics {
185-
let mics = get_minimal_inconsistent_cores(&graph, &profile, &new_inputs, &setting)
186-
.unwrap();
185+
let mics = get_minimal_inconsistent_cores(&graph, &profile, &new_inputs, &setting)?;
187186
if opt.json {
188-
print_json_mics(mics);
187+
print_json_mics(mics)?;
189188
} else {
190-
print_mics(mics);
189+
print_mics(mics)?;
191190
}
192191
}
193192
}
194193
if let Some(max_labelings) = opt.max_labelings {
195-
let l = get_scenfit_labelings(&graph, &profile, &new_inputs, max_labelings, &setting)
196-
.unwrap();
194+
let l = get_scenfit_labelings(&graph, &profile, &new_inputs, max_labelings, &setting)?;
197195
if opt.json {
198-
print_json_labelings(l);
196+
print_json_labelings(l)?;
199197
} else {
200-
print_labelings(l);
198+
print_labelings(l)?;
201199
}
202200
}
203201
if opt.show_predictions {
@@ -206,7 +204,7 @@ fn run() -> Result<()> {
206204
get_predictions_under_scenfit(&graph, &profile, &new_inputs, &setting)?;
207205

208206
if opt.json {
209-
let serialized = serde_json::to_string(&predictions).unwrap();
207+
let serialized = serde_json::to_string(&predictions)?;
210208
println!(",\"Predictions\":{}", serialized);
211209
} else {
212210
print_predictions(&predictions);
@@ -230,29 +228,27 @@ fn run() -> Result<()> {
230228
println!("mcos: {}\n", mcos);
231229
}
232230
if opt.mics {
233-
let mics = get_minimal_inconsistent_cores(&graph, &profile, &new_inputs, &setting)
234-
.unwrap();
231+
let mics = get_minimal_inconsistent_cores(&graph, &profile, &new_inputs, &setting)?;
235232
if opt.json {
236-
print_json_mics(mics);
233+
print_json_mics(mics)?;
237234
} else {
238-
print_mics(mics);
235+
print_mics(mics)?;
239236
}
240237
}
241238
}
242239
if let Some(max_labelings) = opt.max_labelings {
243-
let l =
244-
get_mcos_labelings(&graph, &profile, &new_inputs, max_labelings, &setting).unwrap();
240+
let l = get_mcos_labelings(&graph, &profile, &new_inputs, max_labelings, &setting)?;
245241
if opt.json {
246-
print_json_labelings(l);
242+
print_json_labelings(l)?;
247243
} else {
248-
print_labelings(l);
244+
print_labelings(l)?;
249245
}
250246
}
251247
if opt.show_predictions {
252248
info!("Compute predictions ...");
253249
let predictions = get_predictions_under_mcos(&graph, &profile, &new_inputs, &setting)?;
254250
if opt.json {
255-
let serialized = serde_json::to_string(&predictions).unwrap();
251+
let serialized = serde_json::to_string(&predictions)?;
256252
println!(",\"Predictions\":{}", serialized);
257253
} else {
258254
print_predictions(&predictions);
@@ -386,44 +382,46 @@ fn observations_statistics(profile: &Profile, graph: &Graph) -> ObservationsStat
386382
}
387383
}
388384

389-
fn print_mics(mut mics: Mics) {
385+
fn print_mics(mut mics: Mics) -> Result<()> {
390386
let mut oldmic = vec![];
391-
for (count, mic) in mics.iter().unwrap().enumerate() {
387+
for (count, mic) in mics.iter()?.enumerate() {
392388
if oldmic != *mic {
393389
print!("- mic {}:\n ", count + 1);
394390
for e in mic.clone() {
395-
let node = into_node_id(e).unwrap();
391+
let node = into_node_id(e)?;
396392
print!("{} ", node);
397393
}
398394
println!();
399395
oldmic = mic;
400396
}
401397
}
398+
Ok(())
402399
}
403-
fn print_json_mics(mut mics: Mics) {
400+
fn print_json_mics(mut mics: Mics) -> Result<()> {
404401
println!(",\"mics\":[");
405402

406-
let mut iter = mics.iter().unwrap();
403+
let mut iter = mics.iter()?;
407404
if let Some(mic) = iter.next() {
408405
let nodes: Vec<NodeId> = mic.iter().map(|y| into_node_id(*y).unwrap()).collect();
409-
let serialized = serde_json::to_string(&nodes).unwrap();
406+
let serialized = serde_json::to_string(&nodes)?;
410407
println!("{}", serialized);
411408
let mut oldmic = mic;
412409

413410
for mic in iter {
414411
if oldmic != mic {
415412
let nodes: Vec<NodeId> = mic.iter().map(|y| into_node_id(*y).unwrap()).collect();
416-
let serialized = serde_json::to_string(&nodes).unwrap();
413+
let serialized = serde_json::to_string(&nodes)?;
417414
println!(", {}", serialized);
418415
oldmic = mic;
419416
}
420417
}
421418
}
422419
println!("]");
420+
Ok(())
423421
}
424422

425-
fn print_labelings(mut labelings: LabelsRepair) {
426-
for (count, (labels, repairs)) in labelings.iter().unwrap().enumerate() {
423+
fn print_labelings(mut labelings: LabelsRepair) -> Result<()> {
424+
for (count, (labels, repairs)) in labelings.iter()?.enumerate() {
427425
if count > 0 {
428426
println!();
429427
}
@@ -435,29 +433,31 @@ fn print_labelings(mut labelings: LabelsRepair) {
435433
println!(" - {}", fix);
436434
}
437435
}
436+
Ok(())
438437
}
439-
fn print_json_labelings(mut labelings: LabelsRepair) {
438+
fn print_json_labelings(mut labelings: LabelsRepair) -> Result<()> {
440439
println!(",\"labels under repair\":[");
441440

442-
let mut iter = labelings.iter().unwrap();
441+
let mut iter = labelings.iter()?;
443442
if let Some((labels, repairs)) = iter.next() {
444-
let serialized = serde_json::to_string(&labels).unwrap();
443+
let serialized = serde_json::to_string(&labels)?;
445444
println!("{{\"labels\":{}", serialized);
446445

447-
let serialized = serde_json::to_string(&repairs).unwrap();
446+
let serialized = serde_json::to_string(&repairs)?;
448447
println!(",\"repairs\":{}", serialized);
449448
println!("}}");
450449

451450
for (labels, repairs) in iter {
452-
let serialized = serde_json::to_string(&labels).unwrap();
451+
let serialized = serde_json::to_string(&labels)?;
453452
println!(", {{\"labels\":{}", serialized);
454453

455-
let serialized = serde_json::to_string(&repairs).unwrap();
454+
let serialized = serde_json::to_string(&repairs)?;
456455
println!(",\"repairs\":{}", serialized);
457456
println!("}}");
458457
}
459458
}
460459
println!("]");
460+
Ok(())
461461
}
462462

463463
fn print_labels(labels: &[Prediction]) {

src/bin/optgraph.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ fn run() -> Result<()> {
216216
println!(",\"scenfit\":{}", scenfit);
217217
println!(",\"repair score\":{}", repair_score);
218218
} else {
219-
println!("\nThe network and data can reach a scenfit of {}.", scenfit);
219+
println!("The network and data can reach a scenfit of {}.", scenfit);
220220
}
221221
(scenfit, repair_score, redges)
222222
// with {} removals and {} additions.", repairs, edges.len());
@@ -230,7 +230,7 @@ fn run() -> Result<()> {
230230
println!(",\"repair score\":{}", repair_score);
231231
} else {
232232
println!(
233-
"\nThe network and data can reach a scenfit of {} with repairs of score {}",
233+
"The network and data can reach a scenfit of {} with repairs of score {}",
234234
scenfit, repair_score
235235
);
236236
}
@@ -245,7 +245,7 @@ fn run() -> Result<()> {
245245
println!(",\"repair score\":{}", repair_score);
246246
} else {
247247
println!(
248-
"\nThe network and data can reach a scenfit of {} with {} flipped edges",
248+
"The network and data can reach a scenfit of {} with {} flipped edges",
249249
scenfit, repair_score
250250
);
251251
}
@@ -260,7 +260,7 @@ fn run() -> Result<()> {
260260
println!(",\"repair score\":{}", repair_score);
261261
} else {
262262
println!(
263-
"\nThe network and data can reach a scenfit of {} with {} removed edges.",
263+
"The network and data can reach a scenfit of {} with {} removed edges.",
264264
scenfit, repair_score
265265
);
266266
}
@@ -331,7 +331,7 @@ fn run() -> Result<()> {
331331
})
332332
.collect();
333333

334-
let serialized = serde_json::to_string(&repairs).unwrap();
334+
let serialized = serde_json::to_string(&repairs)?;
335335
println!(",\"Repair sets\":{}", serialized);
336336
} else {
337337
for (count, r) in repairs.iter().enumerate() {

src/cif_parser.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ pub fn read(file: &File) -> Result<Graph> {
1313
let l1 = line?;
1414
let l = l1.trim();
1515
if !l.is_empty() {
16-
match cif::statement(&l) {
17-
Ok(r) => {
18-
graph.add(r);
19-
}
20-
Err(e) => Err(e)?,
21-
}
16+
graph.add(cif::statement(&l)?);
2217
}
2318
}
2419
graph.or_nodes.sort();
@@ -144,7 +139,7 @@ impl Graph {
144139
neg.push(s);
145140
}
146141
Expression::Plain(s) => {
147-
inner.push(format!("{}", s));
142+
inner.push(s.to_string());
148143
pos.push(s);
149144
}
150145
Expression::Unknown(s) => {
@@ -244,7 +239,7 @@ peg::parser! { grammar cif() for str {
244239
pub rule statement() -> Statement
245240
= whitespace()* s:exprlist() whitespace()+ "->" whitespace()+ t:ident() {
246241
if s.len() == 1 {
247-
let expr = s.clone().pop().unwrap();
242+
let expr = s[0].clone();
248243
Statement{ start : SNode::Single(expr) ,target : t.to_string() }
249244
}
250245
else {

src/lib.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,13 @@ fn cautious_consequences_optimal_models(handle: &mut SolveHandle) -> Result<Vec<
426426
let mut symbols = vec![];
427427
loop {
428428
handle.resume()?;
429-
match handle.model() {
430-
Ok(Some(model)) => {
429+
match handle.model()? {
430+
Some(model) => {
431431
if model.optimality_proven()? {
432432
symbols = model.symbols(ShowType::SHOWN)?;
433433
}
434434
}
435-
Ok(None) => break,
436-
Err(e) => return Err(e.into()),
435+
None => break,
437436
}
438437
}
439438
Ok(symbols)
@@ -444,25 +443,22 @@ fn get_optimum(handle: &mut SolveHandle) -> Result<Vec<i64>> {
444443
let mut found = false;
445444
loop {
446445
handle.resume()?;
447-
match handle.model() {
448-
Ok(Some(model)) => {
446+
match handle.model()? {
447+
Some(model) => {
449448
if model.optimality_proven()? {
450449
return Ok(model.cost()?);
451450
} else {
452451
found = true;
453452
last = model.cost()?;
454453
}
455454
}
456-
Ok(None) => {
455+
None => {
457456
if found {
458457
return Ok(last);
459458
} else {
460459
panic!("Error: no optimal model found!");
461460
}
462461
}
463-
Err(e) => {
464-
return Err(e.into());
465-
}
466462
}
467463
}
468464
}
@@ -1091,9 +1087,7 @@ pub fn get_opt_add_remove_edges_greedy(
10911087
let mut fedges: Vec<(FactBase, i64, i64)> = vec![(FactBase::new(), bscenfit, brepscore)];
10921088
let mut tedges = vec![];
10931089

1094-
while !fedges.is_empty() {
1095-
let (oedges, oscenfit, orepscore) = fedges.pop().unwrap();
1096-
1090+
while let Some((oedges, oscenfit, orepscore)) = fedges.pop() {
10971091
if oscenfit == 0 && oedges.len() * 2 >= (orepscore - 1) as usize {
10981092
// early return
10991093
let tuple = (oedges, oscenfit, orepscore);
@@ -1134,8 +1128,8 @@ pub fn get_opt_add_remove_edges_greedy(
11341128
// seach best edge end loop
11351129
loop {
11361130
handle.resume()?;
1137-
match handle.model() {
1138-
Ok(Some(model)) => {
1131+
match handle.model()? {
1132+
Some(model) => {
11391133
if model.optimality_proven()? {
11401134
let symbols = model.symbols(ShowType::SHOWN)?;
11411135
let cost = model.cost()?;
@@ -1145,7 +1139,7 @@ pub fn get_opt_add_remove_edges_greedy(
11451139

11461140
if nscenfit < oscenfit || nrepscore < orepscore {
11471141
// better score or more that 1 scenfit
1148-
let nend = extract_addeddy(&symbols).unwrap();
1142+
let nend = extract_addeddy(&symbols)?;
11491143

11501144
let mut f_end = FactBase::new();
11511145
f_end.insert(&nend);
@@ -1178,8 +1172,8 @@ pub fn get_opt_add_remove_edges_greedy(
11781172
// seach best edge start loop
11791173
loop {
11801174
handle2.resume()?;
1181-
match handle2.model() {
1182-
Ok(Some(model)) => {
1175+
match handle2.model()? {
1176+
Some(model) => {
11831177
if model.optimality_proven()? {
11841178
let symbols2 = model.symbols(ShowType::SHOWN)?;
11851179
let n2scenfit = model.cost()?[0];
@@ -1195,7 +1189,7 @@ pub fn get_opt_add_remove_edges_greedy(
11951189
brepscore = n2repscore;
11961190
}
11971191

1198-
let nedges = extract_addedges(&symbols2).unwrap();
1192+
let nedges = extract_addedges(&symbols2)?;
11991193

12001194
let tuple = (nedges.clone(), n2scenfit, n2repscore);
12011195
if !fedges.contains(&tuple) {
@@ -1205,8 +1199,7 @@ pub fn get_opt_add_remove_edges_greedy(
12051199
}
12061200
}
12071201
}
1208-
Ok(None) => break,
1209-
Err(e) => return Err(e.into()),
1202+
None => break,
12101203
}
12111204
}
12121205
}
@@ -1222,8 +1215,7 @@ pub fn get_opt_add_remove_edges_greedy(
12221215
}
12231216
}
12241217
}
1225-
Ok(None) => break,
1226-
Err(e) => return Err(e.into()),
1218+
None => break,
12271219
}
12281220
}
12291221
}

0 commit comments

Comments
 (0)