File tree Expand file tree Collapse file tree 1 file changed +16
-7
lines changed
rust/rbac-registration/src/utils Expand file tree Collapse file tree 1 file changed +16
-7
lines changed Original file line number Diff line number Diff line change 11//! General utility functions
22
3+ use anyhow:: Context ;
4+
35/// Getting the index by decrementing by 1.
46/// e.g. 1 should refers to index 0
57pub ( crate ) fn decremented_index ( int : i16 ) -> anyhow:: Result < usize > {
6- match usize:: try_from ( int) {
7- Ok ( value) => Ok ( value - 1 ) ,
8- Err ( e) => {
9- Err ( anyhow:: Error :: msg ( format ! (
10- "Failed to convert to usize: {e}"
11- ) ) )
12- } ,
8+ int. checked_sub ( 1 )
9+ . and_then ( |v| usize:: try_from ( v) . ok ( ) )
10+ . context ( "Failed to convert '{int}' to usize: {e:?}" )
11+ }
12+
13+ #[ cfg( test) ]
14+ mod tests {
15+ use super :: * ;
16+
17+ #[ test]
18+ fn test_decremented_index ( ) {
19+ assert_eq ! ( 0 , decremented_index( 1 ) . unwrap( ) ) ;
20+ decremented_index ( 0 ) . unwrap_err ( ) ;
21+ decremented_index ( -1 ) . unwrap_err ( ) ;
1322 }
1423}
You can’t perform that action at this time.
0 commit comments