Files
RustySecrets/src/dss/ss1/serialize.rs
Noah Vesely c25f661645 Rustfmt updates + refactor Travis configuration (#60)
* Update rustfmt compliance

Looks like rustfmt has made some improvements recently, so wanted to bring the
code up to date.

* Add rustfmt to nightly item in Travis matrix

* Use Travis Cargo cache

* Allow fast_finish in Travis

Items that match the `allow_failures` predicate (right now, just Rust nightly),
will still finish, but Travis won't wait for them to report a result if the
other builds have already finished.

* Run kcov in a separate matrix build in Travis

* Rework allowed_failures logic

We don't want rustfmt to match `allow_failures` just because it needs to use
nightly, while we do want nightly to match `allow_failures`. Env vars provide a
solution.

* Add --all switch to rustfmt Travis

* Test building docs in Travis

* Use exact Ubuntu dependencies listed for kcov

Some of the dependencies we were installing were not listed on
https://github.com/SimonKagstrom/kcov/blob/master/INSTALL.md, and we were
missing one dependency that was listed there. When `sudo: true` Travis uses
Ubuntu Trusty.

* No need to build before running kcov

kcov builds its own test executables.

* Generate `Cargo.lock` w/ `cargo update` before running kcov

As noted in aeb3906cce8e3e26c7bc80d6aec417b365f3d2f1 it is not necessary to
build the project before running kcov, but kcov does require a `Cargo.lock`
file, which can be generated with `cargo update`.
2018-08-13 21:29:39 +02:00

81 lines
2.1 KiB
Rust

use super::{MetaData, Share};
use dss::format::{format_share_protobuf, parse_share_protobuf};
use dss::utils::{btreemap_to_hashmap, hashmap_to_btreemap};
use errors::*;
use proto::dss::{MetaDataProto, ShareProto};
pub(crate) fn share_to_string(share: Share) -> String {
let proto = share_to_protobuf(share);
format_share_protobuf(&proto)
}
pub(crate) fn share_from_string(raw: &str) -> Result<Share> {
let mut proto = parse_share_protobuf(raw)?;
let metadata_proto = if proto.has_meta_data() {
Some(metadata_from_proto(proto.take_meta_data()))
} else {
None
};
let i = proto.get_id() as u8;
let k = proto.get_threshold() as u8;
let n = proto.get_shares_count() as u8;
if k < 1 || i < 1 {
bail! {
ErrorKind::ShareParsingError(
format!("Found illegal share info: threshold = {}, identifier = {}.", k, i),
)
}
}
if n < 1 || k > n || i > n {
bail! {
ErrorKind::ShareParsingError(
format!("Found illegal share info: shares_count = {}, threshold = {}, identifier = {}.", n, k, i),
)
}
}
let share = Share {
id: i,
threshold: k,
shares_count: n,
data: proto.take_data(),
hash: proto.take_hash(),
metadata: metadata_proto,
};
Ok(share)
}
pub(crate) fn share_to_protobuf(share: Share) -> ShareProto {
let mut proto = ShareProto::new();
proto.set_id(share.id.into());
proto.set_threshold(share.threshold.into());
proto.set_shares_count(share.shares_count.into());
proto.set_data(share.data);
proto.set_hash(share.hash);
if let Some(meta_data) = share.metadata {
let metadata_proto = metadata_to_proto(meta_data);
proto.set_meta_data(metadata_proto);
}
proto
}
fn metadata_to_proto(meta_data: MetaData) -> MetaDataProto {
let mut proto = MetaDataProto::new();
proto.set_tags(btreemap_to_hashmap(meta_data.tags));
proto
}
fn metadata_from_proto(mut proto: MetaDataProto) -> MetaData {
MetaData {
tags: hashmap_to_btreemap(proto.take_tags()),
}
}