use crate::identity::Identity;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandLine {
identity: Identity,
flattened_commands: Vec<&'static str>,
}
impl CommandLine {
#[must_use]
pub const fn identity(&self) -> Identity {
self.identity
}
#[must_use]
pub fn flattened_commands(&self) -> &[&'static str] {
&self.flattened_commands
}
#[doc(hidden)]
#[must_use]
pub fn from_dispatch(
identity: Identity,
flattened_commands: impl IntoIterator<Item = &'static str>,
) -> Self {
Self {
identity,
flattened_commands: flattened_commands.into_iter().collect(),
}
}
}
#[cfg(test)]
mod tests {
use super::CommandLine;
use crate::identity::Identity;
fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}
#[test]
fn command_line_is_send_and_sync() {
assert_send::<CommandLine>();
assert_sync::<CommandLine>();
}
#[test]
fn identity_and_flattened_commands_read_back_what_from_dispatch_was_given() {
let identity = Identity::from_macro_expansion("a-package", "a-binary", "1.2.3");
let command_line = CommandLine::from_dispatch(identity, ["add", "build"]);
assert_eq!(command_line.identity(), identity);
assert_eq!(command_line.flattened_commands(), ["add", "build"]);
}
#[test]
fn flattened_commands_is_empty_when_nothing_was_given() {
let identity = Identity::from_macro_expansion("a-package", "a-binary", "1.2.3");
let command_line = CommandLine::from_dispatch(identity, []);
assert!(command_line.flattened_commands().is_empty());
}
}