use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use super::SubCommand;
use crate::global::GlobalOpts;
use crate::spawn::ParameterizedSpawn;
#[cfg_attr(
feature = "lt2015_1",
doc = "`p4 [g-opts] counters [-e nameFilter -m max]`"
)]
#[cfg_attr(
not(feature = "lt2015_1"),
doc = "`p4 [g-opts] counters [-e nameFilter] [-m max]`"
)]
#[derive(Debug, Clone, Default)]
pub struct Counters {
bin: PathBuf,
global_opts: GlobalOpts,
filter: Option<String>,
limit: Option<u64>,
}
impl Counters {
pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
Self {
bin: bin.into(),
global_opts,
filter: None,
limit: None,
}
}
pub fn get_bin(&self) -> &Path {
&self.bin
}
pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
self.bin = bin.into();
self
}
pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
self.set_bin(bin);
self
}
#[cfg_attr(
feature = "lt2014_2",
doc = "See the [Global Options](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2015_1", not(feature = "lt2014_2")),
doc = "See the [“Global Options”](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2017_1", not(feature = "lt2015_1")),
doc = "See [“Global Options”](GlobalOpts)."
)]
#[cfg_attr(
all(feature = "lt2018_2", not(feature = "lt2017_1")),
doc = "See [Global Options](GlobalOpts)."
)]
#[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
pub fn get_global_opts(&self) -> &GlobalOpts {
&self.global_opts
}
#[cfg_attr(
feature = "lt2014_2",
doc = "See the [Global Options](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2015_1", not(feature = "lt2014_2")),
doc = "See the [“Global Options”](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2017_1", not(feature = "lt2015_1")),
doc = "See [“Global Options”](GlobalOpts)."
)]
#[cfg_attr(
all(feature = "lt2018_2", not(feature = "lt2017_1")),
doc = "See [Global Options](GlobalOpts)."
)]
#[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
self.global_opts = v;
self
}
#[cfg_attr(
feature = "lt2014_2",
doc = "See the [Global Options](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2015_1", not(feature = "lt2014_2")),
doc = "See the [“Global Options”](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2017_1", not(feature = "lt2015_1")),
doc = "See [“Global Options”](GlobalOpts)."
)]
#[cfg_attr(
all(feature = "lt2018_2", not(feature = "lt2017_1")),
doc = "See [Global Options](GlobalOpts)."
)]
#[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
pub fn global_opts(mut self, v: GlobalOpts) -> Self {
self.global_opts = v;
self
}
pub fn get_filter(&self) -> Option<&str> {
self.filter.as_deref()
}
pub fn set_filter(&mut self, v: impl Into<String>) -> &mut Self {
self.filter = Some(v.into());
self
}
pub fn filter(mut self, v: impl Into<String>) -> Self {
self.set_filter(v);
self
}
pub fn get_limit(&self) -> Option<u64> {
self.limit
}
pub fn set_limit(&mut self, v: u64) -> &mut Self {
self.limit = Some(v);
self
}
pub fn limit(mut self, v: u64) -> Self {
self.set_limit(v);
self
}
}
impl SubCommand for Counters {
fn name(&self) -> &str {
"counters"
}
fn inject_local_args(&self, command: &mut Command) {
if let Some(filter) = &self.filter {
command.arg("-e").arg(filter);
}
if let Some(max) = self.limit {
command.arg("-m").arg(max.to_string());
}
}
fn global_opts(&self) -> Option<&GlobalOpts> {
Some(&self.global_opts)
}
}
impl ParameterizedSpawn<()> for Counters {
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
self.setup_command(&self.bin)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cmd::args_of;
#[test]
fn bin_accessors_update_executable_path() {
let mut command = Counters::new("p4", GlobalOpts::new());
assert_eq!(command.get_bin(), Path::new("p4"));
command.set_bin("/usr/local/bin/p4");
assert_eq!(command.get_bin(), Path::new("/usr/local/bin/p4"));
let command = command.bin("p4.exe");
assert_eq!(command.get_bin(), Path::new("p4.exe"));
}
#[test]
fn without_options() {
let counters = Counters::new("p4", GlobalOpts::new());
assert_eq!(args_of(&counters.setup_command("p4")), ["counters"]);
}
#[test]
fn with_filter() {
let counters = Counters::new("p4", GlobalOpts::new()).filter("mycounter-*");
assert_eq!(counters.get_filter(), Some("mycounter-*"));
assert_eq!(
args_of(&counters.setup_command("p4")),
["counters", "-e", "mycounter-*"]
);
}
#[test]
fn with_limit() {
let counters = Counters::new("p4", GlobalOpts::new()).limit(5);
assert_eq!(counters.get_limit(), Some(5));
assert_eq!(
args_of(&counters.setup_command("p4")),
["counters", "-m", "5"]
);
}
#[test]
fn filter_and_limit_in_syntax_order() {
let mut counters = Counters::new("p4", GlobalOpts::new()).limit(5);
counters.set_filter("mycounter-*");
assert_eq!(
args_of(&counters.setup_command("p4")),
["counters", "-e", "mycounter-*", "-m", "5"]
);
}
#[test]
fn global_opts_are_injected_once() {
let counters = Counters::new(
"p4",
GlobalOpts::new().port("localhost:1666").quiet_mode(true),
)
.filter("mycounter-*");
assert_eq!(
args_of(&counters.setup_command("p4")),
[
"-p",
"localhost:1666",
"-q",
"counters",
"-e",
"mycounter-*"
]
);
}
}