diff --git a/README.md b/README.md index f892f5f..56a47f7 100644 --- a/README.md +++ b/README.md @@ -51,10 +51,11 @@ can also be found in the project repository. ## Advanced usage -Using custom Conan profiles with names derived from the Cargo target information: +Using custom Conan profiles with names derived from the Cargo target information +and a reduced output verbosity level: ```rust -use conan2::ConanInstall; +use conan2::{ConanInstall, ConanVerbosity}; fn main() { let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap(); @@ -64,6 +65,7 @@ fn main() { ConanInstall::new() .profile(&conan_profile) .build("missing") + .verbosity(ConanVerbosity::Error) // Silence Conan warnings .run() .parse() .emit(); diff --git a/example-build-script/build.rs b/example-build-script/build.rs index 4a2bf1d..c37d9fb 100644 --- a/example-build-script/build.rs +++ b/example-build-script/build.rs @@ -1,5 +1,10 @@ -use conan2::ConanInstall; +use conan2::{ConanInstall, ConanVerbosity}; fn main() { - ConanInstall::new().build("missing").run().parse().emit(); + ConanInstall::new() + .build("missing") + .verbosity(ConanVerbosity::Error) // Silence Conan warnings + .run() + .parse() + .emit(); } diff --git a/src/lib.rs b/src/lib.rs index 89c633d..f108446 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,10 +46,11 @@ //! //! ## Advanced usage //! -//! Using custom Conan profiles with names derived from the Cargo target information: +//! Using custom Conan profiles with names derived from the Cargo target information +//! and a reduced output verbosity level: //! //! ```no_run -//! use conan2::ConanInstall; +//! use conan2::{ConanInstall, ConanVerbosity}; //! //! let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap(); //! let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap(); @@ -58,6 +59,7 @@ //! ConanInstall::new() //! .profile(&conan_profile) //! .build("missing") +//! .verbosity(ConanVerbosity::Error) // Silence Conan warnings //! .run() //! .parse() //! .emit(); @@ -95,6 +97,34 @@ const CONAN_ENV: &str = "CONAN"; /// Default Conan binary name const DEFAULT_CONAN: &str = "conan"; +/// `conan` command verbosity level +/// +/// Defines the level of detail of the Conan command output. +/// +/// Enum variants correspond to the following options: +/// `-vquiet`, `-verror`, `-vwarning`, `-vnotice`, `-vstatus`, +/// `-v` or `-vverbose`, `-vv` or `-vdebug`, `-vvv` or `-vtrace`. +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub enum ConanVerbosity { + /// `-vquiet` + Quiet, + /// `-verror` + Error, + /// `-vwarning` + #[default] + Warning, + /// `-vnotice` + Notice, + /// `-vstatus` + Status, + /// `-vverbose` + Verbose, + /// `-vdebug` + Debug, + /// `-vtrace` + Trace, +} + /// `conan install` command builder /// /// This opaque type implements a command line builder for @@ -109,6 +139,8 @@ pub struct ConanInstall { profile: Option, /// Conan build policy build: Option, + /// Conan output verbosity level + verbosity: ConanVerbosity, } /// `conan install` command output data @@ -125,6 +157,21 @@ pub struct CargoInstructions { /// Conan dependency graph as a JSON-based tree structure struct ConanDependencyGraph(Value); +impl std::fmt::Display for ConanVerbosity { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + ConanVerbosity::Quiet => f.write_str("quiet"), + ConanVerbosity::Error => f.write_str("error"), + ConanVerbosity::Warning => f.write_str("warning"), + ConanVerbosity::Notice => f.write_str("notice"), + ConanVerbosity::Status => f.write_str("status"), + ConanVerbosity::Verbose => f.write_str("verbose"), + ConanVerbosity::Debug => f.write_str("debug"), + ConanVerbosity::Trace => f.write_str("trace"), + } + } +} + impl ConanInstall { /// Creates a new `conan install` command with the default recipe path (`.`). #[must_use] @@ -170,6 +217,14 @@ impl ConanInstall { self } + /// Sets the Conan command verbosity level. + /// + /// Matches `-v` Conan executable option. + pub fn verbosity(&mut self, verbosity: ConanVerbosity) -> &mut ConanInstall { + self.verbosity = verbosity; + self + } + /// Runs the `conan install` command and captures its JSON-formatted output. /// /// # Panics @@ -191,7 +246,7 @@ impl ConanInstall { command .arg("install") .arg(recipe) - .arg("-vwarning") + .arg(format!("-v{}", self.verbosity)) .arg("--format") .arg("json") .arg("--output-folder") diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 07af595..4785dc6 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -2,12 +2,13 @@ use std::{io::Write, path::Path}; -use conan2::ConanInstall; +use conan2::{ConanInstall, ConanVerbosity}; #[test] fn run_conan_install() { let output = ConanInstall::with_recipe(Path::new("tests/conanfile.txt")) .output_folder(Path::new(env!("CARGO_TARGET_TMPDIR"))) + .verbosity(ConanVerbosity::Verbose) .build("missing") .run(); @@ -31,6 +32,7 @@ fn run_conan_install() { fn fail_no_conanfile() { let output = ConanInstall::new() .output_folder(Path::new(env!("CARGO_TARGET_TMPDIR"))) + .verbosity(ConanVerbosity::Status) .run(); std::io::stderr().write_all(output.stderr()).unwrap(); @@ -48,6 +50,7 @@ fn fail_no_profile() { let output = ConanInstall::with_recipe(Path::new("tests/conanfile.txt")) .output_folder(Path::new(env!("CARGO_TARGET_TMPDIR"))) .profile("no-such-profile") + .verbosity(ConanVerbosity::Debug) .run(); std::io::stderr().write_all(output.stderr()).unwrap();