More requested changes

This commit is contained in:
Joseph Ryan
2020-07-27 17:34:17 -05:00
parent 3d707a008e
commit cee8023c69
6 changed files with 64 additions and 84 deletions

View File

@@ -7,7 +7,7 @@ use crate::config::{RenderInfo, RenderOptions};
use crate::error::Error;
use crate::formats::cache::{Cache, CACHE_KEY};
/// Allows for different backends to rustdoc to be used with the `Renderer::run()` function. Each
/// Allows for different backends to rustdoc to be used with the `run_format()` function. Each
/// backend renderer has hooks for initialization, documenting an item, entering and exiting a
/// module, and cleanup/finalizing output.
pub trait FormatRenderer: Clone {
@@ -42,75 +42,65 @@ pub trait FormatRenderer: Clone {
fn after_run(&mut self, diag: &rustc_errors::Handler) -> Result<(), Error>;
}
#[derive(Clone)]
pub struct Renderer;
/// Main method for rendering a crate.
pub fn run_format<T: FormatRenderer>(
krate: clean::Crate,
options: RenderOptions,
render_info: RenderInfo,
diag: &rustc_errors::Handler,
edition: Edition,
) -> Result<(), Error> {
let (krate, mut cache) = Cache::from_krate(
render_info.clone(),
options.document_private,
&options.extern_html_root_urls,
&options.output,
krate,
);
impl Renderer {
pub fn new() -> Renderer {
Renderer
}
let (mut format_renderer, mut krate) =
T::init(krate, options, render_info, edition, &mut cache)?;
/// Main method for rendering a crate.
pub fn run<T: FormatRenderer + Clone>(
self,
krate: clean::Crate,
options: RenderOptions,
render_info: RenderInfo,
diag: &rustc_errors::Handler,
edition: Edition,
) -> Result<(), Error> {
let (krate, mut cache) = Cache::from_krate(
render_info.clone(),
options.document_private,
&options.extern_html_root_urls,
&options.output,
krate,
);
let cache = Arc::new(cache);
// Freeze the cache now that the index has been built. Put an Arc into TLS for future
// parallelization opportunities
CACHE_KEY.with(|v| *v.borrow_mut() = cache.clone());
let (mut format_renderer, mut krate) =
T::init(krate, options, render_info, edition, &mut cache)?;
let mut item = match krate.module.take() {
Some(i) => i,
None => return Ok(()),
};
let cache = Arc::new(cache);
// Freeze the cache now that the index has been built. Put an Arc into TLS for future
// parallelization opportunities
CACHE_KEY.with(|v| *v.borrow_mut() = cache.clone());
item.name = Some(krate.name.clone());
let mut item = match krate.module.take() {
Some(i) => i,
None => return Ok(()),
};
// Render the crate documentation
let mut work = vec![(format_renderer.clone(), item)];
item.name = Some(krate.name.clone());
// Render the crate documentation
let mut work = vec![(format_renderer.clone(), item)];
while let Some((mut cx, item)) = work.pop() {
if item.is_mod() {
// modules are special because they add a namespace. We also need to
// recurse into the items of the module as well.
let name = item.name.as_ref().unwrap().to_string();
if name.is_empty() {
panic!("Unexpected module with empty name");
}
cx.mod_item_in(&item, &name, &cache)?;
let module = match item.inner {
clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
_ => unreachable!(),
};
for it in module.items {
debug!("Adding {:?} to worklist", it.name);
work.push((cx.clone(), it));
}
cx.mod_item_out(&name)?;
} else if item.name.is_some() {
cx.item(item, &cache)?;
while let Some((mut cx, item)) = work.pop() {
if item.is_mod() {
// modules are special because they add a namespace. We also need to
// recurse into the items of the module as well.
let name = item.name.as_ref().unwrap().to_string();
if name.is_empty() {
panic!("Unexpected module with empty name");
}
}
format_renderer.after_krate(&krate, &cache)?;
format_renderer.after_run(diag)
cx.mod_item_in(&item, &name, &cache)?;
let module = match item.inner {
clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
_ => unreachable!(),
};
for it in module.items {
debug!("Adding {:?} to worklist", it.name);
work.push((cx.clone(), it));
}
cx.mod_item_out(&name)?;
} else if item.name.is_some() {
cx.item(item, &cache)?;
}
}
format_renderer.after_krate(&krate, &cache)?;
format_renderer.after_run(diag)
}