File tree Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 55use alloc:: vec:: Vec ;
66
77use core:: ptr:: NonNull ;
8+ use core:: str:: FromStr ;
89
910#[ cfg( not( feature = "std" ) ) ]
1011use cstr_core:: CStr ;
@@ -50,6 +51,19 @@ macro_rules! implement_kems {
5051 id as * const _ as * const libc:: c_char
5152 }
5253
54+ impl FromStr for Algorithm {
55+ type Err = crate :: Error ;
56+
57+ fn from_str( s: & str ) -> Result <Self > {
58+ $(
59+ if s == Algorithm :: $kem. name( ) {
60+ return Ok ( Algorithm :: $kem) ;
61+ }
62+ ) *
63+ Err ( crate :: Error :: AlgorithmParsingError )
64+ }
65+ }
66+
5367 $(
5468 #[ cfg( test) ]
5569 #[ allow( non_snake_case) ]
@@ -139,6 +153,14 @@ macro_rules! implement_kems {
139153 assert!( !version. is_empty( ) ) ;
140154 }
141155 }
156+
157+ #[ test]
158+ fn test_from_str( ) {
159+ let algorithm = Algorithm :: $kem;
160+ let name = algorithm. name( ) ;
161+ let parsed = Algorithm :: from_str( name) . unwrap( ) ;
162+ assert_eq!( algorithm, parsed) ;
163+ }
142164 }
143165 ) *
144166 )
Original file line number Diff line number Diff line change @@ -89,6 +89,8 @@ pub enum Error {
8989 ErrorExternalOpenSSL ,
9090 /// Invalid length of a public object
9191 InvalidLength ,
92+ /// Error while trying to parse string to an algorithm
93+ AlgorithmParsingError ,
9294}
9395#[ cfg( feature = "std" ) ]
9496impl std:: error:: Error for Error { }
Original file line number Diff line number Diff line change 55use alloc:: vec:: Vec ;
66
77use core:: ptr:: { null, NonNull } ;
8+ use core:: str:: FromStr ;
89
910#[ cfg( not( feature = "std" ) ) ]
1011use cstr_core:: CStr ;
@@ -52,6 +53,19 @@ macro_rules! implement_sigs {
5253 id as * const _ as * const libc:: c_char
5354 }
5455
56+ impl FromStr for Algorithm {
57+ type Err = crate :: Error ;
58+
59+ fn from_str( s: & str ) -> Result <Self > {
60+ $(
61+ if s == Algorithm :: $sig. name( ) {
62+ return Ok ( Algorithm :: $sig) ;
63+ }
64+ ) *
65+ Err ( crate :: Error :: AlgorithmParsingError )
66+ }
67+ }
68+
5569 $(
5670 #[ cfg( test) ]
5771 #[ allow( non_snake_case) ]
@@ -157,6 +171,13 @@ macro_rules! implement_sigs {
157171 assert!( !version. is_empty( ) ) ;
158172 }
159173 }
174+
175+ #[ test]
176+ fn test_from_str( ) {
177+ let algorithm = Algorithm :: $sig;
178+ let name = algorithm. name( ) ;
179+ let parsed = Algorithm :: from_str( name) . unwrap( ) ;
180+ assert_eq!( algorithm, parsed) ; }
160181 }
161182 ) *
162183 )
You can’t perform that action at this time.
0 commit comments