1
1
//! Helper tools for intra doc links.
2
2
3
- const TYPES : ( [ & str ; 9 ] , [ & str ; 0 ] ) =
4
- ( [ "type" , "struct" , "enum" , "mod" , "trait" , "union" , "module" , "prim" , "primitive" ] , [ ] ) ;
5
- const VALUES : ( [ & str ; 8 ] , [ & str ; 1 ] ) =
6
- ( [ "value" , "function" , "fn" , "method" , "const" , "static" , "mod" , "module" ] , [ "()" ] ) ;
7
- const MACROS : ( [ & str ; 2 ] , [ & str ; 1 ] ) = ( [ "macro" , "derive" ] , [ "!" ] ) ;
3
+ const TYPES : ( & [ & str ] , & [ & str ] ) =
4
+ ( & [ "type" , "struct" , "enum" , "mod" , "trait" , "union" , "module" , "prim" , "primitive" ] , & [ ] ) ;
5
+ const VALUES : ( & [ & str ] , & [ & str ] ) =
6
+ ( & [ "value" , "function" , "fn" , "method" , "const" , "static" , "mod" , "module" ] , & [ "()" ] ) ;
7
+ const MACROS : ( & [ & str ] , & [ & str ] ) = ( & [ "macro" , "derive" ] , & [ "!" ] ) ;
8
8
9
9
/// Extract the specified namespace from an intra-doc-link if one exists.
10
10
///
@@ -17,42 +17,38 @@ pub(super) fn parse_intra_doc_link(s: &str) -> (&str, Option<hir::Namespace>) {
17
17
let s = s. trim_matches ( '`' ) ;
18
18
19
19
[
20
- ( hir:: Namespace :: Types , ( TYPES . 0 . iter ( ) , TYPES . 1 . iter ( ) ) ) ,
21
- ( hir:: Namespace :: Values , ( VALUES . 0 . iter ( ) , VALUES . 1 . iter ( ) ) ) ,
22
- ( hir:: Namespace :: Macros , ( MACROS . 0 . iter ( ) , MACROS . 1 . iter ( ) ) ) ,
20
+ ( hir:: Namespace :: Types , TYPES ) ,
21
+ ( hir:: Namespace :: Values , VALUES ) ,
22
+ ( hir:: Namespace :: Macros , MACROS ) ,
23
23
]
24
24
. into_iter ( )
25
- . find_map ( |( ns, ( mut prefixes, mut suffixes) ) | {
26
- if let Some ( prefix) = prefixes. find ( |& & prefix| {
25
+ . find_map ( |( ns, ( prefixes, suffixes) ) | {
26
+ if let Some ( prefix) = prefixes. iter ( ) . find ( |& & prefix| {
27
27
s. starts_with ( prefix)
28
28
&& s. chars ( ) . nth ( prefix. len ( ) ) . map_or ( false , |c| c == '@' || c == ' ' )
29
29
} ) {
30
30
Some ( ( & s[ prefix. len ( ) + 1 ..] , ns) )
31
31
} else {
32
- suffixes. find_map ( |& suffix| s. strip_suffix ( suffix) . zip ( Some ( ns) ) )
32
+ suffixes. iter ( ) . find_map ( |& suffix| s. strip_suffix ( suffix) . zip ( Some ( ns) ) )
33
33
}
34
34
} )
35
35
. map_or ( ( s, None ) , |( s, ns) | ( s, Some ( ns) ) )
36
36
}
37
37
38
38
pub ( super ) fn strip_prefixes_suffixes ( s : & str ) -> & str {
39
- [
40
- ( TYPES . 0 . iter ( ) , TYPES . 1 . iter ( ) ) ,
41
- ( VALUES . 0 . iter ( ) , VALUES . 1 . iter ( ) ) ,
42
- ( MACROS . 0 . iter ( ) , MACROS . 1 . iter ( ) ) ,
43
- ]
44
- . into_iter ( )
45
- . find_map ( |( mut prefixes, mut suffixes) | {
46
- if let Some ( prefix) = prefixes. find ( |& & prefix| {
47
- s. starts_with ( prefix)
48
- && s. chars ( ) . nth ( prefix. len ( ) ) . map_or ( false , |c| c == '@' || c == ' ' )
49
- } ) {
50
- Some ( & s[ prefix. len ( ) + 1 ..] )
51
- } else {
52
- suffixes. find_map ( |& suffix| s. strip_suffix ( suffix) )
53
- }
54
- } )
55
- . unwrap_or ( s)
39
+ [ TYPES , VALUES , MACROS ]
40
+ . into_iter ( )
41
+ . find_map ( |( prefixes, suffixes) | {
42
+ if let Some ( prefix) = prefixes. iter ( ) . find ( |& & prefix| {
43
+ s. starts_with ( prefix)
44
+ && s. chars ( ) . nth ( prefix. len ( ) ) . map_or ( false , |c| c == '@' || c == ' ' )
45
+ } ) {
46
+ Some ( & s[ prefix. len ( ) + 1 ..] )
47
+ } else {
48
+ suffixes. iter ( ) . find_map ( |& suffix| s. strip_suffix ( suffix) )
49
+ }
50
+ } )
51
+ . unwrap_or ( s)
56
52
}
57
53
58
54
#[ cfg( test) ]
0 commit comments