From d6407c0e8aeb16ad566a57e39f4f7f18774a9fcb Mon Sep 17 00:00:00 2001 From: Noah Vesely Date: Thu, 29 Mar 2018 02:53:57 -0600 Subject: [PATCH] Adds `no_more_than_five` formatter This should be useful when validating very large sets of shares. Wouldn't want to print out up to 254 shares. --- src/errors.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index b05605e..1fbe12b 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -3,6 +3,7 @@ #![allow(unknown_lints, missing_docs)] use std::collections::HashSet; +use std::fmt; #[cfg(feature = "dss")] use dss::ss1; @@ -125,7 +126,7 @@ error_chain! { InconsistentSecretLengths(id: u8, slen_: usize, ids: Vec, slen: usize) { description("The shares are incompatible with each other because they do not all have the same secret length.") - display("The share identifier {} had secret length {}, while the secret length {} was found for share identifier(s): {:?}.", id, slen_, slen, ids) + display("The share identifier {} had secret length {}, while the secret length {} was found for share identifier(s): {}.", id, slen_, slen, no_more_than_five(ids)) } InconsistentShares { @@ -135,7 +136,7 @@ error_chain! { InconsistentThresholds(id: u8, k_: u8, ids: Vec, k: u8) { description("The shares are incompatible with each other because they do not all have the same threshold.") - display("The share identifier {} had k = {}, while k = {} was found for share identifier(s): {:?}.", id, k_, k, ids) + display("The share identifier {} had k = {}, while k = {} was found for share identifier(s): {}.", id, k_, k, no_more_than_five(ids)) } } @@ -145,3 +146,19 @@ error_chain! { IntegerParsingError(::std::num::ParseIntError); } } + +/// Takes a `Vec` and formats it like the normal `fmt::Debug` implementation, unless it has more +//than five elements, in which case the rest are replaced by ellipsis. +fn no_more_than_five(vec: &Vec) -> String { + let len = vec.len(); + if len > 5 { + let mut string = String::from("["); + for item in vec.iter().take(5) { + string += &format!("{}, ", item); + } + string.push_str("...]"); + string + } else { + format!("{:?}", vec) + } +}