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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
use crate::{SwitchCommon, SwitchShape, Switches}; use std::collections::{HashMap, HashSet}; use std::fmt::{self, Display}; #[derive(PartialEq, Eq, Debug, Default)] pub struct Invalid { pub duplicate_shorts: Vec<String>, pub duplicate_longs: Vec<String>, pub one_char_longs: Vec<String>, pub multi_char_shorts: Vec<String>, pub has_empty_switch: bool, } impl Invalid { fn new( duplicate_shorts: Vec<String>, duplicate_longs: Vec<String>, one_char_longs: Vec<String>, multi_char_shorts: Vec<String>, has_empty_switch: bool, ) -> Option<Self> { if duplicate_shorts.is_empty() && duplicate_longs.is_empty() && one_char_longs.is_empty() && multi_char_shorts.is_empty() && !has_empty_switch { None } else { Some(Self { duplicate_shorts, duplicate_longs, one_char_longs, multi_char_shorts, has_empty_switch, }) } } } impl Display for Invalid { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { assert!( !(self.duplicate_shorts.is_empty() && self.duplicate_longs.is_empty() && self.one_char_longs.is_empty() && self.multi_char_shorts.is_empty() && !self.has_empty_switch) ); write!(f, "Invalid argument spec!\n\n")?; if !self.duplicate_shorts.is_empty() { write!(f, "Duplicate shorts: \n{:#?}", self.duplicate_shorts)?; } if !self.duplicate_longs.is_empty() { write!(f, "Duplicate longs: \n{:#?}", self.duplicate_longs)?; } if !self.one_char_longs.is_empty() { write!(f, "Single-character longs: \n{:#?}", self.one_char_longs)?; } if !self.multi_char_shorts.is_empty() { write!(f, "Multi-character shorts: \n{:#?}", self.multi_char_shorts)?; } if self.has_empty_switch { write!(f, "Argument specified with neither short nor long switch")?; } Ok(()) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] struct SwitchInfo { common: SwitchCommon, arity: SwitchShape, } #[derive(Default, Debug)] struct SwitchTable { table: HashMap<String, HashSet<SwitchInfo>>, } impl SwitchTable { fn insert(&mut self, key: &str, info: SwitchInfo) { self.table .entry(key.to_string()) .or_insert_with(HashSet::default) .insert(info); } fn keys_with_multiple_values(&self) -> impl Iterator<Item = String> + '_ { self.table.iter().filter_map( |(k, v)| { if v.len() > 1 { Some(k.clone()) } else { None } }, ) } } #[derive(Default, Debug)] struct InvalidNames { one_char_longs: Vec<String>, multi_char_shorts: Vec<String>, has_empty_switch: bool, } impl InvalidNames { fn insert(&mut self, info: &SwitchInfo) { let short = info.common.short.as_str(); let long = info.common.long.as_str(); if short.len() > 1 { self.multi_char_shorts.push(short.to_string()); } if long.len() == 1 { self.one_char_longs.push(long.to_string()); } if short.len() == 0 && long.len() == 0 { self.has_empty_switch = true; } } } #[derive(Default, Debug)] struct Duplicates { by_short: SwitchTable, by_long: SwitchTable, } impl Duplicates { fn insert(&mut self, info: SwitchInfo) { let short = info.common.short.clone(); let long = info.common.long.clone(); if short.len() > 0 { self.by_short.insert(&short, info.clone()); } if long.len() > 0 { self.by_long.insert(&long, info); } } } #[derive(Default, Debug)] pub struct Checker { duplicates: Duplicates, invalid_names: InvalidNames, } impl Checker { fn insert(&mut self, info: SwitchInfo) { self.invalid_names.insert(&info); self.duplicates.insert(info); } pub fn invalid(self) -> Option<Invalid> { let Checker { duplicates: Duplicates { by_short, by_long }, invalid_names: InvalidNames { one_char_longs, multi_char_shorts, has_empty_switch, }, } = self; let duplicate_shorts = by_short.keys_with_multiple_values().collect::<Vec<_>>(); let duplicate_longs = by_long.keys_with_multiple_values().collect::<Vec<_>>(); Invalid::new( duplicate_shorts, duplicate_longs, one_char_longs, multi_char_shorts, has_empty_switch, ) } } impl Switches for Checker { fn add(&mut self, common: SwitchCommon, arity: SwitchShape) { self.insert(SwitchInfo { common, arity }); } }