Skip to content

Commit c562d17

Browse files
committed
more docs
1 parent 46abe8a commit c562d17

File tree

2 files changed

+74
-85
lines changed

2 files changed

+74
-85
lines changed

src/librustdoc/config.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ impl Options {
506506
};
507507

508508
let parts_paths = matches.opt_strs("include-info-json").iter()
509-
.map(|path| PathToParts::from_path_to_crate_info(PathBuf::from(path)))
509+
.map(|path| PathToParts(PathBuf::from(path)))
510510
.collect();
511511

512512
let default_settings: Vec<Vec<(String, String)>> = vec![
@@ -753,7 +753,7 @@ impl Options {
753753
Ok(result) => result,
754754
Err(e) => dcx.fatal(format!("could not parse --merge: {e}")),
755755
};
756-
let parts_out_dir = matches.opt_str("write-info-json").map(|p| PathToParts::from_path_to_crate_info(PathBuf::from(p)));
756+
let parts_out_dir = matches.opt_str("write-info-json").map(|p| PathToParts(PathBuf::from(p)));
757757

758758
if generate_link_to_definition && (show_coverage || output_format != OutputFormat::Html) {
759759
dcx.fatal(
@@ -921,23 +921,11 @@ fn parse_extern_html_roots(
921921
Ok(externs)
922922
}
923923

924-
/// Absolute path to cci root, including doc.parts, but not the crate name
924+
/// Path to cci root, including doc.parts, but not the crate name
925925
///
926926
/// For example, `/home/user/project/target/doc.parts/<crate>/crate-info.json`.
927927
#[derive(Clone, Debug)]
928-
pub(crate) struct PathToParts(PathBuf);
929-
930-
impl PathToParts {
931-
/// Absolute path to cci root, including doc.parts, but not the crate name
932-
fn from_path_to_crate_info(doc_root: PathBuf) -> Self {
933-
PathToParts(doc_root)
934-
}
935-
936-
/// Gets the final path at which to place the cci part
937-
pub(crate) fn path(&self) -> &Path {
938-
&self.0
939-
}
940-
}
928+
pub(crate) struct PathToParts(pub(crate) PathBuf);
941929

942930
/// Controls merging of cross-crate information
943931
struct ShouldMerge {

src/librustdoc/html/render/write_shared.rs

Lines changed: 70 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -58,47 +58,9 @@ use crate::html::static_files::{self, suffix_path};
5858
use crate::visit::DocVisitor;
5959
use crate::{try_err, try_none};
6060

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
10162

63+
/// Write crate-info.json cross-crate information, static files, invocation-specific files, etc. to disk
10264
pub(crate) fn write_shared(
10365
cx: &mut Context<'_>,
10466
krate: &Crate,
@@ -126,7 +88,7 @@ pub(crate) fn write_shared(
12688
};
12789

12890
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();
13092
write_create_parents(cx, dbg!(path), serde_json::to_string(&info).unwrap())?;
13193
}
13294

@@ -230,30 +192,39 @@ fn write_search_desc(cx: &mut Context<'_>, krate: &Crate, search_desc: &[(usize,
230192
Ok(())
231193
}
232194

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>,
242204
}
243205

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())
248215
}
249-
}
250216

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+
}
257228
}
258229

259230
/// Paths (relative to the doc root) and their pre-merge contents
@@ -274,17 +245,45 @@ impl<T, U> PartsAndLocations<Part<T, U>> {
274245
self.parts.push((path, Part { _artifact: PhantomData, item }));
275246
}
276247

