1
+ use std:: borrow:: Cow ;
2
+ use std:: collections:: HashMap ;
1
3
use std:: fs;
2
4
use std:: path:: Path ;
3
5
4
6
use const_format:: concatcp;
5
7
use pretty_yaml:: config:: { FormatOptions , LanguageOptions } ;
6
8
use rari_doc:: html:: sidebar:: { BasicEntry , Sidebar , SidebarEntry , SubPageEntry , WebExtApiEntry } ;
7
9
use rari_types:: globals:: content_root;
8
- use rari_types:: locale:: default_locale;
10
+ use rari_types:: locale:: { default_locale, Locale } ;
9
11
use rari_utils:: concat_strs;
10
12
11
13
use crate :: error:: ToolError ;
14
+ use crate :: redirects:: { read_redirects_raw, redirects_path} ;
12
15
13
16
const PREFIX : & str = "# Do not add comments to this file. They will be lost.\n \n " ;
14
- static SIDEBAR_PATH_PREFIX : & str = concatcp ! ( "/" , default_locale( ) . as_url_str( ) , "/docs" ) ;
17
+ static EN_US_DOCS_PREFIX : & str = concatcp ! ( "/" , default_locale( ) . as_url_str( ) , "/docs" ) ;
18
+
19
+ type Pair < ' a > = ( Cow < ' a , str > , Option < Cow < ' a , str > > ) ;
20
+ type Pairs < ' a > = & ' a [ Pair < ' a > ] ;
21
+
22
+ pub fn sync_sidebars ( ) -> Result < ( ) , ToolError > {
23
+ let mut redirects = HashMap :: new ( ) ;
24
+ let path = redirects_path ( Locale :: default ( ) ) ?;
25
+ read_redirects_raw ( & path, & mut redirects) ?;
26
+ let pairs = redirects
27
+ . iter ( )
28
+ . map ( |( from, to) | {
29
+ (
30
+ from. strip_prefix ( EN_US_DOCS_PREFIX )
31
+ . map ( Cow :: Borrowed )
32
+ . unwrap_or ( Cow :: Borrowed ( from) ) ,
33
+ Some (
34
+ to. strip_prefix ( EN_US_DOCS_PREFIX )
35
+ . map ( Cow :: Borrowed )
36
+ . unwrap_or ( Cow :: Borrowed ( to) ) ,
37
+ ) ,
38
+ )
39
+ } )
40
+ . collect :: < Vec < _ > > ( ) ;
41
+ update_sidebars ( & pairs) ?;
42
+ Ok ( ( ) )
43
+ }
15
44
16
45
pub fn fmt_sidebars ( ) -> Result < ( ) , ToolError > {
17
46
for ( path, sidebar) in read_sidebars ( ) ? {
@@ -20,30 +49,28 @@ pub fn fmt_sidebars() -> Result<(), ToolError> {
20
49
Ok ( ( ) )
21
50
}
22
51
23
- pub ( crate ) fn update_sidebars ( pairs : & [ ( String , Option < String > ) ] ) -> Result < ( ) , ToolError > {
52
+ pub ( crate ) fn update_sidebars ( pairs : Pairs < ' _ > ) -> Result < ( ) , ToolError > {
24
53
let sidebars = read_sidebars ( ) ?;
25
54
26
55
// add leading slash to pairs, because that is what the sidebars use
27
56
let pairs = & pairs
28
57
. iter ( )
29
58
. map ( |( from, to) | {
30
59
let from = if from. starts_with ( '/' ) {
31
- from. to_string ( )
60
+ Cow :: Borrowed ( from. as_ref ( ) )
32
61
} else {
33
- concat_strs ! ( "/" , from)
62
+ Cow :: Owned ( concat_strs ! ( "/" , from) )
34
63
} ;
35
- let to = if let Some ( to ) = to {
64
+ let to = to . as_ref ( ) . map ( |to| {
36
65
if to. starts_with ( '/' ) {
37
- Some ( to. to_string ( ) )
66
+ Cow :: Borrowed ( to. as_ref ( ) )
38
67
} else {
39
- Some ( concat_strs ! ( "/" , to) )
68
+ Cow :: Owned ( concat_strs ! ( "/" , to) )
40
69
}
41
- } else {
42
- None
43
- } ;
70
+ } ) ;
44
71
( from, to)
45
72
} )
46
- . collect :: < Vec < ( String , Option < String > ) > > ( ) ;
73
+ . collect :: < Vec < Pair < ' _ > > > ( ) ;
47
74
48
75
// Walk the sidebars and potentially replace the links.
49
76
// `process_entry`` is called recursively to process all children
@@ -116,11 +143,11 @@ fn read_sidebars() -> Result<Vec<(std::path::PathBuf, Sidebar)>, ToolError> {
116
143
. collect ( )
117
144
}
118
145
119
- fn replace_pairs ( link : Option < String > , pairs : & [ ( String , Option < String > ) ] ) -> Option < String > {
146
+ fn replace_pairs ( link : Option < String > , pairs : Pairs < ' _ > ) -> Option < String > {
120
147
match link {
121
148
Some ( link) => {
122
149
let mut has_prefix = false ;
123
- let link = if let Some ( l) = link. strip_prefix ( SIDEBAR_PATH_PREFIX ) {
150
+ let link = if let Some ( l) = link. strip_prefix ( EN_US_DOCS_PREFIX ) {
124
151
has_prefix = true ;
125
152
l. to_string ( )
126
153
} else {
@@ -130,9 +157,9 @@ fn replace_pairs(link: Option<String>, pairs: &[(String, Option<String>)]) -> Op
130
157
if link == * from {
131
158
if let Some ( to) = to {
132
159
if has_prefix {
133
- return Some ( concat_strs ! ( SIDEBAR_PATH_PREFIX , to) ) ;
160
+ return Some ( concat_strs ! ( EN_US_DOCS_PREFIX , to) ) ;
134
161
} else {
135
- return Some ( to. clone ( ) ) ;
162
+ return Some ( to. to_string ( ) ) ;
136
163
}
137
164
} else {
138
165
return None ;
@@ -145,7 +172,7 @@ fn replace_pairs(link: Option<String>, pairs: &[(String, Option<String>)]) -> Op
145
172
}
146
173
}
147
174
148
- fn process_entry ( entry : SidebarEntry , pairs : & [ ( String , Option < String > ) ] ) -> SidebarEntry {
175
+ fn process_entry ( entry : SidebarEntry , pairs : Pairs < ' _ > ) -> SidebarEntry {
149
176
match entry {
150
177
SidebarEntry :: Section ( BasicEntry {
151
178
link,
@@ -322,22 +349,22 @@ mod test {
322
349
let _sidebars = SidebarFixtures :: new ( vec ! [ sb] ) ;
323
350
let pairs = vec ! [
324
351
(
325
- "Web/CSS/CSS_Box_Alignment/Box_Alignment_In_Block_Abspos_Tables" . to_string ( ) ,
326
- Some ( "Web/CSS/CSS_Box_Alignment/Something_New" . to_string ( ) ) ,
352
+ Cow :: Borrowed ( "Web/CSS/CSS_Box_Alignment/Box_Alignment_In_Block_Abspos_Tables" ) ,
353
+ Some ( Cow :: Borrowed ( "Web/CSS/CSS_Box_Alignment/Something_New" ) ) ,
327
354
) ,
328
355
(
329
- "Web/CSS/CSS_Box_Alignment/Box_Alignment_In_Grid_Layout" . to_string ( ) ,
330
- Some ( "Web/CSS/CSS_Box_Alignment/Also_New" . to_string ( ) ) ,
356
+ Cow :: Borrowed ( "Web/CSS/CSS_Box_Alignment/Box_Alignment_In_Grid_Layout" ) ,
357
+ Some ( Cow :: Borrowed ( "Web/CSS/CSS_Box_Alignment/Also_New" ) ) ,
331
358
) ,
332
359
(
333
- "Web/HTTP/Headers" . to_string ( ) ,
334
- Some ( "Web/HTTP/Headers_New" . to_string ( ) ) ,
360
+ Cow :: Borrowed ( "Web/HTTP/Headers" ) ,
361
+ Some ( Cow :: Borrowed ( "Web/HTTP/Headers_New" ) ) ,
335
362
) ,
336
363
(
337
- "/Web/CSS/CSS_Box_Alignment/Box_Alignment_in_Multi-column_Layout" . to_string ( ) ,
364
+ Cow :: Borrowed ( "/Web/CSS/CSS_Box_Alignment/Box_Alignment_in_Multi-column_Layout" ) ,
338
365
None ,
339
366
) ,
340
- ( "/Web/CSS/CSS_Box_Alignment" . to_string ( ) , None ) ,
367
+ ( Cow :: Borrowed ( "/Web/CSS/CSS_Box_Alignment" ) , None ) ,
341
368
] ;
342
369
let res = update_sidebars ( & pairs) ;
343
370
assert ! ( res. is_ok( ) ) ;
0 commit comments