Skip to content

Commit 8bf5aea

Browse files
committed
implement Clone for Spec.
1 parent 8fc114c commit 8bf5aea

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ pub enum SSKRError {
66
#[error("When combining shares, the provided shares contained a duplicate member index")]
77
DuplicateMemberIndex,
88

9+
#[error("Invalid group specification.")]
10+
GroupSpecInvalid,
11+
912
#[error("When creating a split spec, the group count is invalid")]
1013
GroupCountInvalid,
1114

src/spec.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bc_shamir::MAX_SHARE_COUNT;
33
use crate::SSKRError;
44

55
/// A specification for an SSKR split.
6-
#[derive(Debug, PartialEq)]
6+
#[derive(Debug, Clone, PartialEq)]
77
pub struct Spec {
88
group_threshold: usize,
99
groups: Vec<GroupSpec>,
@@ -62,7 +62,7 @@ impl Spec {
6262
}
6363

6464
/// A specification for a group of shares within an SSKR split.
65-
#[derive(Debug, PartialEq)]
65+
#[derive(Debug, Clone, PartialEq)]
6666
pub struct GroupSpec {
6767
member_threshold: usize,
6868
member_count: usize,
@@ -105,4 +105,30 @@ impl GroupSpec {
105105
pub fn member_count(&self) -> usize {
106106
self.member_count
107107
}
108+
109+
/// Parses a group specification from a string.
110+
pub fn parse(s: &str) -> Result<Self, SSKRError> {
111+
let parts: Vec<&str> = s.split('-').collect();
112+
if parts.len() != 3 {
113+
return Err(SSKRError::GroupSpecInvalid);
114+
}
115+
let member_threshold = parts[0].parse::<usize>().map_err(|_| SSKRError::GroupSpecInvalid)?;
116+
if parts[1] != "of" {
117+
return Err(SSKRError::GroupSpecInvalid);
118+
}
119+
let member_count = parts[2].parse::<usize>().map_err(|_| SSKRError::GroupSpecInvalid)?;
120+
Self::new(member_threshold, member_count)
121+
}
122+
}
123+
124+
impl Default for GroupSpec {
125+
fn default() -> Self {
126+
Self::new(1, 1).unwrap()
127+
}
128+
}
129+
130+
impl std::fmt::Display for GroupSpec {
131+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132+
write!(f, "{}-of-{}", self.member_threshold, self.member_count)
133+
}
108134
}

0 commit comments

Comments
 (0)