Skip to content

Commit

Permalink
Renamed baseline.rs and dump.rs files (#348)
Browse files Browse the repository at this point in the history
* Changed some load_rustdoc arguments to a struct

* Renamed baseline.rs and dump.rs files

* Apply suggestions from code review

* Update src/rustdoc_gen.rs

---------

Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com>
Co-authored-by: Predrag Gruevski <obi1kenobi82@gmail.com>
  • Loading branch information
3 people authored Feb 5, 2023
1 parent 0a4cdef commit 4920a4e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
26 changes: 13 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#![forbid(unsafe_code)]

mod baseline;
mod check_release;
mod config;
mod dump;
mod manifest;
mod query;
mod rustdoc_cmd;
mod rustdoc_gen;
mod templating;
mod util;

use dump::RustDocCommand;
use itertools::Itertools;
use rustdoc_cmd::RustdocCommand;
use semver::Version;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -91,13 +91,13 @@ fn main() -> anyhow::Result<()> {
Some(SemverChecksCommands::CheckRelease(args)) => {
let mut config = GlobalConfig::new().set_level(args.verbosity.log_level());

let loader: Box<dyn baseline::BaselineLoader> =
let loader: Box<dyn rustdoc_gen::BaselineLoader> =
if let Some(path) = args.baseline_rustdoc.as_deref() {
Box::new(baseline::RustdocBaseline::new(path.to_owned()))
Box::new(rustdoc_gen::RustdocBaseline::new(path.to_owned()))
} else if let Some(root) = args.baseline_root.as_deref() {
let metadata = args.manifest.metadata().no_deps().exec()?;
let target = metadata.target_directory.as_std_path().join(util::SCOPE);
Box::new(baseline::PathBaseline::new(root, &target)?)
Box::new(rustdoc_gen::PathBaseline::new(root, &target)?)
} else if let Some(rev) = args.baseline_rev.as_deref() {
let metadata = args.manifest.metadata().no_deps().exec()?;
let source = metadata.workspace_root.as_std_path();
Expand All @@ -107,7 +107,7 @@ fn main() -> anyhow::Result<()> {
.as_std_path()
.join(util::SCOPE)
.join(format!("git-{slug}"));
Box::new(baseline::GitBaseline::with_rev(
Box::new(rustdoc_gen::GitBaseline::with_rev(
source,
&target,
rev,
Expand All @@ -116,14 +116,14 @@ fn main() -> anyhow::Result<()> {
} else {
let metadata = args.manifest.metadata().no_deps().exec()?;
let target = metadata.target_directory.as_std_path().join(util::SCOPE);
let mut registry = baseline::RegistryBaseline::new(&target, &mut config)?;
let mut registry = rustdoc_gen::RegistryBaseline::new(&target, &mut config)?;
if let Some(version) = args.baseline_version.as_deref() {
let version = semver::Version::parse(version)?;
registry.set_version(version);
}
Box::new(registry)
};
let rustdoc_cmd = dump::RustDocCommand::new()
let rustdoc_cmd = rustdoc_cmd::RustdocCommand::new()
.deps(false)
.silence(!config.is_verbose());

Expand Down Expand Up @@ -212,8 +212,8 @@ enum CurrentCratePath<'a> {
fn generate_versioned_crates(
config: &mut GlobalConfig,
current_crate_path: CurrentCratePath,
loader: &dyn baseline::BaselineLoader,
rustdoc_cmd: &RustDocCommand,
loader: &dyn rustdoc_gen::BaselineLoader,
rustdoc_cmd: &RustdocCommand,
crate_name: &str,
version: Option<&Version>,
) -> anyhow::Result<(VersionedCrate, VersionedCrate)> {
Expand All @@ -233,9 +233,9 @@ fn generate_versioned_crates(
let baseline_path = loader.load_rustdoc(
config,
rustdoc_cmd,
baseline::CrateDataForRustdoc {
rustdoc_gen::CrateDataForRustdoc {
name: crate_name,
crate_type: baseline::CrateType::Baseline {
crate_type: rustdoc_gen::CrateType::Baseline {
highest_allowed_version: version,
},
},
Expand Down
16 changes: 8 additions & 8 deletions src/dump.rs → src/rustdoc_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RustDocCommand {
pub struct RustdocCommand {
deps: bool,
silence: bool,
}

impl RustDocCommand {
impl RustdocCommand {
pub fn new() -> Self {
Self {
deps: false,
Expand Down Expand Up @@ -152,7 +152,7 @@ impl RustDocCommand {
}
}

impl Default for RustDocCommand {
impl Default for RustdocCommand {
fn default() -> Self {
Self::new()
}
Expand All @@ -162,11 +162,11 @@ impl Default for RustDocCommand {
mod tests {
use std::path::Path;

use super::RustDocCommand;
use super::RustdocCommand;

#[test]
fn rustdoc_for_lib_crate_without_lib_section() {
RustDocCommand::default()
RustdocCommand::default()
.dump(
Path::new("./test_rustdoc/implicit_lib/Cargo.toml"),
None,
Expand All @@ -177,7 +177,7 @@ mod tests {

#[test]
fn rustdoc_for_lib_crate_with_lib_section() {
RustDocCommand::default()
RustdocCommand::default()
.dump(
Path::new("./test_rustdoc/renamed_lib/Cargo.toml"),
None,
Expand All @@ -188,7 +188,7 @@ mod tests {

#[test]
fn rustdoc_for_bin_crate_without_bin_section() {
RustDocCommand::default()
RustdocCommand::default()
.dump(
Path::new("./test_rustdoc/implicit_bin/Cargo.toml"),
None,
Expand All @@ -199,7 +199,7 @@ mod tests {

#[test]
fn rustdoc_for_bin_crate_with_bin_section() {
RustDocCommand::default()
RustdocCommand::default()
.dump(
Path::new("./test_rustdoc/renamed_bin/Cargo.toml"),
None,
Expand Down
29 changes: 15 additions & 14 deletions src/baseline.rs → src/rustdoc_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::path::{Path, PathBuf};
use anyhow::Context as _;
use crates_index::Crate;

use crate::dump::RustDocCommand;
use crate::manifest::Manifest;
use crate::rustdoc_cmd::RustdocCommand;
use crate::util::slugify;
use crate::GlobalConfig;

Expand Down Expand Up @@ -177,7 +177,7 @@ fn save_placeholder_rustdoc_manifest(
}

#[allow(dead_code)]
#[derive(Debug)]
#[derive(Debug, Clone)]
pub(crate) enum CrateType<'a> {
Current,
Baseline {
Expand All @@ -188,26 +188,25 @@ pub(crate) enum CrateType<'a> {
},
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub(crate) struct CrateDataForRustdoc<'a> {
pub(crate) crate_type: CrateType<'a>,
pub(crate) name: &'a str,
// TODO: pass an enum describing which features to enable
}

impl<'a> ToString for CrateType<'a> {
fn to_string(&self) -> String {
impl<'a> CrateType<'a> {
fn type_name(&self) -> &'static str {
match self {
CrateType::Current => "current",
CrateType::Baseline { .. } => "baseline",
}
.to_string()
}
}

fn generate_rustdoc(
config: &mut GlobalConfig,
rustdoc: &RustDocCommand,
rustdoc: &RustdocCommand,
target_root: PathBuf,
crate_source: CrateSource,
crate_data: CrateDataForRustdoc,
Expand Down Expand Up @@ -240,7 +239,7 @@ fn generate_rustdoc(
"Parsing",
format_args!(
"{name} v{version} ({}, cached)",
crate_data.crate_type.to_string()
crate_data.crate_type.type_name()
),
)?;
return Ok(cached_rustdoc);
Expand All @@ -259,8 +258,10 @@ fn generate_rustdoc(

config.shell_status(
"Parsing",
format_args!("{name} v{version} ({})", crate_data.crate_type.to_string()),
format_args!("{name} v{version} ({})", crate_data.crate_type.type_name()),
)?;
// TODO: replace "baseline" with a string passed as a function argument
// (the plan is to make this function work for both baseline and current).

let rustdoc_path = rustdoc.dump(
placeholder_manifest_path.as_path(),
Expand Down Expand Up @@ -295,7 +296,7 @@ pub(crate) trait BaselineLoader {
fn load_rustdoc(
&self,
config: &mut GlobalConfig,
rustdoc: &RustDocCommand,
rustdoc: &RustdocCommand,
crate_data: CrateDataForRustdoc,
) -> anyhow::Result<PathBuf>;
}
Expand All @@ -314,7 +315,7 @@ impl BaselineLoader for RustdocBaseline {
fn load_rustdoc(
&self,
_config: &mut GlobalConfig,
_rustdoc: &RustDocCommand,
_rustdoc: &RustdocCommand,
_crate_data: CrateDataForRustdoc,
) -> anyhow::Result<PathBuf> {
Ok(self.path.clone())
Expand Down Expand Up @@ -358,7 +359,7 @@ impl BaselineLoader for PathBaseline {
fn load_rustdoc(
&self,
config: &mut GlobalConfig,
rustdoc: &RustDocCommand,
rustdoc: &RustdocCommand,
crate_data: CrateDataForRustdoc,
) -> anyhow::Result<PathBuf> {
let manifest: &Manifest = self.lookup.get(crate_data.name).with_context(|| {
Expand Down Expand Up @@ -442,7 +443,7 @@ impl BaselineLoader for GitBaseline {
fn load_rustdoc(
&self,
config: &mut GlobalConfig,
rustdoc: &RustDocCommand,
rustdoc: &RustdocCommand,
crate_data: CrateDataForRustdoc,
) -> anyhow::Result<PathBuf> {
self.path.load_rustdoc(config, rustdoc, crate_data)
Expand Down Expand Up @@ -543,7 +544,7 @@ impl BaselineLoader for RegistryBaseline {
fn load_rustdoc(
&self,
config: &mut GlobalConfig,
rustdoc: &RustDocCommand,
rustdoc: &RustdocCommand,
crate_data: CrateDataForRustdoc,
) -> anyhow::Result<PathBuf> {
let crate_ = self
Expand Down

0 comments on commit 4920a4e

Please # to comment.