248+
/// Singleton part, one file
277249
fn with(path: PathBuf, part: U) -> Self {
278250
let mut ret = Self::default();
279251
ret.push(path, part);
280252
ret
281253
}
282254
}
283255

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+
284283
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
285284
struct SearchIndex;
286285
type SearchIndexPart = Part<SearchIndex, SortedJson>;
287-
impl NamedPart for SearchIndexPart {
286+
impl CciPart for SearchIndexPart {
288287
type FileFormat = sorted_template::Js;
289288
fn blank_template(_cx: &Context<'_>) -> SortedTemplate<Self::FileFormat> {
290289
SortedTemplate::before_after(r"var searchIndex = new Map([", r"]);
@@ -302,7 +301,7 @@ impl PartsAndLocations<SearchIndexPart> {
302301
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
303302
struct AllCrates;
304303
type AllCratesPart = Part<AllCrates, SortedJson>;
305-
impl NamedPart for AllCratesPart {
304+
impl CciPart for AllCratesPart {
306305
type FileFormat = sorted_template::Js;
307306
fn blank_template(_cx: &Context<'_>) -> SortedTemplate<Self::FileFormat> {
308307
SortedTemplate::before_after("window.ALL_CRATES = [", "];")
@@ -336,7 +335,7 @@ fn hack_get_external_crate_names(cx: &Context<'_>) -> Result<Vec<String>, Error>
336335
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
337336
struct CratesIndex;
338337
type CratesIndexPart = Part<CratesIndex, String>;
339-
impl NamedPart for CratesIndexPart {
338+
impl CciPart for CratesIndexPart {
340339
type FileFormat = sorted_template::Html;
341340
fn blank_template(cx: &Context<'_>) -> SortedTemplate<Self::FileFormat> {
342341
let mut magic = String::from("\u{FFFC}");
@@ -381,7 +380,7 @@ impl PartsAndLocations<CratesIndexPart> {
381380
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
382381
struct Sources;
383382
type SourcesPart = Part<Sources, SortedJson>;
384-
impl NamedPart for SourcesPart {
383+
impl CciPart for SourcesPart {
385384
type FileFormat = sorted_template::Js;
386385
fn blank_template(_cx: &Context<'_>) -> SortedTemplate<Self::FileFormat> {
387386
// This needs to be `var`, not `const`.
@@ -474,7 +473,7 @@ impl Hierarchy {
474473
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
475474
struct TypeAlias;
476475
type TypeAliasPart = Part<TypeAlias, SortedJson>;
477-
impl NamedPart for TypeAliasPart {
476+
impl CciPart for TypeAliasPart {
478477
type FileFormat = sorted_template::Js;
479478
fn blank_template(_cx: &Context<'_>) -> SortedTemplate<Self::FileFormat> {
480479
SortedTemplate::before_after(r"(function() {
@@ -589,7 +588,7 @@ impl PartsAndLocations<TypeAliasPart> {
589588
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
590589
struct TraitAlias;
591590
type TraitAliasPart = Part<TraitAlias, SortedJson>;
592-
impl NamedPart for TraitAliasPart {
591+
impl CciPart for TraitAliasPart {
593592
type FileFormat = sorted_template::Js;
594593
fn blank_template(_cx: &Context<'_>) -> SortedTemplate<Self::FileFormat> {
595594
SortedTemplate::before_after(r"(function() {
@@ -836,25 +835,27 @@ impl Serialize for AliasSerializableImpl {
836835
}
837836
}
838837

838+
/// Create all parents
839839
fn create_parents(cx: &mut Context<'_>, path: &Path) -> Result<(), Error> {
840840
let parent = path.parent().expect("trying to write to an empty path");
841841
// TODO: check cache for whether this directory has already been created
842842
try_err!(cx.shared.fs.create_dir_all(parent), parent);
843843
Ok(())
844844
}
845845

846+
/// Create parents and then write
846847
fn write_create_parents(cx: &mut Context<'_>, path: PathBuf, content: String) -> Result<(), Error> {
847848
create_parents(cx, &path)?;
848849
cx.shared.fs.write(path, content)?;
849850
Ok(())
850851
}
851852

852853
/// 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>(
854855
cx: &mut Context<'_>,
855856
read_rendered_cci: bool,
856857
crates_info: &[CrateInfo],
857-
) -> Result<(), Error> where <T as NamedPart>::FileFormat: std::fmt::Debug {
858+
) -> Result<(), Error> {
858859
// read parts from disk
859860
let path_parts = crates_info.iter()
860861
.map(|crate_info| crate_info.get::<T>().unwrap().parts.iter())

0 commit comments

Comments
 (0)