@@ -58,47 +58,9 @@ use crate::html::static_files::{self, suffix_path};
58
58
use crate :: visit:: DocVisitor ;
59
59
use crate :: { try_err, try_none} ;
60
60
61
- // TODO
62
- // sort template have diff thing
63
- // weird behavior; extract crate list from search index? or somewhere similar
64
- // string faster loading tetchnique
65
- // run rest of the tests and fix things up
66
-
67
- #[ derive( Serialize , Deserialize , Clone , Debug ) ]
68
- struct CrateInfo {
69
- src_files_js : PartsAndLocations < SourcesPart > ,
70
- search_index_js : PartsAndLocations < SearchIndexPart > ,
71
- all_crates : PartsAndLocations < AllCratesPart > ,
72
- crates_index : PartsAndLocations < CratesIndexPart > ,
73
- trait_impl : PartsAndLocations < TraitAliasPart > ,
74
- type_impl : PartsAndLocations < TypeAliasPart > ,
75
- }
76
-
77
- impl CrateInfo {
78
- /// Gets a reference to the cross-crate information parts for `T`
79
- fn get < T : ' static > ( & self ) -> Option < & PartsAndLocations < T > > {
80
- ( & self . src_files_js as & dyn Any ) . downcast_ref ( )
81
- . or_else ( || ( & self . search_index_js as & dyn Any ) . downcast_ref ( ) )
82
- . or_else ( || ( & self . all_crates as & dyn Any ) . downcast_ref ( ) )
83
- . or_else ( || ( & self . crates_index as & dyn Any ) . downcast_ref ( ) )
84
- . or_else ( || ( & self . trait_impl as & dyn Any ) . downcast_ref ( ) )
85
- . or_else ( || ( & self . type_impl as & dyn Any ) . downcast_ref ( ) )
86
- }
87
-
88
- /// read all of the crate info from its location on the filesystem
89
- fn read ( parts_paths : & [ PathToParts ] ) -> Result < Vec < Self > , Error > {
90
- parts_paths. iter ( )
91
- . map ( |parts_path| {
92
- let path = parts_path. path ( ) ;
93
- let parts = try_err ! ( fs:: read( & path) , & path) ;
94
- let parts: CrateInfo = try_err ! ( serde_json:: from_slice( & parts) , & path) ;
95
- Ok :: < _ , Error > ( parts)
96
- } )
97
- . collect :: < Result < Vec < CrateInfo > , Error > > ( )
98
- }
99
- }
100
-
61
+ // TODO: string faster loading tetchnique with JSON.stringify
101
62
63
+ /// Write crate-info.json cross-crate information, static files, invocation-specific files, etc. to disk
102
64
pub ( crate ) fn write_shared (
103
65
cx : & mut Context < ' _ > ,
104
66
krate : & Crate ,
@@ -126,7 +88,7 @@ pub(crate) fn write_shared(
126
88
} ;
127
89
128
90
if let Some ( parts_out_dir) = & opt. parts_out_dir {
129
- let path = parts_out_dir. path ( ) . to_owned ( ) ;
91
+ let path = parts_out_dir. 0 . clone ( ) ;
130
92
write_create_parents ( cx, dbg ! ( path) , serde_json:: to_string ( & info) . unwrap ( ) ) ?;
131
93
}
132
94
@@ -230,30 +192,39 @@ fn write_search_desc(cx: &mut Context<'_>, krate: &Crate, search_desc: &[(usize,
230
192
Ok ( ( ) )
231
193
}
232
194
233
- /// A piece of one of the shared artifacts for documentation (search index, sources, alias list, etc.)
234
- ///
235
- /// Merged at a user specified time and written to the `doc/` directory
236
- # [ derive ( Serialize , Deserialize , Debug , Clone ) ]
237
- # [ serde ( transparent ) ]
238
- struct Part < T , U > {
239
- # [ serde ( skip ) ]
240
- _artifact : PhantomData < T > ,
241
- item : U ,
195
+ /// Written to `crate-info.json`. Contains pre-rendered contents to insert into the CCI template
196
+ # [ derive ( Serialize , Deserialize , Clone , Debug ) ]
197
+ struct CrateInfo {
198
+ src_files_js : PartsAndLocations < SourcesPart > ,
199
+ search_index_js : PartsAndLocations < SearchIndexPart > ,
200
+ all_crates : PartsAndLocations < AllCratesPart > ,
201
+ crates_index : PartsAndLocations < CratesIndexPart > ,
202
+ trait_impl : PartsAndLocations < TraitAliasPart > ,
203
+ type_impl : PartsAndLocations < TypeAliasPart > ,
242
204
}
243
205
244
- impl < T , U : fmt:: Display > fmt:: Display for Part < T , U > {
245
- /// Writes serialized JSON
246
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
247
- write ! ( f, "{}" , self . item)
206
+ impl CrateInfo {
207
+ /// Gets a reference to the cross-crate information parts for `T`
208
+ fn get < T : ' static > ( & self ) -> Option < & PartsAndLocations < T > > {
209
+ ( & self . src_files_js as & dyn Any ) . downcast_ref ( )
210
+ . or_else ( || ( & self . search_index_js as & dyn Any ) . downcast_ref ( ) )
211
+ . or_else ( || ( & self . all_crates as & dyn Any ) . downcast_ref ( ) )
212
+ . or_else ( || ( & self . crates_index as & dyn Any ) . downcast_ref ( ) )
213
+ . or_else ( || ( & self . trait_impl as & dyn Any ) . downcast_ref ( ) )
214
+ . or_else ( || ( & self . type_impl as & dyn Any ) . downcast_ref ( ) )
248
215
}
249
- }
250
216
251
- pub ( crate ) trait NamedPart : Sized {
252
- /// Identifies the kind of cross crate information.
253
- ///
254
- /// The cci type name in `doc.parts/<cci type>`
255
- type FileFormat : sorted_template:: FileFormat ;
256
- fn blank_template ( cx : & Context < ' _ > ) -> SortedTemplate < Self :: FileFormat > ;
217
+ /// read all of the crate info from its location on the filesystem
218
+ fn read ( parts_paths : & [ PathToParts ] ) -> Result < Vec < Self > , Error > {
219
+ parts_paths. iter ( )
220
+ . map ( |parts_path| {
221
+ let path = & parts_path. 0 ;
222
+ let parts = try_err ! ( fs:: read( & path) , & path) ;
223
+ let parts: CrateInfo = try_err ! ( serde_json:: from_slice( & parts) , & path) ;
224
+ Ok :: < _ , Error > ( parts)
225
+ } )
226
+ . collect :: < Result < Vec < CrateInfo > , Error > > ( )
227
+ }
257
228
}
258
229
259
230
/// Paths (relative to the doc root) and their pre-merge contents
@@ -274,17 +245,45 @@ impl<T, U> PartsAndLocations<Part<T, U>> {
274
245
self . parts . push ( ( path, Part { _artifact : PhantomData , item } ) ) ;
275
246
}
276
247
248
+ /// Singleton part, one file
277
249
fn with ( path : PathBuf , part : U ) -> Self {
278
250
let mut ret = Self :: default ( ) ;
279
251
ret. push ( path, part) ;
280
252
ret
281
253
}
282
254
}
283
255
256
+ /// A piece of one of the shared artifacts for documentation (search index, sources, alias list, etc.)
257
+ ///
258
+ /// Merged at a user specified time and written to the `doc/` directory
259
+ #[ derive( Serialize , Deserialize , Debug , Clone ) ]
260
+ #[ serde( transparent) ]
261
+ struct Part < T , U > {
262
+ #[ serde( skip) ]
263
+ _artifact : PhantomData < T > ,
264
+ item : U ,
265
+ }
266
+
267
+ impl < T , U : fmt:: Display > fmt:: Display for Part < T , U > {
268
+ /// Writes serialized JSON
269
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
270
+ write ! ( f, "{}" , self . item)
271
+ }
272
+ }
273
+
274
+ /// Wrapper trait for `Part<T, U>`
275
+ pub ( crate ) trait CciPart : Sized + fmt:: Display + ' static {
276
+ /// Identifies the kind of cross crate information.
277
+ ///
278
+ /// The cci type name in `doc.parts/<cci type>`
279
+ type FileFormat : sorted_template:: FileFormat ;
280
+ fn blank_template ( cx : & Context < ' _ > ) -> SortedTemplate < Self :: FileFormat > ;
281
+ }
282
+
284
283
#[ derive( Serialize , Deserialize , Clone , Default , Debug ) ]
285
284
struct SearchIndex ;
286
285
type SearchIndexPart = Part < SearchIndex , SortedJson > ;
287
- impl NamedPart for SearchIndexPart {
286
+ impl CciPart for SearchIndexPart {
288
287
type FileFormat = sorted_template:: Js ;
289
288
fn blank_template ( _cx : & Context < ' _ > ) -> SortedTemplate < Self :: FileFormat > {
290
289
SortedTemplate :: before_after ( r"var searchIndex = new Map([" , r"]);
@@ -302,7 +301,7 @@ impl PartsAndLocations<SearchIndexPart> {
302
301
#[ derive( Serialize , Deserialize , Clone , Default , Debug ) ]
303
302
struct AllCrates ;
304
303
type AllCratesPart = Part < AllCrates , SortedJson > ;
305
- impl NamedPart for AllCratesPart {
304
+ impl CciPart for AllCratesPart {
306
305
type FileFormat = sorted_template:: Js ;
307
306
fn blank_template ( _cx : & Context < ' _ > ) -> SortedTemplate < Self :: FileFormat > {
308
307
SortedTemplate :: before_after ( "window.ALL_CRATES = [" , "];" )
@@ -336,7 +335,7 @@ fn hack_get_external_crate_names(cx: &Context<'_>) -> Result<Vec<String>, Error>
336
335
#[ derive( Serialize , Deserialize , Clone , Default , Debug ) ]
337
336
struct CratesIndex ;
338
337
type CratesIndexPart = Part < CratesIndex , String > ;
339
- impl NamedPart for CratesIndexPart {
338
+ impl CciPart for CratesIndexPart {
340
339
type FileFormat = sorted_template:: Html ;
341
340
fn blank_template ( cx : & Context < ' _ > ) -> SortedTemplate < Self :: FileFormat > {
342
341
let mut magic = String :: from ( "\u{FFFC} " ) ;
@@ -381,7 +380,7 @@ impl PartsAndLocations<CratesIndexPart> {
381
380
#[ derive( Serialize , Deserialize , Clone , Default , Debug ) ]
382
381
struct Sources ;
383
382
type SourcesPart = Part < Sources , SortedJson > ;
384
- impl NamedPart for SourcesPart {
383
+ impl CciPart for SourcesPart {
385
384
type FileFormat = sorted_template:: Js ;
386
385
fn blank_template ( _cx : & Context < ' _ > ) -> SortedTemplate < Self :: FileFormat > {
387
386
// This needs to be `var`, not `const`.
@@ -474,7 +473,7 @@ impl Hierarchy {
474
473
#[ derive( Serialize , Deserialize , Clone , Default , Debug ) ]
475
474
struct TypeAlias ;
476
475
type TypeAliasPart = Part < TypeAlias , SortedJson > ;
477
- impl NamedPart for TypeAliasPart {
476
+ impl CciPart for TypeAliasPart {
478
477
type FileFormat = sorted_template:: Js ;
479
478
fn blank_template ( _cx : & Context < ' _ > ) -> SortedTemplate < Self :: FileFormat > {
480
479
SortedTemplate :: before_after ( r"(function() {
@@ -589,7 +588,7 @@ impl PartsAndLocations<TypeAliasPart> {
589
588
#[ derive( Serialize , Deserialize , Clone , Default , Debug ) ]
590
589
struct TraitAlias ;
591
590
type TraitAliasPart = Part < TraitAlias , SortedJson > ;
592
- impl NamedPart for TraitAliasPart {
591
+ impl CciPart for TraitAliasPart {
593
592
type FileFormat = sorted_template:: Js ;
594
593
fn blank_template ( _cx : & Context < ' _ > ) -> SortedTemplate < Self :: FileFormat > {
595
594
SortedTemplate :: before_after ( r"(function() {
@@ -836,25 +835,27 @@ impl Serialize for AliasSerializableImpl {
836
835
}
837
836
}
838
837
838
+ /// Create all parents
839
839
fn create_parents ( cx : & mut Context < ' _ > , path : & Path ) -> Result < ( ) , Error > {
840
840
let parent = path. parent ( ) . expect ( "trying to write to an empty path" ) ;
841
841
// TODO: check cache for whether this directory has already been created
842
842
try_err ! ( cx. shared. fs. create_dir_all( parent) , parent) ;
843
843
Ok ( ( ) )
844
844
}
845
845
846
+ /// Create parents and then write
846
847
fn write_create_parents ( cx : & mut Context < ' _ > , path : PathBuf , content : String ) -> Result < ( ) , Error > {
847
848
create_parents ( cx, & path) ?;
848
849
cx. shared . fs . write ( path, content) ?;
849
850
Ok ( ( ) )
850
851
}
851
852
852
853
/// info from this crate and the --include-info-json'd crates
853
- fn write_rendered_cci < T : NamedPart + DeserializeOwned + fmt :: Display + fmt :: Debug + Any > (
854
+ fn write_rendered_cci < T : CciPart + DeserializeOwned > (
854
855
cx : & mut Context < ' _ > ,
855
856
read_rendered_cci : bool ,
856
857
crates_info : & [ CrateInfo ] ,
857
- ) -> Result < ( ) , Error > where < T as NamedPart > :: FileFormat : std :: fmt :: Debug {
858
+ ) -> Result < ( ) , Error > {
858
859
// read parts from disk
859
860
let path_parts = crates_info. iter ( )
860
861
. map ( |crate_info| crate_info. get :: < T > ( ) . unwrap ( ) . parts . iter ( ) )
0 commit comments