Skip to content
This repository has been archived by the owner on May 20, 2020. It is now read-only.

Commit

Permalink
use enum for verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
euclio committed Aug 26, 2017
1 parent fd6dcbc commit c4723b7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use error::*;
use ui::Ui;

pub use error::{Error, ErrorKind};
pub use ui::Verbosity;

/// A structure that contains various fields that hold data in order to generate doc output.
#[derive(Debug)]
Expand All @@ -60,15 +61,15 @@ impl Config {
/// ## Arguments
///
/// - `manifest_path`: The path to the `Cargo.toml` of the crate being documented
pub fn new(quiet: bool, manifest_path: PathBuf, assets: Vec<Asset>) -> Result<Config> {
pub fn new(verbosity: Verbosity, manifest_path: PathBuf, assets: Vec<Asset>) -> Result<Config> {
let host = analysis::AnalysisHost::new(analysis::Target::Debug);

if !manifest_path.is_file() || !manifest_path.ends_with("Cargo.toml") {
bail!("The --manifest-path must be a path to a Cargo.toml file");
}

Ok(Config {
ui: Ui::new(quiet),
ui: Ui::new(verbosity),
manifest_path,
host,
assets,
Expand Down
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extern crate clap;

use clap::{App, Arg, SubCommand};

use rustdoc::{Config, build};
use rustdoc::{build, Config, Verbosity};

use std::io::{Write, stderr};
use std::process;
Expand Down Expand Up @@ -57,7 +57,12 @@ fn run() -> rustdoc::error::Result<()> {
// unwrap is okay because we take a default value
let manifest_path = PathBuf::from(&matches.value_of("manifest-path").unwrap());
let assets = include!(concat!(env!("OUT_DIR"), "/asset.in"));
let config = Config::new(matches.is_present("quiet"), manifest_path, assets)?;
let verbosity = if matches.is_present("quiet") {
Verbosity::Quiet
} else {
Verbosity::Normal
};
let config = Config::new(verbosity, manifest_path, assets)?;

match matches.subcommand() {
("build", Some(matches)) => {
Expand Down
32 changes: 24 additions & 8 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use std::cell::Cell;
use std::cmp;
use std::fmt::{self, Debug};

use indicatif::{ProgressBar, ProgressStyle};

#[derive(Debug, Default)]
pub struct Ui {
quiet: bool,
verbosity: Verbosity,
}

impl Ui {
pub fn new(quiet: bool) -> Ui {
Ui { quiet }
pub fn new(verbosity: Verbosity) -> Ui {
Ui { verbosity }
}

pub fn start_task(&self, name: &str) -> Task {
let spinner = if self.quiet {
ProgressBar::hidden()
} else {
ProgressBar::new_spinner()
let spinner = match self.verbosity {
Verbosity::Normal => ProgressBar::new_spinner(),
Verbosity::Quiet => ProgressBar::hidden(),
};

spinner.enable_steady_tick(50);
Expand All @@ -35,12 +35,28 @@ impl Ui {
}

pub fn warn(&self, message: &str) {
if !self.quiet {
if self.verbosity > Verbosity::Quiet {
eprintln!("warning: {}", message);
}
}
}

/// The verbosity of the output displayed to the user.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Verbosity {
/// No output.
Quiet,

/// Normal output, with spinners.
Normal,
}

impl Default for Verbosity {
fn default() -> Verbosity {
Verbosity::Normal
}
}

pub struct Task<'a> {
ui: &'a Ui,
spinner: ProgressBar,
Expand Down
13 changes: 8 additions & 5 deletions tests/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ mod validation_tests {
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use rustdoc::{build, Config};
use rustdoc::{build, Config, Verbosity};

#[test]
fn json_fmt_test() {
let config = Config::new(true, PathBuf::from("example/Cargo.toml"), vec![])
.unwrap_or_else(|err| {
panic!("Couldn't create the config: {}", err);
});
let config = Config::new(
Verbosity::Quiet,
PathBuf::from("example/Cargo.toml"),
vec![],
).unwrap_or_else(|err| {
panic!("Couldn't create the config: {}", err);
});

build(&config, &["json"]).unwrap_or_else(|err| {
panic!("Error: {}", err);
Expand Down

0 comments on commit c4723b7

Please sign in to comment.