@@ -3,7 +3,7 @@ use bc_shamir::MAX_SHARE_COUNT;
3
3
use crate :: SSKRError ;
4
4
5
5
/// A specification for an SSKR split.
6
- #[ derive( Debug , PartialEq ) ]
6
+ #[ derive( Debug , Clone , PartialEq ) ]
7
7
pub struct Spec {
8
8
group_threshold : usize ,
9
9
groups : Vec < GroupSpec > ,
@@ -62,7 +62,7 @@ impl Spec {
62
62
}
63
63
64
64
/// A specification for a group of shares within an SSKR split.
65
- #[ derive( Debug , PartialEq ) ]
65
+ #[ derive( Debug , Clone , PartialEq ) ]
66
66
pub struct GroupSpec {
67
67
member_threshold : usize ,
68
68
member_count : usize ,
@@ -105,4 +105,30 @@ impl GroupSpec {
105
105
pub fn member_count ( & self ) -> usize {
106
106
self . member_count
107
107
}
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
+ }
108
134
}
0 commit comments