diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index 39e33da4496..1ea9e28ae42 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -4,6 +4,9 @@ use std::ffi::OsStr; use std::fmt; use std::path::PathBuf; +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; +use rustc_hir::def_id::DefId; +use rustc_middle::middle::privacy::AccessLevels; use rustc_session::config::{self, parse_crate_types_from_list, parse_externs, CrateType}; use rustc_session::config::{ build_codegen_options, build_debugging_options, get_cmd_lint_options, host_triple, @@ -249,6 +252,20 @@ pub struct RenderOptions { pub document_hidden: bool, } +/// Temporary storage for data obtained during `RustdocVisitor::clean()`. +/// Later on moved into `CACHE_KEY`. +#[derive(Default)] +pub struct RenderInfo { + pub inlined: FxHashSet, + pub external_paths: crate::core::ExternalPaths, + pub exact_paths: FxHashMap>, + pub access_levels: AccessLevels, + pub deref_trait_did: Option, + pub deref_mut_trait_did: Option, + pub owned_box_did: Option, + pub output_format: Option, +} + impl Options { /// Parses the given command-line for options. If an error message or other early-return has /// been printed, returns `Err` with the exit code. diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 263909d5559..85bb7ca4cd6 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -32,8 +32,8 @@ use std::rc::Rc; use crate::clean; use crate::clean::{AttributesExt, MAX_DEF_ID}; +use crate::config::RenderInfo; use crate::config::{Options as RustdocOptions, RenderOptions}; -use crate::html::render::RenderInfo; use crate::passes::{self, Condition::*, ConditionalPass}; pub use rustc_session::config::{CodegenOptions, DebuggingOptions, Input, Options}; diff --git a/src/librustdoc/error.rs b/src/librustdoc/error.rs new file mode 100644 index 00000000000..77063ab4639 --- /dev/null +++ b/src/librustdoc/error.rs @@ -0,0 +1,56 @@ +use std::error; +use std::fmt::{self, Formatter}; +use std::path::{Path, PathBuf}; + +use crate::docfs::PathError; + +#[derive(Debug)] +pub struct Error { + pub file: PathBuf, + pub error: String, +} + +impl error::Error for Error {} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let file = self.file.display().to_string(); + if file.is_empty() { + write!(f, "{}", self.error) + } else { + write!(f, "\"{}\": {}", self.file.display(), self.error) + } + } +} + +impl PathError for Error { + fn new>(e: S, path: P) -> Error + where + S: ToString + Sized, + { + Error { file: path.as_ref().to_path_buf(), error: e.to_string() } + } +} + +#[macro_export] +macro_rules! try_none { + ($e:expr, $file:expr) => {{ + use std::io; + match $e { + Some(e) => e, + None => { + return Err(Error::new(io::Error::new(io::ErrorKind::Other, "not found"), $file)); + } + } + }}; +} + +#[macro_export] +macro_rules! try_err { + ($e:expr, $file:expr) => {{ + match $e { + Ok(e) => e, + Err(e) => return Err(Error::new(e, $file)), + } + }}; +} diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index f7050cf3777..18ef97118e0 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -30,9 +30,8 @@ use std::cell::{Cell, RefCell}; use std::cmp::Ordering; use std::collections::{BTreeMap, VecDeque}; use std::default::Default; -use std::error; use std::ffi::OsStr; -use std::fmt::{self, Formatter, Write}; +use std::fmt::{self, Write}; use std::fs::{self, File}; use std::io::prelude::*; use std::io::{self, BufReader}; @@ -50,7 +49,6 @@ use rustc_feature::UnstableFeatures; use rustc_hir as hir; use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_hir::Mutability; -use rustc_middle::middle::privacy::AccessLevels; use rustc_middle::middle::stability; use rustc_span::edition::Edition; use rustc_span::hygiene::MacroKind; @@ -60,9 +58,11 @@ use serde::ser::SerializeSeq; use serde::{Serialize, Serializer}; use crate::clean::{self, AttributesExt, Deprecation, GetDefId, SelfTy, TypeKind}; -use crate::config::{OutputFormat, RenderOptions}; +use crate::config::RenderInfo; +use crate::config::RenderOptions; use crate::docfs::{DocFS, ErrorStorage, PathError}; use crate::doctree; +use crate::error::Error; use crate::html::escape::Escape; use crate::html::format::fmt_impl_for_trait_page; use crate::html::format::Function; @@ -90,55 +90,6 @@ crate fn ensure_trailing_slash(v: &str) -> impl fmt::Display + '_ { }) } -#[derive(Debug)] -pub struct Error { - pub file: PathBuf, - pub error: String, -} - -impl error::Error for Error {} - -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - let file = self.file.display().to_string(); - if file.is_empty() { - write!(f, "{}", self.error) - } else { - write!(f, "\"{}\": {}", self.file.display(), self.error) - } - } -} - -impl PathError for Error { - fn new>(e: S, path: P) -> Error - where - S: ToString + Sized, - { - Error { file: path.as_ref().to_path_buf(), error: e.to_string() } - } -} - -macro_rules! try_none { - ($e:expr, $file:expr) => {{ - use std::io; - match $e { - Some(e) => e, - None => { - return Err(Error::new(io::Error::new(io::ErrorKind::Other, "not found"), $file)); - } - } - }}; -} - -macro_rules! try_err { - ($e:expr, $file:expr) => {{ - match $e { - Ok(e) => e, - Err(e) => return Err(Error::new(e, $file)), - } - }}; -} - /// Major driving force in all rustdoc rendering. This contains information /// about where in the tree-like hierarchy rendering is occurring and controls /// how the current page is being rendered. @@ -260,20 +211,6 @@ impl Impl { } } -/// Temporary storage for data obtained during `RustdocVisitor::clean()`. -/// Later on moved into `CACHE_KEY`. -#[derive(Default)] -pub struct RenderInfo { - pub inlined: FxHashSet, - pub external_paths: crate::core::ExternalPaths, - pub exact_paths: FxHashMap>, - pub access_levels: AccessLevels, - pub deref_trait_did: Option, - pub deref_mut_trait_did: Option, - pub owned_box_did: Option, - pub output_format: Option, -} - // Helper structs for rendering items/sidebars and carrying along contextual // information diff --git a/src/librustdoc/html/render/cache.rs b/src/librustdoc/html/render/cache.rs index 1b5c8a9378e..6da4a4628e8 100644 --- a/src/librustdoc/html/render/cache.rs +++ b/src/librustdoc/html/render/cache.rs @@ -12,7 +12,8 @@ use std::path::{Path, PathBuf}; use serde::Serialize; use super::{plain_summary_line, shorten, Impl, IndexItem, IndexItemFunctionType, ItemType}; -use super::{Generic, RenderInfo, RenderType, TypeWithKind}; +use super::{Generic, RenderType, TypeWithKind}; +use crate::config::RenderInfo; /// Indicates where an external crate can be found. pub enum ExternalLocation { diff --git a/src/librustdoc/html/sources.rs b/src/librustdoc/html/sources.rs index e3215921f12..aaa73b100c2 100644 --- a/src/librustdoc/html/sources.rs +++ b/src/librustdoc/html/sources.rs @@ -1,10 +1,11 @@ use crate::clean; use crate::docfs::PathError; +use crate::error::Error; use crate::fold::DocFolder; use crate::html::format::Buffer; use crate::html::highlight; use crate::html::layout; -use crate::html::render::{Error, SharedContext, BASIC_KEYWORDS}; +use crate::html::render::{SharedContext, BASIC_KEYWORDS}; use rustc_hir::def_id::LOCAL_CRATE; use rustc_span::source_map::FileName; use std::ffi::OsStr; diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index cbf53d52ef0..ac31ab5980c 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -63,6 +63,8 @@ mod config; mod core; mod docfs; mod doctree; +#[macro_use] +mod error; mod fold; pub mod html { crate mod escape; @@ -85,7 +87,7 @@ mod visit_lib; struct Output { krate: clean::Crate, - renderinfo: html::render::RenderInfo, + renderinfo: config::RenderInfo, renderopts: config::RenderOptions, }