| 1 | //! clap number parsers. |
| 2 | //! |
| 3 | //! This crate contains functions to validate and parse numerical values from |
| 4 | //! strings provided by [clap]. |
| 5 | //! |
| 6 | //! * `maybe_hex` |
| 7 | //! Validates an unsigned integer value that can be base-10 or base-16. |
| 8 | //! * `maybe_hex_range` |
| 9 | //! Validates an unsigned integer value that can be base-10 or base-16 within a range. |
| 10 | //! * `number_range` |
| 11 | //! Validate a signed or unsigned integer value. |
| 12 | //! * `si_number` |
| 13 | //! Validate a signed or unsigned integer value with a metric prefix. |
| 14 | //! * `si_number_range` |
| 15 | //! Validate a signed or unsigned integer value with a metric prefix within a range. |
| 16 | //! |
| 17 | //! [clap]: https://github.com/clap-rs/clap |
| 18 | #![deny(missing_docs)] |
| 19 | |
| 20 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::{iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/, strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#}; |
| 21 | use num_traitsrust-analyzer cargo num-traits 0.2.19 crate/::identitiesrust-analyzer cargo num-traits 0.2.19 identities/::Zerorust-analyzer cargo num-traits 0.2.19 identities/Zero#; |
| 22 | use num_traitsrust-analyzer cargo num-traits 0.2.19 crate/::{signrust-analyzer cargo num-traits 0.2.19 sign/, CheckedAddrust-analyzer cargo num-traits 0.2.19 ops/checked/CheckedAdd#, CheckedMulrust-analyzer cargo num-traits 0.2.19 ops/checked/CheckedMul#, CheckedSubrust-analyzer cargo num-traits 0.2.19 ops/checked/CheckedSub#, Numrust-analyzer cargo num-traits 0.2.19 Num#}; |
| 23 | |
| 24 | fn check_rangerust-analyzer cargo clap-num 1.2.0 check_range().<T>(val: T, min: T, max: T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> |
| 25 | where |
| 26 | T: FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#, |
| 27 | <T as FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#>::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 28 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 29 | T: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 30 | { |
| 31 | if val rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().max { |
| 32 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("exceeds maximum of {max}")) |
| 33 | } else if val rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().min { |
| 34 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("less than minimum of {min}")) |
| 35 | } else { |
| 36 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(val) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// Validate a signed or unsigned integer value. |
| 41 | /// |
| 42 | /// # Arguments |
| 43 | /// |
| 44 | /// * `s` - String to parse. |
| 45 | /// * `min` - Minimum value, inclusive. |
| 46 | /// * `max` - Maximum value, inclusive. |
| 47 | /// |
| 48 | /// # Example |
| 49 | /// |
| 50 | /// This allows for a number of cents to be passed in the range of 0-99 |
| 51 | /// (inclusive). |
| 52 | /// |
| 53 | /// ``` |
| 54 | /// use clap::Parser; |
| 55 | /// use clap_num::number_range; |
| 56 | /// |
| 57 | /// fn less_than_100(s: &str) -> Result<u8, String> { |
| 58 | /// number_range(s, 0, 99) |
| 59 | /// } |
| 60 | /// |
| 61 | /// #[derive(Parser)] |
| 62 | /// struct Change { |
| 63 | /// #[clap(long, value_parser=less_than_100)] |
| 64 | /// cents: u8, |
| 65 | /// } |
| 66 | /// # let args = Change::parse_from(&["", "--cents", "99"]); |
| 67 | /// # assert_eq!(args.cents, 99); |
| 68 | /// ``` |
| 69 | /// |
| 70 | /// To run this example run `cargo run --example change`, giving arguments to |
| 71 | /// the program after `--`, for example: |
| 72 | /// |
| 73 | /// ```text |
| 74 | /// $ cargo run --example change -- --cents 99 |
| 75 | /// Change: 99 cents |
| 76 | /// ``` |
| 77 | /// |
| 78 | /// ## Error Messages |
| 79 | /// |
| 80 | /// Values that are not numbers will show an error message similar to this: |
| 81 | /// |
| 82 | /// ```text |
| 83 | /// error: Invalid value for '--cents <cents>': invalid digit found in string |
| 84 | /// ``` |
| 85 | /// |
| 86 | /// Values resulting in integer overflow will show an error message similar to this: |
| 87 | /// |
| 88 | /// ```text |
| 89 | /// error: Invalid value for '--cents <cents>': number too large to fit in target type |
| 90 | /// ``` |
| 91 | /// |
| 92 | /// Values exceeding the limits will show an error message similar to this: |
| 93 | /// |
| 94 | /// ```text |
| 95 | /// error: Invalid value for '--cents <cents>': exceeds maximum of 99 |
| 96 | /// ``` |
| 97 | pub fn number_rangerust-analyzer cargo clap-num 1.2.0 number_range().<T>(s: &str, min: T, max: T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> |
| 98 | where |
| 99 | T: FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#, |
| 100 | <T as FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#>::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 101 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 102 | T: PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#, |
| 103 | T: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 104 | { |
| 105 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(min <= max, "minimum of {min} exceeds maximum of {max}"); |
| 106 | let val = s.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<T>().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(stringifyrust-analyzer cargo clap-num 1.2.0 stringify().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 107 | check_rangerust-analyzer cargo clap-num 1.2.0 check_range().(val, min, max) |
| 108 | } |
| 109 | |
| 110 | static OVERFLOW_MSGrust-analyzer cargo clap-num 1.2.0 OVERFLOW_MSG.: &str = "number too large to fit in target type"; |
| 111 | |
| 112 | // helper for mapping errors to strings |
| 113 | fn stringifyrust-analyzer cargo clap-num 1.2.0 stringify().<T: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#>(e: T) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 114 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{e}") |
| 115 | } |
| 116 | |
| 117 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 118 | enum SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix# { |
| 119 | Yottarust-analyzer cargo clap-num 1.2.0 SiPrefix#Yotta#, |
| 120 | Zettarust-analyzer cargo clap-num 1.2.0 SiPrefix#Zetta#, |
| 121 | Exarust-analyzer cargo clap-num 1.2.0 SiPrefix#Exa#, |
| 122 | Petarust-analyzer cargo clap-num 1.2.0 SiPrefix#Peta#, |
| 123 | Terarust-analyzer cargo clap-num 1.2.0 SiPrefix#Tera#, |
| 124 | Gigarust-analyzer cargo clap-num 1.2.0 SiPrefix#Giga#, |
| 125 | Megarust-analyzer cargo clap-num 1.2.0 SiPrefix#Mega#, |
| 126 | Kilorust-analyzer cargo clap-num 1.2.0 SiPrefix#Kilo#, |
| 127 | } |
| 128 | |
| 129 | impl SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix# { |
| 130 | fn from_charrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]from_char().(symbol: char) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]> { |
| 131 | match symbol { |
| 132 | 'Y' => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]::Yottarust-analyzer cargo clap-num 1.2.0 SiPrefix#Yotta#), |
| 133 | 'Z' => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]::Zettarust-analyzer cargo clap-num 1.2.0 SiPrefix#Zetta#), |
| 134 | 'E' => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]::Exarust-analyzer cargo clap-num 1.2.0 SiPrefix#Exa#), |
| 135 | 'P' => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]::Petarust-analyzer cargo clap-num 1.2.0 SiPrefix#Peta#), |
| 136 | 'T' => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]::Terarust-analyzer cargo clap-num 1.2.0 SiPrefix#Tera#), |
| 137 | 'G' => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]::Gigarust-analyzer cargo clap-num 1.2.0 SiPrefix#Giga#), |
| 138 | 'M' => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]::Megarust-analyzer cargo clap-num 1.2.0 SiPrefix#Mega#), |
| 139 | 'k' | 'K' => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]::Kilorust-analyzer cargo clap-num 1.2.0 SiPrefix#Kilo#), |
| 140 | _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | fn multiplierrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]multiplier().(&self) -> u128 { |
| 145 | match self { |
| 146 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Yottarust-analyzer cargo clap-num 1.2.0 SiPrefix#Yotta# => 1_000_000_000_000_000_000_000_000, |
| 147 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Zettarust-analyzer cargo clap-num 1.2.0 SiPrefix#Zetta# => 1_000_000_000_000_000_000_000, |
| 148 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Exarust-analyzer cargo clap-num 1.2.0 SiPrefix#Exa# => 1_000_000_000_000_000_000, |
| 149 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Petarust-analyzer cargo clap-num 1.2.0 SiPrefix#Peta# => 1_000_000_000_000_000, |
| 150 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Terarust-analyzer cargo clap-num 1.2.0 SiPrefix#Tera# => 1_000_000_000_000, |
| 151 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Gigarust-analyzer cargo clap-num 1.2.0 SiPrefix#Giga# => 1_000_000_000, |
| 152 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Megarust-analyzer cargo clap-num 1.2.0 SiPrefix#Mega# => 1_000_000, |
| 153 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Kilorust-analyzer cargo clap-num 1.2.0 SiPrefix#Kilo# => 1_000, |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | fn digitsrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]digits().(&self) -> usize { |
| 158 | match self { |
| 159 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Yottarust-analyzer cargo clap-num 1.2.0 SiPrefix#Yotta# => 24, |
| 160 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Zettarust-analyzer cargo clap-num 1.2.0 SiPrefix#Zetta# => 21, |
| 161 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Exarust-analyzer cargo clap-num 1.2.0 SiPrefix#Exa# => 18, |
| 162 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Petarust-analyzer cargo clap-num 1.2.0 SiPrefix#Peta# => 15, |
| 163 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Terarust-analyzer cargo clap-num 1.2.0 SiPrefix#Tera# => 12, |
| 164 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Gigarust-analyzer cargo clap-num 1.2.0 SiPrefix#Giga# => 9, |
| 165 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Megarust-analyzer cargo clap-num 1.2.0 SiPrefix#Mega# => 6, |
| 166 | SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::Kilorust-analyzer cargo clap-num 1.2.0 SiPrefix#Kilo# => 3, |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | fn parse_postrust-analyzer cargo clap-num 1.2.0 parse_post().<T>(mut post: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, digits: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> |
| 172 | where |
| 173 | <T as FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#>::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 174 | T: PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd# + FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#, |
| 175 | { |
| 176 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(zeros) = digits.checked_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_sub().(post.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().()) { |
| 177 | post.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Extend<char>`]extend().(iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::repeatrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/repeat/repeat().('0').takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(zeros)); |
| 178 | post.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<T>().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(stringifyrust-analyzer cargo clap-num 1.2.0 stringify().) |
| 179 | } else { |
| 180 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().("not an integer")) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /// Validate a signed or unsigned integer value with a [metric prefix]. |
| 185 | /// |
| 186 | /// This can accept strings with the (case sensitive) SI symbols. |
| 187 | /// |
| 188 | /// | Symbol | Name | Value | |
| 189 | /// |--------|-------|-----------------------------------| |
| 190 | /// | Y | yotta | 1_000_000_000_000_000_000_000_000 | |
| 191 | /// | Z | zetta | 1_000_000_000_000_000_000_000 | |
| 192 | /// | E | exa | 1_000_000_000_000_000_000 | |
| 193 | /// | P | peta | 1_000_000_000_000_000 | |
| 194 | /// | T | tera | 1_000_000_000_000 | |
| 195 | /// | G | giga | 1_000_000_000 | |
| 196 | /// | M | mega | 1_000_000 | |
| 197 | /// | k | kilo | 1_000 | |
| 198 | /// |
| 199 | /// The strings can be provided with a decimal, or using the SI symbol as the |
| 200 | /// decimal separator. |
| 201 | /// |
| 202 | /// | String | Value | |
| 203 | /// |--------|-----------| |
| 204 | /// | 3k3 | 3300 | |
| 205 | /// | 3.3k | 3300 | |
| 206 | /// | 1M | 1_000_000 | |
| 207 | /// |
| 208 | /// # Example |
| 209 | /// |
| 210 | /// This allows for resistance value to be passed using SI symbols. |
| 211 | /// |
| 212 | /// ``` |
| 213 | /// use clap::Parser; |
| 214 | /// use clap_num::si_number; |
| 215 | /// |
| 216 | /// #[derive(Parser)] |
| 217 | /// struct Args { |
| 218 | /// #[clap(short, long, value_parser=si_number::<u128>)] |
| 219 | /// resistance: u128, |
| 220 | /// } |
| 221 | /// # let args = Args::parse_from(&["", "--resistance", "1M1"]); |
| 222 | /// # assert_eq!(args.resistance, 1_100_000); |
| 223 | /// ``` |
| 224 | /// |
| 225 | /// To run this example use `cargo run --example resistance`, giving arguments |
| 226 | /// to the program after `--`, for example: |
| 227 | /// |
| 228 | /// ```text |
| 229 | /// $ cargo run --example resistance -- --resistance 1M1 |
| 230 | /// Resistance: 1100000 ohms |
| 231 | /// ``` |
| 232 | /// |
| 233 | /// [metric prefix]: https://en.wikipedia.org/wiki/Metric_prefix |
| 234 | pub fn si_numberrust-analyzer cargo clap-num 1.2.0 si_number().<T>(s: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> |
| 235 | where |
| 236 | <T as TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<u128>>::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#Error#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 237 | <T as FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#>::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 238 | T: CheckedAddrust-analyzer cargo num-traits 0.2.19 ops/checked/CheckedAdd#, |
| 239 | T: CheckedMulrust-analyzer cargo num-traits 0.2.19 ops/checked/CheckedMul#, |
| 240 | T: CheckedSubrust-analyzer cargo num-traits 0.2.19 ops/checked/CheckedSub#, |
| 241 | T: FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#, |
| 242 | T: PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#, |
| 243 | T: TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<u128>, |
| 244 | T: Zerorust-analyzer cargo num-traits 0.2.19 identities/Zero#, |
| 245 | { |
| 246 | // contains SI symbol |
| 247 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(si_prefix_index) = s.findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().(|c| SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::from_charrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]from_char().(c).is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().()) { |
| 248 | let si_prefix = SiPrefixrust-analyzer cargo clap-num 1.2.0 SiPrefix#::from_charrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]from_char().(s.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().si_prefix_index]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as char).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 249 | let multiplier: T = T::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#try_from().(si_prefix.multiplierrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]multiplier().()).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| OVERFLOW_MSGrust-analyzer cargo clap-num 1.2.0 OVERFLOW_MSG.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 250 | |
| 251 | let (pre_si, post_si) = s.split_atrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_at().(si_prefix_index); |
| 252 | let post_si = &post_si[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 253 | |
| 254 | if pre_si.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 255 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#("no value found before SI symbol".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 256 | } |
| 257 | |
| 258 | // in the format of "1k234" for 1_234 |
| 259 | let (pre, post) = if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().post_si.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 260 | ( |
| 261 | pre_si.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<T>().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(stringifyrust-analyzer cargo clap-num 1.2.0 stringify().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 262 | parse_postrust-analyzer cargo clap-num 1.2.0 parse_post().(post_si.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), si_prefix.digitsrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]digits().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 263 | ) |
| 264 | |
| 265 | // in the format of "1.234k" for 1_234 |
| 266 | } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((pre_dec, post_dec)) = s.split_oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_once().('.') { |
| 267 | let mut post_dec: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# = post_dec.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 268 | post_dec.poprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]pop().(); // remove SI symbol |
| 269 | let post_dec = parse_postrust-analyzer cargo clap-num 1.2.0 parse_post().(post_dec, si_prefix.digitsrust-analyzer cargo clap-num 1.2.0 impl#[SiPrefix]digits().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 270 | (pre_dec.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<T>().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(stringifyrust-analyzer cargo clap-num 1.2.0 stringify().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., post_dec) |
| 271 | |
| 272 | // no decimal |
| 273 | } else { |
| 274 | (pre_si.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<T>().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(stringifyrust-analyzer cargo clap-num 1.2.0 stringify().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., T::zerorust-analyzer cargo num-traits 0.2.19 identities/Zero#zero().()) |
| 275 | }; |
| 276 | |
| 277 | let pre = pre.checked_mulrust-analyzer cargo num-traits 0.2.19 ops/checked/CheckedMul#checked_mul().(&multiplier).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(OVERFLOW_MSGrust-analyzer cargo clap-num 1.2.0 OVERFLOW_MSG.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 278 | |
| 279 | if pre rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#ge().T::zerorust-analyzer cargo num-traits 0.2.19 identities/Zero#zero().() { |
| 280 | pre.checked_addrust-analyzer cargo num-traits 0.2.19 ops/checked/CheckedAdd#checked_add().(&post) |
| 281 | } else { |
| 282 | pre.checked_subrust-analyzer cargo num-traits 0.2.19 ops/checked/CheckedSub#checked_sub().(&post) |
| 283 | } |
| 284 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| OVERFLOW_MSGrust-analyzer cargo clap-num 1.2.0 OVERFLOW_MSG..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()) |
| 285 | } else { |
| 286 | // no SI symbol, parse normally |
| 287 | s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() |
| 288 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|&c| c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne().'_') |
| 289 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>() |
| 290 | .parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<T>() |
| 291 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(stringifyrust-analyzer cargo clap-num 1.2.0 stringify().) |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /// Validate a signed or unsigned integer value with a [metric prefix] within |
| 296 | /// a range. |
| 297 | /// |
| 298 | /// This combines [`si_number`] and [`number_range`], see the |
| 299 | /// documentation for those functions for details. |
| 300 | /// |
| 301 | /// # Example |
| 302 | /// |
| 303 | /// This extends the example in [`si_number`], and only allows a range of |
| 304 | /// resistances from 1k to 999.999k. |
| 305 | /// |
| 306 | /// ``` |
| 307 | /// use clap::Parser; |
| 308 | /// use clap_num::si_number_range; |
| 309 | /// |
| 310 | /// fn kilo(s: &str) -> Result<u32, String> { |
| 311 | /// si_number_range(s, 1_000, 999_999) |
| 312 | /// } |
| 313 | /// |
| 314 | /// #[derive(Parser)] |
| 315 | /// struct Args { |
| 316 | /// #[clap(short, long, value_parser=kilo)] |
| 317 | /// resistance: u32, |
| 318 | /// } |
| 319 | /// # let args = Args::parse_from(&["", "--resistance", "999k999"]); |
| 320 | /// # assert_eq!(args.resistance, 999_999); |
| 321 | /// ``` |
| 322 | /// |
| 323 | /// [metric prefix]: https://en.wikipedia.org/wiki/Metric_prefix |
| 324 | pub fn si_number_rangerust-analyzer cargo clap-num 1.2.0 si_number_range().<T>(s: &str, min: T, max: T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> |
| 325 | where |
| 326 | <T as TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<u128>>::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#Error#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 327 | <T as FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#>::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 328 | T: CheckedAddrust-analyzer cargo num-traits 0.2.19 ops/checked/CheckedAdd#, |
| 329 | T: CheckedMulrust-analyzer cargo num-traits 0.2.19 ops/checked/CheckedMul#, |
| 330 | T: CheckedSubrust-analyzer cargo num-traits 0.2.19 ops/checked/CheckedSub#, |
| 331 | T: FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#, |
| 332 | T: PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#, |
| 333 | T: TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<u128>, |
| 334 | T: Zerorust-analyzer cargo num-traits 0.2.19 identities/Zero#, |
| 335 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 336 | T: PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#, |
| 337 | T: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 338 | { |
| 339 | let val = si_numberrust-analyzer cargo clap-num 1.2.0 si_number().(s)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 340 | check_rangerust-analyzer cargo clap-num 1.2.0 check_range().(val, min, max) |
| 341 | } |
| 342 | |
| 343 | /// Validates an unsigned integer value that can be base-10 or base-16. |
| 344 | /// |
| 345 | /// The number is assumed to be base-10 by default, it is parsed as hex if the |
| 346 | /// number is prefixed with `0x`, case insensitive. |
| 347 | /// |
| 348 | /// # Example |
| 349 | /// |
| 350 | /// This allows base-10 addresses to be passed normally, or base-16 values to |
| 351 | /// be passed when prefixed with `0x`. |
| 352 | /// |
| 353 | /// ``` |
| 354 | /// use clap::Parser; |
| 355 | /// use clap_num::maybe_hex; |
| 356 | /// |
| 357 | /// #[derive(Parser)] |
| 358 | /// struct Args { |
| 359 | /// #[clap(short, long, value_parser=maybe_hex::<u32>)] |
| 360 | /// address: u32, |
| 361 | /// } |
| 362 | /// # let args = Args::parse_from(&["", "-a", "0x10"]); |
| 363 | /// # assert_eq!(args.address, 16); |
| 364 | /// ``` |
| 365 | pub fn maybe_hexrust-analyzer cargo clap-num 1.2.0 maybe_hex().<T: Numrust-analyzer cargo num-traits 0.2.19 Num# + signrust-analyzer cargo num-traits 0.2.19 sign/::Unsignedrust-analyzer cargo num-traits 0.2.19 sign/Unsigned#>(s: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> |
| 366 | where |
| 367 | <T as Numrust-analyzer cargo num-traits 0.2.19 Num#>::FromStrRadixErrrust-analyzer cargo num-traits 0.2.19 Num#FromStrRadixErr#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 368 | { |
| 369 | const HEX_PREFIXrust-analyzer cargo clap-num 1.2.0 HEX_PREFIX.: &str = "0x"; |
| 370 | const HEX_PREFIX_UPPERrust-analyzer cargo clap-num 1.2.0 HEX_PREFIX_UPPER.: &str = "0X"; |
| 371 | const HEX_PREFIX_LENrust-analyzer cargo clap-num 1.2.0 HEX_PREFIX_LEN.: usize = HEX_PREFIXrust-analyzer cargo clap-num 1.2.0 HEX_PREFIX..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().(); |
| 372 | |
| 373 | let result = if s.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(HEX_PREFIXrust-analyzer cargo clap-num 1.2.0 HEX_PREFIX.) || s.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(HEX_PREFIX_UPPERrust-analyzer cargo clap-num 1.2.0 HEX_PREFIX_UPPER.) { |
| 374 | T::from_str_radixrust-analyzer cargo num-traits 0.2.19 Num#from_str_radix().(&s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().HEX_PREFIX_LENrust-analyzer cargo clap-num 1.2.0 HEX_PREFIX_LEN...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., 16) |
| 375 | } else { |
| 376 | T::from_str_radixrust-analyzer cargo num-traits 0.2.19 Num#from_str_radix().(s, 10) |
| 377 | }; |
| 378 | |
| 379 | result.map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(stringifyrust-analyzer cargo clap-num 1.2.0 stringify().) |
| 380 | } |
| 381 | |
| 382 | /// Validates an unsigned integer value that can be base-10 or base-16 within |
| 383 | /// a range. |
| 384 | /// |
| 385 | /// This combines [`maybe_hex`] and [`number_range`], see the |
| 386 | /// documentation for those functions for details. |
| 387 | /// |
| 388 | /// # Example |
| 389 | /// |
| 390 | /// This extends the example in [`maybe_hex`], and only allows a range of |
| 391 | /// addresses from `0x100` to `0x200`. |
| 392 | /// |
| 393 | /// ``` |
| 394 | /// use clap::Parser; |
| 395 | /// use clap_num::maybe_hex_range; |
| 396 | /// |
| 397 | /// fn address_in_range(s: &str) -> Result<u32, String> { |
| 398 | /// maybe_hex_range(s, 0x100, 0x200) |
| 399 | /// } |
| 400 | /// |
| 401 | /// #[derive(Parser)] |
| 402 | /// struct Args { |
| 403 | /// #[clap(short, long, value_parser=address_in_range)] |
| 404 | /// address: u32, |
| 405 | /// } |
| 406 | /// # let args = Args::parse_from(&["", "-a", "300"]); |
| 407 | /// # assert_eq!(args.address, 300); |
| 408 | /// ``` |
| 409 | pub fn maybe_hex_rangerust-analyzer cargo clap-num 1.2.0 maybe_hex_range().<T>(s: &str, min: T, max: T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> |
| 410 | where |
| 411 | <T as Numrust-analyzer cargo num-traits 0.2.19 Num#>::FromStrRadixErrrust-analyzer cargo num-traits 0.2.19 Num#FromStrRadixErr#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 412 | <T as FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#>::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 413 | T: FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#, |
| 414 | T: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 415 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 416 | T: Numrust-analyzer cargo num-traits 0.2.19 Num#, |
| 417 | T: signrust-analyzer cargo num-traits 0.2.19 sign/::Unsignedrust-analyzer cargo num-traits 0.2.19 sign/Unsigned#, |
| 418 | { |
| 419 | let val = maybe_hexrust-analyzer cargo clap-num 1.2.0 maybe_hex().(s)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 420 | check_rangerust-analyzer cargo clap-num 1.2.0 check_range().(val, min, max) |
| 421 | } |
| 422 | |
| 423 | /// Validates an unsigned integer value that can be base-10 or base-2. |
| 424 | /// |
| 425 | /// The number is assumed to be base-10 by default, it is parsed as binary if the |
| 426 | /// number is prefixed with `0b` (case sensitive!). |
| 427 | /// |
| 428 | /// # Example |
| 429 | /// |
| 430 | /// This allows base-10 addresses to be passed normally, or base-2 values to |
| 431 | /// be passed when prefixed with `0b`. |
| 432 | /// |
| 433 | /// ``` |
| 434 | /// use clap::Parser; |
| 435 | /// use clap_num::maybe_bin; |
| 436 | /// |
| 437 | /// #[derive(Parser)] |
| 438 | /// struct Args { |
| 439 | /// #[clap(short, long, value_parser=maybe_bin::<u8>)] |
| 440 | /// address: u8, |
| 441 | /// } |
| 442 | /// # let args = Args::parse_from(&["", "-a", "0b1001"]); |
| 443 | /// # assert_eq!(args.address, 9); |
| 444 | /// ``` |
| 445 | pub fn maybe_binrust-analyzer cargo clap-num 1.2.0 maybe_bin().<T: Numrust-analyzer cargo num-traits 0.2.19 Num# + signrust-analyzer cargo num-traits 0.2.19 sign/::Unsignedrust-analyzer cargo num-traits 0.2.19 sign/Unsigned#>(s: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> |
| 446 | where |
| 447 | <T as Numrust-analyzer cargo num-traits 0.2.19 Num#>::FromStrRadixErrrust-analyzer cargo num-traits 0.2.19 Num#FromStrRadixErr#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 448 | { |
| 449 | const BIN_PREFIXrust-analyzer cargo clap-num 1.2.0 BIN_PREFIX.: &str = "0b"; |
| 450 | const BIN_PREFIX_LENrust-analyzer cargo clap-num 1.2.0 BIN_PREFIX_LEN.: usize = BIN_PREFIXrust-analyzer cargo clap-num 1.2.0 BIN_PREFIX..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().(); |
| 451 | |
| 452 | let result = if s.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(BIN_PREFIXrust-analyzer cargo clap-num 1.2.0 BIN_PREFIX.) { |
| 453 | T::from_str_radixrust-analyzer cargo num-traits 0.2.19 Num#from_str_radix().(&s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().BIN_PREFIX_LENrust-analyzer cargo clap-num 1.2.0 BIN_PREFIX_LEN...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., 2) |
| 454 | } else { |
| 455 | T::from_str_radixrust-analyzer cargo num-traits 0.2.19 Num#from_str_radix().(s, 10) |
| 456 | }; |
| 457 | |
| 458 | result.map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(stringifyrust-analyzer cargo clap-num 1.2.0 stringify().) |
| 459 | } |
| 460 | |
| 461 | /// Validates an unsigned integer value that can be base-10 or base-2 within |
| 462 | /// a range. |
| 463 | /// |
| 464 | /// This combines [`maybe_bin`] and [`number_range`], see the |
| 465 | /// documentation of those functions for details. |
| 466 | /// |
| 467 | /// # Example |
| 468 | /// |
| 469 | /// This extends the example in [`maybe_bin`], and only allows a range of |
| 470 | /// addresses from 2 (`0b10`) to 15 (`0b1111`). |
| 471 | /// |
| 472 | /// ``` |
| 473 | /// use clap::Parser; |
| 474 | /// use clap_num::maybe_bin_range; |
| 475 | /// |
| 476 | /// fn address_in_range(s: &str) -> Result<u8, String> { |
| 477 | /// maybe_bin_range(s, 0b10, 0b1111) |
| 478 | /// } |
| 479 | /// |
| 480 | /// #[derive(Parser)] |
| 481 | /// struct Args { |
| 482 | /// #[clap(short, long, value_parser=address_in_range)] |
| 483 | /// address: u8, |
| 484 | /// } |
| 485 | /// # let args = Args::parse_from(&["", "-a", "0b1001"]); |
| 486 | /// # assert_eq!(args.address, 9); |
| 487 | /// ``` |
| 488 | pub fn maybe_bin_rangerust-analyzer cargo clap-num 1.2.0 maybe_bin_range().<T>(s: &str, min: T, max: T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> |
| 489 | where |
| 490 | <T as Numrust-analyzer cargo num-traits 0.2.19 Num#>::FromStrRadixErrrust-analyzer cargo num-traits 0.2.19 Num#FromStrRadixErr#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 491 | <T as FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#>::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 492 | T: FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#, |
| 493 | T: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 494 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 495 | T: Numrust-analyzer cargo num-traits 0.2.19 Num#, |
| 496 | T: signrust-analyzer cargo num-traits 0.2.19 sign/::Unsignedrust-analyzer cargo num-traits 0.2.19 sign/Unsigned#, |
| 497 | { |
| 498 | let val = maybe_binrust-analyzer cargo clap-num 1.2.0 maybe_bin().(s)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 499 | check_rangerust-analyzer cargo clap-num 1.2.0 check_range().(val, min, max) |
| 500 | } |