use std::collections::{BTreeSet, HashSet};
use crate::tree::{Route, single_alias};
pub(crate) fn reject_host_words(generated: &usage::Spec, updates: bool) -> Result<(), String> {
let leaf = subspace_product::reserved_leaf_words(updates);
let group = subspace_product::reserved_group_words(updates);
let taken = generated
.cmd
.subcommands
.iter()
.find_map(|(name, command)| {
let reserved = if command.subcommands.is_empty() {
&leaf
} else {
&group
};
std::iter::once(name)
.chain(&command.aliases)
.chain(&command.hidden_aliases)
.find(|word| reserved.contains(&word.as_str()))
});
match taken {
Some(name) => Err(format!(
"Generated command collides with a reserved root word: {name}"
)),
None => Ok(()),
}
}
pub(crate) fn reject_flag_overlap(
product: &usage::Spec,
generated: &usage::Spec,
) -> Result<(), String> {
let taken: BTreeSet<String> = command_flags(&product.cmd).flat_map(flag_keys).collect();
let mut commands: Vec<&usage::SpecCommand> = generated.cmd.subcommands.values().collect();
while let Some(command) = commands.pop() {
commands.extend(command.subcommands.values());
if let Some(key) = command_flags(command)
.flat_map(flag_keys)
.find(|key| taken.contains(key))
{
return Err(format!(
"Generated command flag collides with a product flag: {key}"
));
}
}
Ok(())
}
fn command_flags(command: &usage::SpecCommand) -> impl Iterator<Item = &usage::SpecFlag> {
command
.flags
.iter()
.chain(command.clause.iter().flat_map(|clause| &clause.flags))
}
fn flag_keys(flag: &usage::SpecFlag) -> Vec<String> {
flag.long
.iter()
.chain(&flag.hidden_aliases)
.map(|long| format!("--{long}"))
.chain(
flag.short
.iter()
.chain(&flag.hidden_short_aliases)
.map(|short| format!("-{short}")),
)
.chain(flag.negate.clone())
.chain(std::iter::once(flag.name.clone()))
.collect()
}
pub(crate) fn merge_spec(spec: &mut usage::Spec, extra: usage::Spec) -> Result<(), String> {
let existing = root_names(spec);
let generated = root_names(&extra);
if let Some(name) = existing.intersection(&generated).next() {
return Err(format!(
"Generated command collides with existing command: {name}"
));
}
spec.cmd.subcommands.extend(extra.cmd.subcommands);
spec.restamp();
Ok(())
}
fn root_names(spec: &usage::Spec) -> BTreeSet<&String> {
spec.cmd
.subcommands
.iter()
.flat_map(|(name, command)| {
std::iter::once(name)
.chain(&command.aliases)
.chain(&command.hidden_aliases)
})
.collect()
}
pub(crate) fn validate_names(routes: &[Route]) -> Result<(), String> {
let spellings: Vec<Vec<String>> = routes.iter().flat_map(spellings).collect();
if let Some(word) = flag_like(&spellings) {
return Err(format!("Generated command name reads as a flag: {word}"));
}
match first_collision(&spellings) {
Some(name) => Err(format!("Generated command collision: {name}")),
None => Ok(()),
}
}
fn flag_like(spellings: &[Vec<String>]) -> Option<&String> {
spellings
.iter()
.flatten()
.find(|word| word.starts_with('-'))
}
fn spellings(route: &Route) -> Vec<Vec<String>> {
let mut spellings = vec![route.path.clone()];
if let Some(action) = &route.action
&& Some(action) != route.path.last()
{
let mut alias = route.path.clone();
alias.pop();
alias.push(action.clone());
spellings.push(alias);
}
if let Some(alias) = single_alias(route) {
spellings.push(vec![alias.to_owned()]);
}
spellings
}
fn first_collision(spellings: &[Vec<String>]) -> Option<String> {
let groups: HashSet<&str> = spellings
.iter()
.filter(|path| path.len() > 1)
.map(|path| path[0].as_str())
.collect();
let mut seen = HashSet::new();
spellings.iter().find_map(|path| {
let name = path.join(" ");
let collides = path.iter().any(String::is_empty)
|| (path.len() == 1 && groups.contains(name.as_str()))
|| !seen.insert(name.clone());
collides.then_some(name)
})
}