Skip to content

Commit

Permalink
fix(lib): generalize the manifest_path accepted by entry functions
Browse files Browse the repository at this point in the history
The old manifest_path for verify_conditions(), prepare(), and
list_packages() was an Option<PathBuf> which limited the types that
could be passed. Generalize manifest_path to now allow Option<impl
AsRef<Path>> which allows a broader range of types to be passed in.

BREAKING CHANGE: generalize the manifest_path passed to the main library
functions

The manifest_path specified in verify_conditions(), prepare(), and
list_packages() previously supplied enough type inforation to support
type inferance at the call site. The more generalized manifest_path may
cause type inferance to fail in circumstances where it used to suceed.
The fix for this is to add further type information at the call site for
these functions.
  • Loading branch information
sbosnick committed Jul 24, 2020
1 parent 135c0dc commit 9f76a25
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
22 changes: 17 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ pub use error::{Error, Result};
///
/// This implments the `verifyConditions` step for `sementic-release` for a
/// Cargo-based rust workspace.
pub fn verify_conditions(mut output: impl Write, manifest_path: Option<&PathBuf>) -> Result<()> {
pub fn verify_conditions(
mut output: impl Write,
manifest_path: Option<impl AsRef<Path>>,
) -> Result<()> {
info!("Checking CARGO_REGISTRY_TOKEN");
env::var_os("CARGO_REGISTRY_TOKEN")
.and_then(|val| if val.is_empty() { None } else { Some(()) })
Expand Down Expand Up @@ -123,7 +126,11 @@ pub fn verify_conditions(mut output: impl Write, manifest_path: Option<&PathBuf>

// TODO: remove when not needed
#[allow(missing_docs)]
pub fn prepare(_output: impl Write, _manifest_path: Option<&PathBuf>, _version: &str) -> Result<()> {
pub fn prepare(
_output: impl Write,
_manifest_path: Option<impl AsRef<Path>>,
_version: &str,
) -> Result<()> {
todo!()
}

Expand All @@ -136,7 +143,10 @@ pub fn prepare(_output: impl Write, _manifest_path: Option<&PathBuf>, _version:
///
/// This is a debuging aid and does not directly correspond to a sementic release
/// step.
pub fn list_packages(mut output: impl Write, manifest_path: Option<&PathBuf>) -> Result<()> {
pub fn list_packages(
mut output: impl Write,
manifest_path: Option<impl AsRef<Path>>,
) -> Result<()> {
info!("Building package graph");
let graph = get_package_graph(manifest_path)?;

Expand All @@ -155,7 +165,9 @@ pub fn list_packages(mut output: impl Write, manifest_path: Option<&PathBuf>) ->
Ok(())
}

fn get_package_graph(manifest_path: Option<&PathBuf>) -> Result<PackageGraph> {
fn get_package_graph(manifest_path: Option<impl AsRef<Path>>) -> Result<PackageGraph> {
let manifest_path = manifest_path.as_ref().map(|path| path.as_ref());

let mut command = MetadataCommand::new();
if let Some(path) = manifest_path {
command.manifest_path(path);
Expand All @@ -165,7 +177,7 @@ fn get_package_graph(manifest_path: Option<&PathBuf>) -> Result<PackageGraph> {

command.build_graph().map_err(|err| {
let path = match manifest_path {
Some(path) => path.clone(),
Some(path) => path.to_path_buf(),
None => env::current_dir()
.map(|path| path.join("Cargo.toml"))
.unwrap_or_else(|e| {
Expand Down
14 changes: 10 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use std::fs::File;
use std::io::{prelude::*, stdout, BufWriter};
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use anyhow::{Context, Error};
use human_panic::setup_panic;
Expand Down Expand Up @@ -95,9 +95,9 @@ impl Subcommand {
use Subcommand::*;

match self {
ListPackages(opt) => Ok(list_packages(w, (&opt.manifest_path).into())?),
VerifyConditions(opt) => Ok(verify_conditions(w, (&opt.manifest_path).into())?),
Prepare(opt) => Ok(prepare(w, (&opt.common.manifest_path).into(), &opt.next_version)?),
ListPackages(opt) => Ok(list_packages(w, opt.manifest_path())?),
VerifyConditions(opt) => Ok(verify_conditions(w, opt.manifest_path())?),
Prepare(opt) => Ok(prepare(w, opt.common.manifest_path(), &opt.next_version)?),
}
}
}
Expand All @@ -124,3 +124,9 @@ fn main() -> Result<(), Error> {
None => opt.subcommand.run(BufWriter::new(stdout())),
}
}

impl CommonOpt {
fn manifest_path(&self) -> Option<&Path> {
self.manifest_path.as_ref().map(|path| path.as_path())
}
}

0 comments on commit 9f76a25

Please sign in to comment.