2
2
3
3
use crate :: options;
4
4
5
- // parse_style parses a style string into a NumberingStyle.
6
- fn parse_style ( chars : & [ char ] ) -> Result < crate :: NumberingStyle , String > {
7
- if chars. len ( ) == 1 && chars[ 0 ] == 'a' {
8
- Ok ( crate :: NumberingStyle :: All )
9
- } else if chars. len ( ) == 1 && chars[ 0 ] == 't' {
10
- Ok ( crate :: NumberingStyle :: NonEmpty )
11
- } else if chars. len ( ) == 1 && chars[ 0 ] == 'n' {
12
- Ok ( crate :: NumberingStyle :: None )
13
- } else if chars. len ( ) > 1 && chars[ 0 ] == 'p' {
14
- let s: String = chars[ 1 ..] . iter ( ) . cloned ( ) . collect ( ) ;
15
- match regex:: Regex :: new ( & s) {
16
- Ok ( re) => Ok ( crate :: NumberingStyle :: Regex ( Box :: new ( re) ) ) ,
17
- Err ( _) => Err ( String :: from ( "Illegal regular expression" ) ) ,
18
- }
19
- } else {
20
- Err ( String :: from ( "Illegal style encountered" ) )
21
- }
22
- }
23
-
24
5
// parse_options loads the options into the settings, returning an array of
25
6
// error messages.
26
7
#[ allow( clippy:: cognitive_complexity) ]
@@ -35,47 +16,32 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &clap::ArgMatches) ->
35
16
. get_one :: < String > ( options:: NUMBER_FORMAT )
36
17
. map ( Into :: into)
37
18
. unwrap_or_default ( ) ;
38
- match opts. get_one :: < String > ( options:: BODY_NUMBERING ) {
19
+ match opts
20
+ . get_one :: < String > ( options:: HEADER_NUMBERING )
21
+ . map ( String :: as_str)
22
+ . map ( TryInto :: try_into)
23
+ {
39
24
None => { }
40
- Some ( val) => {
41
- let chars: Vec < char > = val. chars ( ) . collect ( ) ;
42
- match parse_style ( & chars) {
43
- Ok ( s) => {
44
- settings. body_numbering = s;
45
- }
46
- Err ( message) => {
47
- errs. push ( message) ;
48
- }
49
- }
50
- }
25
+ Some ( Ok ( style) ) => settings. header_numbering = style,
26
+ Some ( Err ( message) ) => errs. push ( message. to_string ( ) ) ,
51
27
}
52
- match opts. get_one :: < String > ( options:: FOOTER_NUMBERING ) {
28
+ match opts
29
+ . get_one :: < String > ( options:: BODY_NUMBERING )
30
+ . map ( String :: as_str)
31
+ . map ( TryInto :: try_into)
32
+ {
53
33
None => { }
54
- Some ( val) => {
55
- let chars: Vec < char > = val. chars ( ) . collect ( ) ;
56
- match parse_style ( & chars) {
57
- Ok ( s) => {
58
- settings. footer_numbering = s;
59
- }
60
- Err ( message) => {
61
- errs. push ( message) ;
62
- }
63
- }
64
- }
34
+ Some ( Ok ( style) ) => settings. body_numbering = style,
35
+ Some ( Err ( message) ) => errs. push ( message. to_string ( ) ) ,
65
36
}
66
- match opts. get_one :: < String > ( options:: HEADER_NUMBERING ) {
37
+ match opts
38
+ . get_one :: < String > ( options:: FOOTER_NUMBERING )
39
+ . map ( String :: as_str)
40
+ . map ( TryInto :: try_into)
41
+ {
67
42
None => { }
68
- Some ( val) => {
69
- let chars: Vec < char > = val. chars ( ) . collect ( ) ;
70
- match parse_style ( & chars) {
71
- Ok ( s) => {
72
- settings. header_numbering = s;
73
- }
74
- Err ( message) => {
75
- errs. push ( message) ;
76
- }
77
- }
78
- }
43
+ Some ( Ok ( style) ) => settings. footer_numbering = style,
44
+ Some ( Err ( message) ) => errs. push ( message. to_string ( ) ) ,
79
45
}
80
46
match opts. get_one :: < usize > ( options:: NUMBER_WIDTH ) {
81
47
None => { }
0 commit comments