-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathlib.rs
More file actions
88 lines (78 loc) · 2.23 KB
/
Copy pathlib.rs
File metadata and controls
88 lines (78 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) The cargo-guppy Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Support for comparing Cargo and Guppy.
use crate::{check::CheckOpts, diff::DiffOpts};
use clap::Parser;
use color_eyre::eyre::Result;
use either::Either;
use guppy::graph::PackageGraph;
use std::{
env,
path::{Path, PathBuf},
};
use tempfile::TempDir;
pub mod check;
pub mod common;
pub mod diff;
#[cfg(test)]
mod tests;
pub mod type_conversions;
#[derive(Debug, Parser)]
pub struct CargoCompare {
// TODO: add global options
#[clap(subcommand)]
cmd: Command,
}
impl CargoCompare {
pub fn exec(self) -> Result<()> {
match self.cmd {
Command::Diff(opts) => {
// Don't use the temporary home here so that Cargo caches can be reused.
let graph = opts.common.metadata_opts.make_command().build_graph()?;
let ctx = GlobalContext::new(false, &graph)?;
opts.exec(&ctx)
}
Command::Check(opts) => {
// Don't use the temporary home here so that Cargo caches can be reused.
let graph = opts.metadata.make_command().build_graph()?;
let ctx = GlobalContext::new(false, &graph)?;
opts.exec(&ctx)
}
}
}
}
#[derive(Debug, Parser)]
enum Command {
/// Perform a diff of Cargo's results against Guppy's
Diff(DiffOpts),
/// Generate many queries and compare Cargo and Guppy
Check(CheckOpts),
}
/// Global context for Cargo comparisons.
#[derive(Debug)]
pub struct GlobalContext<'g> {
home_dir: Either<TempDir, PathBuf>,
graph: &'g PackageGraph,
}
impl<'g> GlobalContext<'g> {
pub fn new(temp_home: bool, graph: &'g PackageGraph) -> Result<Self> {
let home = if temp_home {
Either::Left(TempDir::new()?)
} else {
Either::Right(env::current_dir()?)
};
Ok(Self {
home_dir: home,
graph,
})
}
pub fn home_dir(&self) -> &Path {
match &self.home_dir {
Either::Left(temp_home) => temp_home.path(),
Either::Right(home_dir) => home_dir.as_path(),
}
}
pub fn graph(&self) -> &'g PackageGraph {
self.graph
}
}