| 1 |
use std::convert::TryInto; |
| 2 |
use std::ops::RangeBounds; |
| 3 |
|
| 4 |
use crate::builder::Str; |
| 5 |
use crate::builder::StyledStr; |
| 6 |
use crate::parser::ValueSource; |
| 7 |
use crate::util::AnyValue; |
| 8 |
use crate::util::AnyValueId; |
| 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 |
pub struct ValueParser(ValueParserInner); |
| 64 |
|
| 65 |
enum ValueParserInner { |
| 66 |
|
| 67 |
Bool, |
| 68 |
|
| 69 |
String, |
| 70 |
|
| 71 |
OsString, |
| 72 |
|
| 73 |
PathBuf, |
| 74 |
Other(Box<dyn AnyValueParser>), |
| 75 |
} |
| 76 |
|
| 77 |
impl ValueParser { |
| 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 |
pub fn new<P>(other: P) -> Self |
| 113 |
where |
| 114 |
P: TypedValueParser, |
| 115 |
{ |
| 116 |
Self(ValueParserInner::Other(Box::new(other))) |
| 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 |
pub const fn bool() -> Self { |
| 144 |
Self(ValueParserInner::Bool) |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
pub const fn string() -> Self { |
| 169 |
Self(ValueParserInner::String) |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
pub const fn os_string() -> Self { |
| 201 |
Self(ValueParserInner::OsString) |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
pub const fn path_buf() -> Self { |
| 227 |
Self(ValueParserInner::PathBuf) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
impl ValueParser { |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
pub(crate) fn parse_ref( |
| 236 |
&self, |
| 237 |
cmd: &crate::Command, |
| 238 |
arg: Option<&crate::Arg>, |
| 239 |
value: &std::ffi::OsStr, |
| 240 |
source: ValueSource, |
| 241 |
) -> Result<AnyValue, crate::Error> { |
| 242 |
self.any_value_parser().parse_ref_(cmd, arg, value, source) |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
pub fn type_id(&self) -> AnyValueId { |
| 247 |
self.any_value_parser().type_id() |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
pub fn possible_values( |
| 255 |
&self, |
| 256 |
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> { |
| 257 |
self.any_value_parser().possible_values() |
| 258 |
} |
| 259 |
|
| 260 |
fn any_value_parser(&self) -> &dyn AnyValueParser { |
| 261 |
match &self.0 { |
| 262 |
ValueParserInner::Bool => &BoolValueParser {}, |
| 263 |
ValueParserInner::String => &StringValueParser {}, |
| 264 |
ValueParserInner::OsString => &OsStringValueParser {}, |
| 265 |
ValueParserInner::PathBuf => &PathBufValueParser {}, |
| 266 |
ValueParserInner::Other(o) => o.as_ref(), |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
impl<P> From<P> for ValueParser |
| 295 |
where |
| 296 |
P: TypedValueParser + Send + Sync + 'static, |
| 297 |
{ |
| 298 |
fn from(p: P) -> Self { |
| 299 |
Self::new(p) |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
impl From<_AnonymousValueParser> for ValueParser { |
| 304 |
fn from(p: _AnonymousValueParser) -> Self { |
| 305 |
p.0 |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
impl From<std::ops::Range<i64>> for ValueParser { |
| 334 |
fn from(value: std::ops::Range<i64>) -> Self { |
| 335 |
let inner = RangedI64ValueParser::<i64>::new().range(value.start..value.end); |
| 336 |
Self::from(inner) |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
impl From<std::ops::RangeInclusive<i64>> for ValueParser { |
| 365 |
fn from(value: std::ops::RangeInclusive<i64>) -> Self { |
| 366 |
let inner = RangedI64ValueParser::<i64>::new().range(value.start()..=value.end()); |
| 367 |
Self::from(inner) |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
impl From<std::ops::RangeFrom<i64>> for ValueParser { |
| 396 |
fn from(value: std::ops::RangeFrom<i64>) -> Self { |
| 397 |
let inner = RangedI64ValueParser::<i64>::new().range(value.start..); |
| 398 |
Self::from(inner) |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
impl From<std::ops::RangeTo<i64>> for ValueParser { |
| 427 |
fn from(value: std::ops::RangeTo<i64>) -> Self { |
| 428 |
let inner = RangedI64ValueParser::<i64>::new().range(..value.end); |
| 429 |
Self::from(inner) |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
impl From<std::ops::RangeToInclusive<i64>> for ValueParser { |
| 458 |
fn from(value: std::ops::RangeToInclusive<i64>) -> Self { |
| 459 |
let inner = RangedI64ValueParser::<i64>::new().range(..=value.end); |
| 460 |
Self::from(inner) |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
impl From<std::ops::RangeFull> for ValueParser { |
| 489 |
fn from(value: std::ops::RangeFull) -> Self { |
| 490 |
let inner = RangedI64ValueParser::<i64>::new().range(value); |
| 491 |
Self::from(inner) |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
impl<P, const C: usize> From<[P; C]> for ValueParser |
| 521 |
where |
| 522 |
P: Into<super::PossibleValue>, |
| 523 |
{ |
| 524 |
fn from(values: [P; C]) -> Self { |
| 525 |
let inner = PossibleValuesParser::from(values); |
| 526 |
Self::from(inner) |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
impl<P> From<Vec<P>> for ValueParser |
| 557 |
where |
| 558 |
P: Into<super::PossibleValue>, |
| 559 |
{ |
| 560 |
fn from(values: Vec<P>) -> Self { |
| 561 |
let inner = PossibleValuesParser::from(values); |
| 562 |
Self::from(inner) |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
impl std::fmt::Debug for ValueParser { |
| 567 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { |
| 568 |
match &self.0 { |
| 569 |
ValueParserInner::Bool => f.debug_struct("ValueParser::bool").finish(), |
| 570 |
ValueParserInner::String => f.debug_struct("ValueParser::string").finish(), |
| 571 |
ValueParserInner::OsString => f.debug_struct("ValueParser::os_string").finish(), |
| 572 |
ValueParserInner::PathBuf => f.debug_struct("ValueParser::path_buf").finish(), |
| 573 |
ValueParserInner::Other(o) => write!(f, "ValueParser::other({:?})", o.type_id()), |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
impl Clone for ValueParser { |
| 579 |
fn clone(&self) -> Self { |
| 580 |
Self(match &self.0 { |
| 581 |
ValueParserInner::Bool => ValueParserInner::Bool, |
| 582 |
ValueParserInner::String => ValueParserInner::String, |
| 583 |
ValueParserInner::OsString => ValueParserInner::OsString, |
| 584 |
ValueParserInner::PathBuf => ValueParserInner::PathBuf, |
| 585 |
ValueParserInner::Other(o) => ValueParserInner::Other(o.clone_any()), |
| 586 |
}) |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
trait AnyValueParser: Send + Sync + 'static { |
| 592 |
fn parse_ref( |
| 593 |
&self, |
| 594 |
cmd: &crate::Command, |
| 595 |
arg: Option<&crate::Arg>, |
| 596 |
value: &std::ffi::OsStr, |
| 597 |
) -> Result<AnyValue, crate::Error>; |
| 598 |
|
| 599 |
fn parse_ref_( |
| 600 |
&self, |
| 601 |
cmd: &crate::Command, |
| 602 |
arg: Option<&crate::Arg>, |
| 603 |
value: &std::ffi::OsStr, |
| 604 |
_source: ValueSource, |
| 605 |
) -> Result<AnyValue, crate::Error> { |
| 606 |
self.parse_ref(cmd, arg, value) |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
fn type_id(&self) -> AnyValueId; |
| 611 |
|
| 612 |
fn possible_values( |
| 613 |
&self, |
| 614 |
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>>; |
| 615 |
|
| 616 |
fn clone_any(&self) -> Box<dyn AnyValueParser>; |
| 617 |
} |
| 618 |
|
| 619 |
impl<T, P> AnyValueParser for P |
| 620 |
where |
| 621 |
T: std::any::Any + Clone + Send + Sync + 'static, |
| 622 |
P: TypedValueParser<Value = T>, |
| 623 |
{ |
| 624 |
fn parse_ref( |
| 625 |
&self, |
| 626 |
cmd: &crate::Command, |
| 627 |
arg: Option<&crate::Arg>, |
| 628 |
value: &std::ffi::OsStr, |
| 629 |
) -> Result<AnyValue, crate::Error> { |
| 630 |
let value = ok!(TypedValueParser::parse_ref(self, cmd, arg, value)); |
| 631 |
Ok(AnyValue::new(value)) |
| 632 |
} |
| 633 |
|
| 634 |
fn parse_ref_( |
| 635 |
&self, |
| 636 |
cmd: &crate::Command, |
| 637 |
arg: Option<&crate::Arg>, |
| 638 |
value: &std::ffi::OsStr, |
| 639 |
source: ValueSource, |
| 640 |
) -> Result<AnyValue, crate::Error> { |
| 641 |
let value = ok!(TypedValueParser::parse_ref_(self, cmd, arg, value, source)); |
| 642 |
Ok(AnyValue::new(value)) |
| 643 |
} |
| 644 |
|
| 645 |
fn type_id(&self) -> AnyValueId { |
| 646 |
AnyValueId::of::<T>() |
| 647 |
} |
| 648 |
|
| 649 |
fn possible_values( |
| 650 |
&self, |
| 651 |
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> { |
| 652 |
P::possible_values(self) |
| 653 |
} |
| 654 |
|
| 655 |
fn clone_any(&self) -> Box<dyn AnyValueParser> { |
| 656 |
Box::new(self.clone()) |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
pub trait TypedValueParser: Clone + Send + Sync + 'static { |
| 712 |
|
| 713 |
type Value: Send + Sync + Clone; |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
fn parse_ref( |
| 719 |
&self, |
| 720 |
cmd: &crate::Command, |
| 721 |
arg: Option<&crate::Arg>, |
| 722 |
value: &std::ffi::OsStr, |
| 723 |
) -> Result<Self::Value, crate::Error>; |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
fn parse_ref_( |
| 729 |
&self, |
| 730 |
cmd: &crate::Command, |
| 731 |
arg: Option<&crate::Arg>, |
| 732 |
value: &std::ffi::OsStr, |
| 733 |
_source: ValueSource, |
| 734 |
) -> Result<Self::Value, crate::Error> { |
| 735 |
self.parse_ref(cmd, arg, value) |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
fn parse( |
| 742 |
&self, |
| 743 |
cmd: &crate::Command, |
| 744 |
arg: Option<&crate::Arg>, |
| 745 |
value: std::ffi::OsString, |
| 746 |
) -> Result<Self::Value, crate::Error> { |
| 747 |
self.parse_ref(cmd, arg, &value) |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
fn parse_( |
| 754 |
&self, |
| 755 |
cmd: &crate::Command, |
| 756 |
arg: Option<&crate::Arg>, |
| 757 |
value: std::ffi::OsString, |
| 758 |
_source: ValueSource, |
| 759 |
) -> Result<Self::Value, crate::Error> { |
| 760 |
self.parse(cmd, arg, value) |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
fn possible_values( |
| 768 |
&self, |
| 769 |
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> { |
| 770 |
None |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
fn map<T, F>(self, func: F) -> MapValueParser<Self, F> |
| 811 |
where |
| 812 |
T: Send + Sync + Clone, |
| 813 |
F: Fn(Self::Value) -> T + Clone, |
| 814 |
{ |
| 815 |
MapValueParser::new(self, func) |
| 816 |
} |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
fn try_map<T, E, F>(self, func: F) -> TryMapValueParser<Self, F> |
| 861 |
where |
| 862 |
F: Fn(Self::Value) -> Result<T, E> + Clone + Send + Sync + 'static, |
| 863 |
T: Send + Sync + Clone, |
| 864 |
E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 865 |
{ |
| 866 |
TryMapValueParser::new(self, func) |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
impl<F, T, E> TypedValueParser for F |
| 871 |
where |
| 872 |
F: Fn(&str) -> Result<T, E> + Clone + Send + Sync + 'static, |
| 873 |
E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 874 |
T: Send + Sync + Clone, |
| 875 |
{ |
| 876 |
type Value = T; |
| 877 |
|
| 878 |
fn parse_ref( |
| 879 |
&self, |
| 880 |
cmd: &crate::Command, |
| 881 |
arg: Option<&crate::Arg>, |
| 882 |
value: &std::ffi::OsStr, |
| 883 |
) -> Result<Self::Value, crate::Error> { |
| 884 |
let value = ok!(value.to_str().ok_or_else(|| { |
| 885 |
crate::Error::invalid_utf8( |
| 886 |
cmd, |
| 887 |
crate::output::Usage::new(cmd).create_usage_with_title(&[]), |
| 888 |
) |
| 889 |
})); |
| 890 |
let value = ok!((self)(value).map_err(|e| { |
| 891 |
let arg = arg |
| 892 |
.map(|a| a.to_string()) |
| 893 |
.unwrap_or_else(|| "...".to_owned()); |
| 894 |
crate::Error::value_validation(arg, value.to_owned(), e.into()).with_cmd(cmd) |
| 895 |
})); |
| 896 |
Ok(value) |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
#[derive(Copy, Clone, Debug)] |
| 904 |
#[non_exhaustive] |
| 905 |
pub struct StringValueParser {} |
| 906 |
|
| 907 |
impl StringValueParser { |
| 908 |
|
| 909 |
pub fn new() -> Self { |
| 910 |
Self {} |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
impl TypedValueParser for StringValueParser { |
| 915 |
type Value = String; |
| 916 |
|
| 917 |
fn parse_ref( |
| 918 |
&self, |
| 919 |
cmd: &crate::Command, |
| 920 |
arg: Option<&crate::Arg>, |
| 921 |
value: &std::ffi::OsStr, |
| 922 |
) -> Result<Self::Value, crate::Error> { |
| 923 |
TypedValueParser::parse(self, cmd, arg, value.to_owned()) |
| 924 |
} |
| 925 |
|
| 926 |
fn parse( |
| 927 |
&self, |
| 928 |
cmd: &crate::Command, |
| 929 |
_arg: Option<&crate::Arg>, |
| 930 |
value: std::ffi::OsString, |
| 931 |
) -> Result<Self::Value, crate::Error> { |
| 932 |
let value = ok!(value.into_string().map_err(|_| { |
| 933 |
crate::Error::invalid_utf8( |
| 934 |
cmd, |
| 935 |
crate::output::Usage::new(cmd).create_usage_with_title(&[]), |
| 936 |
) |
| 937 |
})); |
| 938 |
Ok(value) |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
impl Default for StringValueParser { |
| 943 |
fn default() -> Self { |
| 944 |
Self::new() |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
#[derive(Copy, Clone, Debug)] |
| 952 |
#[non_exhaustive] |
| 953 |
pub struct OsStringValueParser {} |
| 954 |
|
| 955 |
impl OsStringValueParser { |
| 956 |
|
| 957 |
pub fn new() -> Self { |
| 958 |
Self {} |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
impl TypedValueParser for OsStringValueParser { |
| 963 |
type Value = std::ffi::OsString; |
| 964 |
|
| 965 |
fn parse_ref( |
| 966 |
&self, |
| 967 |
cmd: &crate::Command, |
| 968 |
arg: Option<&crate::Arg>, |
| 969 |
value: &std::ffi::OsStr, |
| 970 |
) -> Result<Self::Value, crate::Error> { |
| 971 |
TypedValueParser::parse(self, cmd, arg, value.to_owned()) |
| 972 |
} |
| 973 |
|
| 974 |
fn parse( |
| 975 |
&self, |
| 976 |
_cmd: &crate::Command, |
| 977 |
_arg: Option<&crate::Arg>, |
| 978 |
value: std::ffi::OsString, |
| 979 |
) -> Result<Self::Value, crate::Error> { |
| 980 |
Ok(value) |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
impl Default for OsStringValueParser { |
| 985 |
fn default() -> Self { |
| 986 |
Self::new() |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
#[derive(Copy, Clone, Debug)] |
| 994 |
#[non_exhaustive] |
| 995 |
pub struct PathBufValueParser {} |
| 996 |
|
| 997 |
impl PathBufValueParser { |
| 998 |
|
| 999 |
pub fn new() -> Self { |
| 1000 |
Self {} |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
impl TypedValueParser for PathBufValueParser { |
| 1005 |
type Value = std::path::PathBuf; |
| 1006 |
|
| 1007 |
fn parse_ref( |
| 1008 |
&self, |
| 1009 |
cmd: &crate::Command, |
| 1010 |
arg: Option<&crate::Arg>, |
| 1011 |
value: &std::ffi::OsStr, |
| 1012 |
) -> Result<Self::Value, crate::Error> { |
| 1013 |
TypedValueParser::parse(self, cmd, arg, value.to_owned()) |
| 1014 |
} |
| 1015 |
|
| 1016 |
fn parse( |
| 1017 |
&self, |
| 1018 |
cmd: &crate::Command, |
| 1019 |
arg: Option<&crate::Arg>, |
| 1020 |
value: std::ffi::OsString, |
| 1021 |
) -> Result<Self::Value, crate::Error> { |
| 1022 |
if value.is_empty() { |
| 1023 |
return Err(crate::Error::empty_value( |
| 1024 |
cmd, |
| 1025 |
&[], |
| 1026 |
arg.map(ToString::to_string) |
| 1027 |
.unwrap_or_else(|| "...".to_owned()), |
| 1028 |
)); |
| 1029 |
} |
| 1030 |
Ok(Self::Value::from(value)) |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
impl Default for PathBufValueParser { |
| 1035 |
fn default() -> Self { |
| 1036 |
Self::new() |
| 1037 |
} |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
#[derive(Clone, Debug)] |
| 1079 |
pub struct EnumValueParser<E: crate::ValueEnum + Clone + Send + Sync + 'static>( |
| 1080 |
std::marker::PhantomData<E>, |
| 1081 |
); |
| 1082 |
|
| 1083 |
impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> EnumValueParser<E> { |
| 1084 |
|
| 1085 |
pub fn new() -> Self { |
| 1086 |
let phantom: std::marker::PhantomData<E> = Default::default(); |
| 1087 |
Self(phantom) |
| 1088 |
} |
| 1089 |
} |
| 1090 |
|
| 1091 |
impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> TypedValueParser for EnumValueParser<E> { |
| 1092 |
type Value = E; |
| 1093 |
|
| 1094 |
fn parse_ref( |
| 1095 |
&self, |
| 1096 |
cmd: &crate::Command, |
| 1097 |
arg: Option<&crate::Arg>, |
| 1098 |
value: &std::ffi::OsStr, |
| 1099 |
) -> Result<Self::Value, crate::Error> { |
| 1100 |
let ignore_case = arg.map(|a| a.is_ignore_case_set()).unwrap_or(false); |
| 1101 |
let possible_vals = || { |
| 1102 |
E::value_variants() |
| 1103 |
.iter() |
| 1104 |
.filter_map(|v| v.to_possible_value()) |
| 1105 |
.filter(|v| !v.is_hide_set()) |
| 1106 |
.map(|v| v.get_name().to_owned()) |
| 1107 |
.collect::<Vec<_>>() |
| 1108 |
}; |
| 1109 |
|
| 1110 |
let value = ok!(value.to_str().ok_or_else(|| { |
| 1111 |
crate::Error::invalid_value( |
| 1112 |
cmd, |
| 1113 |
value.to_string_lossy().into_owned(), |
| 1114 |
&possible_vals(), |
| 1115 |
arg.map(ToString::to_string) |
| 1116 |
.unwrap_or_else(|| "...".to_owned()), |
| 1117 |
) |
| 1118 |
})); |
| 1119 |
let value = ok!(E::value_variants() |
| 1120 |
.iter() |
| 1121 |
.find(|v| { |
| 1122 |
v.to_possible_value() |
| 1123 |
.expect("ValueEnum::value_variants contains only values with a corresponding ValueEnum::to_possible_value") |
| 1124 |
.matches(value, ignore_case) |
| 1125 |
}) |
| 1126 |
.ok_or_else(|| { |
| 1127 |
crate::Error::invalid_value( |
| 1128 |
cmd, |
| 1129 |
value.to_owned(), |
| 1130 |
&possible_vals(), |
| 1131 |
arg.map(ToString::to_string) |
| 1132 |
.unwrap_or_else(|| "...".to_owned()), |
| 1133 |
) |
| 1134 |
})) |
| 1135 |
.clone(); |
| 1136 |
Ok(value) |
| 1137 |
} |
| 1138 |
|
| 1139 |
fn possible_values( |
| 1140 |
&self, |
| 1141 |
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> { |
| 1142 |
Some(Box::new( |
| 1143 |
E::value_variants() |
| 1144 |
.iter() |
| 1145 |
.filter_map(|v| v.to_possible_value()), |
| 1146 |
)) |
| 1147 |
} |
| 1148 |
} |
| 1149 |
|
| 1150 |
impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> Default for EnumValueParser<E> { |
| 1151 |
fn default() -> Self { |
| 1152 |
Self::new() |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
#[derive(Clone, Debug)] |
| 1196 |
pub struct PossibleValuesParser(Vec<super::PossibleValue>); |
| 1197 |
|
| 1198 |
impl PossibleValuesParser { |
| 1199 |
|
| 1200 |
pub fn new(values: impl Into<PossibleValuesParser>) -> Self { |
| 1201 |
values.into() |
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
impl TypedValueParser for PossibleValuesParser { |
| 1206 |
type Value = String; |
| 1207 |
|
| 1208 |
fn parse_ref( |
| 1209 |
&self, |
| 1210 |
cmd: &crate::Command, |
| 1211 |
arg: Option<&crate::Arg>, |
| 1212 |
value: &std::ffi::OsStr, |
| 1213 |
) -> Result<Self::Value, crate::Error> { |
| 1214 |
TypedValueParser::parse(self, cmd, arg, value.to_owned()) |
| 1215 |
} |
| 1216 |
|
| 1217 |
fn parse( |
| 1218 |
&self, |
| 1219 |
cmd: &crate::Command, |
| 1220 |
arg: Option<&crate::Arg>, |
| 1221 |
value: std::ffi::OsString, |
| 1222 |
) -> Result<String, crate::Error> { |
| 1223 |
let value = ok!(value.into_string().map_err(|_| { |
| 1224 |
crate::Error::invalid_utf8( |
| 1225 |
cmd, |
| 1226 |
crate::output::Usage::new(cmd).create_usage_with_title(&[]), |
| 1227 |
) |
| 1228 |
})); |
| 1229 |
|
| 1230 |
let ignore_case = arg.map(|a| a.is_ignore_case_set()).unwrap_or(false); |
| 1231 |
if self.0.iter().any(|v| v.matches(&value, ignore_case)) { |
| 1232 |
Ok(value) |
| 1233 |
} else { |
| 1234 |
let possible_vals = self |
| 1235 |
.0 |
| 1236 |
.iter() |
| 1237 |
.filter(|v| !v.is_hide_set()) |
| 1238 |
.map(|v| v.get_name().to_owned()) |
| 1239 |
.collect::<Vec<_>>(); |
| 1240 |
|
| 1241 |
Err(crate::Error::invalid_value( |
| 1242 |
cmd, |
| 1243 |
value, |
| 1244 |
&possible_vals, |
| 1245 |
arg.map(ToString::to_string) |
| 1246 |
.unwrap_or_else(|| "...".to_owned()), |
| 1247 |
)) |
| 1248 |
} |
| 1249 |
} |
| 1250 |
|
| 1251 |
fn possible_values( |
| 1252 |
&self, |
| 1253 |
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> { |
| 1254 |
Some(Box::new(self.0.iter().cloned())) |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
impl<I, T> From<I> for PossibleValuesParser |
| 1259 |
where |
| 1260 |
I: IntoIterator<Item = T>, |
| 1261 |
T: Into<super::PossibleValue>, |
| 1262 |
{ |
| 1263 |
fn from(values: I) -> Self { |
| 1264 |
Self(values.into_iter().map(|t| t.into()).collect()) |
| 1265 |
} |
| 1266 |
} |
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
#[derive(Copy, Clone, Debug)] |
| 1315 |
pub struct RangedI64ValueParser<T: TryFrom<i64> + Clone + Send + Sync = i64> { |
| 1316 |
bounds: (std::ops::Bound<i64>, std::ops::Bound<i64>), |
| 1317 |
target: std::marker::PhantomData<T>, |
| 1318 |
} |
| 1319 |
|
| 1320 |
impl<T: TryFrom<i64> + Clone + Send + Sync> RangedI64ValueParser<T> { |
| 1321 |
|
| 1322 |
pub fn new() -> Self { |
| 1323 |
Self::from(..) |
| 1324 |
} |
| 1325 |
|
| 1326 |
|
| 1327 |
pub fn range<B: RangeBounds<i64>>(mut self, range: B) -> Self { |
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
let start = match range.start_bound() { |
| 1332 |
l @ std::ops::Bound::Included(i) => { |
| 1333 |
debug_assert!( |
| 1334 |
self.bounds.contains(i), |
| 1335 |
"{} must be in {:?}", |
| 1336 |
i, |
| 1337 |
self.bounds |
| 1338 |
); |
| 1339 |
l.cloned() |
| 1340 |
} |
| 1341 |
l @ std::ops::Bound::Excluded(i) => { |
| 1342 |
debug_assert!( |
| 1343 |
self.bounds.contains(&i.saturating_add(1)), |
| 1344 |
"{} must be in {:?}", |
| 1345 |
i, |
| 1346 |
self.bounds |
| 1347 |
); |
| 1348 |
l.cloned() |
| 1349 |
} |
| 1350 |
std::ops::Bound::Unbounded => self.bounds.start_bound().cloned(), |
| 1351 |
}; |
| 1352 |
let end = match range.end_bound() { |
| 1353 |
l @ std::ops::Bound::Included(i) => { |
| 1354 |
debug_assert!( |
| 1355 |
self.bounds.contains(i), |
| 1356 |
"{} must be in {:?}", |
| 1357 |
i, |
| 1358 |
self.bounds |
| 1359 |
); |
| 1360 |
l.cloned() |
| 1361 |
} |
| 1362 |
l @ std::ops::Bound::Excluded(i) => { |
| 1363 |
debug_assert!( |
| 1364 |
self.bounds.contains(&i.saturating_sub(1)), |
| 1365 |
"{} must be in {:?}", |
| 1366 |
i, |
| 1367 |
self.bounds |
| 1368 |
); |
| 1369 |
l.cloned() |
| 1370 |
} |
| 1371 |
std::ops::Bound::Unbounded => self.bounds.end_bound().cloned(), |
| 1372 |
}; |
| 1373 |
self.bounds = (start, end); |
| 1374 |
self |
| 1375 |
} |
| 1376 |
|
| 1377 |
fn format_bounds(&self) -> String { |
| 1378 |
let mut result = match self.bounds.0 { |
| 1379 |
std::ops::Bound::Included(i) => i.to_string(), |
| 1380 |
std::ops::Bound::Excluded(i) => i.saturating_add(1).to_string(), |
| 1381 |
std::ops::Bound::Unbounded => i64::MIN.to_string(), |
| 1382 |
}; |
| 1383 |
result.push_str(".."); |
| 1384 |
match self.bounds.1 { |
| 1385 |
std::ops::Bound::Included(i) => { |
| 1386 |
result.push('='); |
| 1387 |
result.push_str(&i.to_string()); |
| 1388 |
} |
| 1389 |
std::ops::Bound::Excluded(i) => { |
| 1390 |
result.push_str(&i.to_string()); |
| 1391 |
} |
| 1392 |
std::ops::Bound::Unbounded => { |
| 1393 |
result.push_str(&i64::MAX.to_string()); |
| 1394 |
} |
| 1395 |
} |
| 1396 |
result |
| 1397 |
} |
| 1398 |
} |
| 1399 |
|
| 1400 |
impl<T: TryFrom<i64> + Clone + Send + Sync + 'static> TypedValueParser for RangedI64ValueParser<T> |
| 1401 |
where |
| 1402 |
<T as TryFrom<i64>>::Error: Send + Sync + 'static + std::error::Error + ToString, |
| 1403 |
{ |
| 1404 |
type Value = T; |
| 1405 |
|
| 1406 |
fn parse_ref( |
| 1407 |
&self, |
| 1408 |
cmd: &crate::Command, |
| 1409 |
arg: Option<&crate::Arg>, |
| 1410 |
raw_value: &std::ffi::OsStr, |
| 1411 |
) -> Result<Self::Value, crate::Error> { |
| 1412 |
let value = ok!(raw_value.to_str().ok_or_else(|| { |
| 1413 |
crate::Error::invalid_utf8( |
| 1414 |
cmd, |
| 1415 |
crate::output::Usage::new(cmd).create_usage_with_title(&[]), |
| 1416 |
) |
| 1417 |
})); |
| 1418 |
let value = ok!(value.parse::<i64>().map_err(|err| { |
| 1419 |
let arg = arg |
| 1420 |
.map(|a| a.to_string()) |
| 1421 |
.unwrap_or_else(|| "...".to_owned()); |
| 1422 |
crate::Error::value_validation( |
| 1423 |
arg, |
| 1424 |
raw_value.to_string_lossy().into_owned(), |
| 1425 |
err.into(), |
| 1426 |
) |
| 1427 |
.with_cmd(cmd) |
| 1428 |
})); |
| 1429 |
if !self.bounds.contains(&value) { |
| 1430 |
let arg = arg |
| 1431 |
.map(|a| a.to_string()) |
| 1432 |
.unwrap_or_else(|| "...".to_owned()); |
| 1433 |
return Err(crate::Error::value_validation( |
| 1434 |
arg, |
| 1435 |
raw_value.to_string_lossy().into_owned(), |
| 1436 |
format!("{} is not in {}", value, self.format_bounds()).into(), |
| 1437 |
) |
| 1438 |
.with_cmd(cmd)); |
| 1439 |
} |
| 1440 |
|
| 1441 |
let value: Result<Self::Value, _> = value.try_into(); |
| 1442 |
let value = ok!(value.map_err(|err| { |
| 1443 |
let arg = arg |
| 1444 |
.map(|a| a.to_string()) |
| 1445 |
.unwrap_or_else(|| "...".to_owned()); |
| 1446 |
crate::Error::value_validation( |
| 1447 |
arg, |
| 1448 |
raw_value.to_string_lossy().into_owned(), |
| 1449 |
err.into(), |
| 1450 |
) |
| 1451 |
.with_cmd(cmd) |
| 1452 |
})); |
| 1453 |
|
| 1454 |
Ok(value) |
| 1455 |
} |
| 1456 |
} |
| 1457 |
|
| 1458 |
impl<T: TryFrom<i64> + Clone + Send + Sync, B: RangeBounds<i64>> From<B> |
| 1459 |
for RangedI64ValueParser<T> |
| 1460 |
{ |
| 1461 |
fn from(range: B) -> Self { |
| 1462 |
Self { |
| 1463 |
bounds: (range.start_bound().cloned(), range.end_bound().cloned()), |
| 1464 |
target: Default::default(), |
| 1465 |
} |
| 1466 |
} |
| 1467 |
} |
| 1468 |
|
| 1469 |
impl<T: TryFrom<i64> + Clone + Send + Sync> Default for RangedI64ValueParser<T> { |
| 1470 |
fn default() -> Self { |
| 1471 |
Self::new() |
| 1472 |
} |
| 1473 |
} |
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
#[derive(Copy, Clone, Debug)] |
| 1514 |
pub struct RangedU64ValueParser<T: TryFrom<u64> = u64> { |
| 1515 |
bounds: (std::ops::Bound<u64>, std::ops::Bound<u64>), |
| 1516 |
target: std::marker::PhantomData<T>, |
| 1517 |
} |
| 1518 |
|
| 1519 |
impl<T: TryFrom<u64>> RangedU64ValueParser<T> { |
| 1520 |
|
| 1521 |
pub fn new() -> Self { |
| 1522 |
Self::from(..) |
| 1523 |
} |
| 1524 |
|
| 1525 |
|
| 1526 |
pub fn range<B: RangeBounds<u64>>(mut self, range: B) -> Self { |
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
let start = match range.start_bound() { |
| 1531 |
l @ std::ops::Bound::Included(i) => { |
| 1532 |
debug_assert!( |
| 1533 |
self.bounds.contains(i), |
| 1534 |
"{} must be in {:?}", |
| 1535 |
i, |
| 1536 |
self.bounds |
| 1537 |
); |
| 1538 |
l.cloned() |
| 1539 |
} |
| 1540 |
l @ std::ops::Bound::Excluded(i) => { |
| 1541 |
debug_assert!( |
| 1542 |
self.bounds.contains(&i.saturating_add(1)), |
| 1543 |
"{} must be in {:?}", |
| 1544 |
i, |
| 1545 |
self.bounds |
| 1546 |
); |
| 1547 |
l.cloned() |
| 1548 |
} |
| 1549 |
std::ops::Bound::Unbounded => self.bounds.start_bound().cloned(), |
| 1550 |
}; |
| 1551 |
let end = match range.end_bound() { |
| 1552 |
l @ std::ops::Bound::Included(i) => { |
| 1553 |
debug_assert!( |
| 1554 |
self.bounds.contains(i), |
| 1555 |
"{} must be in {:?}", |
| 1556 |
i, |
| 1557 |
self.bounds |
| 1558 |
); |
| 1559 |
l.cloned() |
| 1560 |
} |
| 1561 |
l @ std::ops::Bound::Excluded(i) => { |
| 1562 |
debug_assert!( |
| 1563 |
self.bounds.contains(&i.saturating_sub(1)), |
| 1564 |
"{} must be in {:?}", |
| 1565 |
i, |
| 1566 |
self.bounds |
| 1567 |
); |
| 1568 |
l.cloned() |
| 1569 |
} |
| 1570 |
std::ops::Bound::Unbounded => self.bounds.end_bound().cloned(), |
| 1571 |
}; |
| 1572 |
self.bounds = (start, end); |
| 1573 |
self |
| 1574 |
} |
| 1575 |
|
| 1576 |
fn format_bounds(&self) -> String { |
| 1577 |
let mut result = match self.bounds.0 { |
| 1578 |
std::ops::Bound::Included(i) => i.to_string(), |
| 1579 |
std::ops::Bound::Excluded(i) => i.saturating_add(1).to_string(), |
| 1580 |
std::ops::Bound::Unbounded => u64::MIN.to_string(), |
| 1581 |
}; |
| 1582 |
result.push_str(".."); |
| 1583 |
match self.bounds.1 { |
| 1584 |
std::ops::Bound::Included(i) => { |
| 1585 |
result.push('='); |
| 1586 |
result.push_str(&i.to_string()); |
| 1587 |
} |
| 1588 |
std::ops::Bound::Excluded(i) => { |
| 1589 |
result.push_str(&i.to_string()); |
| 1590 |
} |
| 1591 |
std::ops::Bound::Unbounded => { |
| 1592 |
result.push_str(&u64::MAX.to_string()); |
| 1593 |
} |
| 1594 |
} |
| 1595 |
result |
| 1596 |
} |
| 1597 |
} |
| 1598 |
|
| 1599 |
impl<T: TryFrom<u64> + Clone + Send + Sync + 'static> TypedValueParser for RangedU64ValueParser<T> |
| 1600 |
where |
| 1601 |
<T as TryFrom<u64>>::Error: Send + Sync + 'static + std::error::Error + ToString, |
| 1602 |
{ |
| 1603 |
type Value = T; |
| 1604 |
|
| 1605 |
fn parse_ref( |
| 1606 |
&self, |
| 1607 |
cmd: &crate::Command, |
| 1608 |
arg: Option<&crate::Arg>, |
| 1609 |
raw_value: &std::ffi::OsStr, |
| 1610 |
) -> Result<Self::Value, crate::Error> { |
| 1611 |
let value = ok!(raw_value.to_str().ok_or_else(|| { |
| 1612 |
crate::Error::invalid_utf8( |
| 1613 |
cmd, |
| 1614 |
crate::output::Usage::new(cmd).create_usage_with_title(&[]), |
| 1615 |
) |
| 1616 |
})); |
| 1617 |
let value = ok!(value.parse::<u64>().map_err(|err| { |
| 1618 |
let arg = arg |
| 1619 |
.map(|a| a.to_string()) |
| 1620 |
.unwrap_or_else(|| "...".to_owned()); |
| 1621 |
crate::Error::value_validation( |
| 1622 |
arg, |
| 1623 |
raw_value.to_string_lossy().into_owned(), |
| 1624 |
err.into(), |
| 1625 |
) |
| 1626 |
.with_cmd(cmd) |
| 1627 |
})); |
| 1628 |
if !self.bounds.contains(&value) { |
| 1629 |
let arg = arg |
| 1630 |
.map(|a| a.to_string()) |
| 1631 |
.unwrap_or_else(|| "...".to_owned()); |
| 1632 |
return Err(crate::Error::value_validation( |
| 1633 |
arg, |
| 1634 |
raw_value.to_string_lossy().into_owned(), |
| 1635 |
format!("{} is not in {}", value, self.format_bounds()).into(), |
| 1636 |
) |
| 1637 |
.with_cmd(cmd)); |
| 1638 |
} |
| 1639 |
|
| 1640 |
let value: Result<Self::Value, _> = value.try_into(); |
| 1641 |
let value = ok!(value.map_err(|err| { |
| 1642 |
let arg = arg |
| 1643 |
.map(|a| a.to_string()) |
| 1644 |
.unwrap_or_else(|| "...".to_owned()); |
| 1645 |
crate::Error::value_validation( |
| 1646 |
arg, |
| 1647 |
raw_value.to_string_lossy().into_owned(), |
| 1648 |
err.into(), |
| 1649 |
) |
| 1650 |
.with_cmd(cmd) |
| 1651 |
})); |
| 1652 |
|
| 1653 |
Ok(value) |
| 1654 |
} |
| 1655 |
} |
| 1656 |
|
| 1657 |
impl<T: TryFrom<u64>, B: RangeBounds<u64>> From<B> for RangedU64ValueParser<T> { |
| 1658 |
fn from(range: B) -> Self { |
| 1659 |
Self { |
| 1660 |
bounds: (range.start_bound().cloned(), range.end_bound().cloned()), |
| 1661 |
target: Default::default(), |
| 1662 |
} |
| 1663 |
} |
| 1664 |
} |
| 1665 |
|
| 1666 |
impl<T: TryFrom<u64>> Default for RangedU64ValueParser<T> { |
| 1667 |
fn default() -> Self { |
| 1668 |
Self::new() |
| 1669 |
} |
| 1670 |
} |
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
#[derive(Copy, Clone, Debug)] |
| 1676 |
#[non_exhaustive] |
| 1677 |
pub struct BoolValueParser {} |
| 1678 |
|
| 1679 |
impl BoolValueParser { |
| 1680 |
|
| 1681 |
pub fn new() -> Self { |
| 1682 |
Self {} |
| 1683 |
} |
| 1684 |
|
| 1685 |
fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue> { |
| 1686 |
["true", "false"] |
| 1687 |
.iter() |
| 1688 |
.copied() |
| 1689 |
.map(crate::builder::PossibleValue::new) |
| 1690 |
} |
| 1691 |
} |
| 1692 |
|
| 1693 |
impl TypedValueParser for BoolValueParser { |
| 1694 |
type Value = bool; |
| 1695 |
|
| 1696 |
fn parse_ref( |
| 1697 |
&self, |
| 1698 |
cmd: &crate::Command, |
| 1699 |
arg: Option<&crate::Arg>, |
| 1700 |
value: &std::ffi::OsStr, |
| 1701 |
) -> Result<Self::Value, crate::Error> { |
| 1702 |
let value = if value == std::ffi::OsStr::new("true") { |
| 1703 |
true |
| 1704 |
} else if value == std::ffi::OsStr::new("false") { |
| 1705 |
false |
| 1706 |
} else { |
| 1707 |
|
| 1708 |
let possible_vals = Self::possible_values() |
| 1709 |
.map(|v| v.get_name().to_owned()) |
| 1710 |
.collect::<Vec<_>>(); |
| 1711 |
|
| 1712 |
return Err(crate::Error::invalid_value( |
| 1713 |
cmd, |
| 1714 |
value.to_string_lossy().into_owned(), |
| 1715 |
&possible_vals, |
| 1716 |
arg.map(ToString::to_string) |
| 1717 |
.unwrap_or_else(|| "...".to_owned()), |
| 1718 |
)); |
| 1719 |
}; |
| 1720 |
Ok(value) |
| 1721 |
} |
| 1722 |
|
| 1723 |
fn possible_values( |
| 1724 |
&self, |
| 1725 |
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> { |
| 1726 |
Some(Box::new(Self::possible_values())) |
| 1727 |
} |
| 1728 |
} |
| 1729 |
|
| 1730 |
impl Default for BoolValueParser { |
| 1731 |
fn default() -> Self { |
| 1732 |
Self::new() |
| 1733 |
} |
| 1734 |
} |
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
#[derive(Copy, Clone, Debug)] |
| 1777 |
#[non_exhaustive] |
| 1778 |
pub struct FalseyValueParser {} |
| 1779 |
|
| 1780 |
impl FalseyValueParser { |
| 1781 |
|
| 1782 |
pub fn new() -> Self { |
| 1783 |
Self {} |
| 1784 |
} |
| 1785 |
|
| 1786 |
fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue> { |
| 1787 |
crate::util::TRUE_LITERALS |
| 1788 |
.iter() |
| 1789 |
.chain(crate::util::FALSE_LITERALS.iter()) |
| 1790 |
.copied() |
| 1791 |
.map(|l| crate::builder::PossibleValue::new(l).hide(l != "true" && l != "false")) |
| 1792 |
} |
| 1793 |
} |
| 1794 |
|
| 1795 |
impl TypedValueParser for FalseyValueParser { |
| 1796 |
type Value = bool; |
| 1797 |
|
| 1798 |
fn parse_ref( |
| 1799 |
&self, |
| 1800 |
cmd: &crate::Command, |
| 1801 |
_arg: Option<&crate::Arg>, |
| 1802 |
value: &std::ffi::OsStr, |
| 1803 |
) -> Result<Self::Value, crate::Error> { |
| 1804 |
let value = ok!(value.to_str().ok_or_else(|| { |
| 1805 |
crate::Error::invalid_utf8( |
| 1806 |
cmd, |
| 1807 |
crate::output::Usage::new(cmd).create_usage_with_title(&[]), |
| 1808 |
) |
| 1809 |
})); |
| 1810 |
let value = if value.is_empty() { |
| 1811 |
false |
| 1812 |
} else { |
| 1813 |
crate::util::str_to_bool(value).unwrap_or(true) |
| 1814 |
}; |
| 1815 |
Ok(value) |
| 1816 |
} |
| 1817 |
|
| 1818 |
fn possible_values( |
| 1819 |
&self, |
| 1820 |
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> { |
| 1821 |
Some(Box::new(Self::possible_values())) |
| 1822 |
} |
| 1823 |
} |
| 1824 |
|
| 1825 |
impl Default for FalseyValueParser { |
| 1826 |
fn default() -> Self { |
| 1827 |
Self::new() |
| 1828 |
} |
| 1829 |
} |
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
#[derive(Copy, Clone, Debug)] |
| 1876 |
#[non_exhaustive] |
| 1877 |
pub struct BoolishValueParser {} |
| 1878 |
|
| 1879 |
impl BoolishValueParser { |
| 1880 |
|
| 1881 |
pub fn new() -> Self { |
| 1882 |
Self {} |
| 1883 |
} |
| 1884 |
|
| 1885 |
fn possible_values() -> impl Iterator<Item = crate::builder::PossibleValue> { |
| 1886 |
crate::util::TRUE_LITERALS |
| 1887 |
.iter() |
| 1888 |
.chain(crate::util::FALSE_LITERALS.iter()) |
| 1889 |
.copied() |
| 1890 |
.map(|l| crate::builder::PossibleValue::new(l).hide(l != "true" && l != "false")) |
| 1891 |
} |
| 1892 |
} |
| 1893 |
|
| 1894 |
impl TypedValueParser for BoolishValueParser { |
| 1895 |
type Value = bool; |
| 1896 |
|
| 1897 |
fn parse_ref( |
| 1898 |
&self, |
| 1899 |
cmd: &crate::Command, |
| 1900 |
arg: Option<&crate::Arg>, |
| 1901 |
value: &std::ffi::OsStr, |
| 1902 |
) -> Result<Self::Value, crate::Error> { |
| 1903 |
let value = ok!(value.to_str().ok_or_else(|| { |
| 1904 |
crate::Error::invalid_utf8( |
| 1905 |
cmd, |
| 1906 |
crate::output::Usage::new(cmd).create_usage_with_title(&[]), |
| 1907 |
) |
| 1908 |
})); |
| 1909 |
let value = ok!(crate::util::str_to_bool(value).ok_or_else(|| { |
| 1910 |
let arg = arg |
| 1911 |
.map(|a| a.to_string()) |
| 1912 |
.unwrap_or_else(|| "...".to_owned()); |
| 1913 |
crate::Error::value_validation(arg, value.to_owned(), "value was not a boolean".into()) |
| 1914 |
.with_cmd(cmd) |
| 1915 |
})); |
| 1916 |
Ok(value) |
| 1917 |
} |
| 1918 |
|
| 1919 |
fn possible_values( |
| 1920 |
&self, |
| 1921 |
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> { |
| 1922 |
Some(Box::new(Self::possible_values())) |
| 1923 |
} |
| 1924 |
} |
| 1925 |
|
| 1926 |
impl Default for BoolishValueParser { |
| 1927 |
fn default() -> Self { |
| 1928 |
Self::new() |
| 1929 |
} |
| 1930 |
} |
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
#[derive(Copy, Clone, Debug)] |
| 1967 |
#[non_exhaustive] |
| 1968 |
pub struct NonEmptyStringValueParser {} |
| 1969 |
|
| 1970 |
impl NonEmptyStringValueParser { |
| 1971 |
|
| 1972 |
pub fn new() -> Self { |
| 1973 |
Self {} |
| 1974 |
} |
| 1975 |
} |
| 1976 |
|
| 1977 |
impl TypedValueParser for NonEmptyStringValueParser { |
| 1978 |
type Value = String; |
| 1979 |
|
| 1980 |
fn parse_ref( |
| 1981 |
&self, |
| 1982 |
cmd: &crate::Command, |
| 1983 |
arg: Option<&crate::Arg>, |
| 1984 |
value: &std::ffi::OsStr, |
| 1985 |
) -> Result<Self::Value, crate::Error> { |
| 1986 |
if value.is_empty() { |
| 1987 |
return Err(crate::Error::empty_value( |
| 1988 |
cmd, |
| 1989 |
&[], |
| 1990 |
arg.map(ToString::to_string) |
| 1991 |
.unwrap_or_else(|| "...".to_owned()), |
| 1992 |
)); |
| 1993 |
} |
| 1994 |
let value = ok!(value.to_str().ok_or_else(|| { |
| 1995 |
crate::Error::invalid_utf8( |
| 1996 |
cmd, |
| 1997 |
crate::output::Usage::new(cmd).create_usage_with_title(&[]), |
| 1998 |
) |
| 1999 |
})); |
| 2000 |
Ok(value.to_owned()) |
| 2001 |
} |
| 2002 |
} |
| 2003 |
|
| 2004 |
impl Default for NonEmptyStringValueParser { |
| 2005 |
fn default() -> Self { |
| 2006 |
Self::new() |
| 2007 |
} |
| 2008 |
} |
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
#[derive(Clone, Debug)] |
| 2014 |
pub struct MapValueParser<P, F> { |
| 2015 |
parser: P, |
| 2016 |
func: F, |
| 2017 |
} |
| 2018 |
|
| 2019 |
impl<P, F, T> MapValueParser<P, F> |
| 2020 |
where |
| 2021 |
P: TypedValueParser, |
| 2022 |
P::Value: Send + Sync + Clone, |
| 2023 |
F: Fn(P::Value) -> T + Clone, |
| 2024 |
T: Send + Sync + Clone, |
| 2025 |
{ |
| 2026 |
fn new(parser: P, func: F) -> Self { |
| 2027 |
Self { parser, func } |
| 2028 |
} |
| 2029 |
} |
| 2030 |
|
| 2031 |
impl<P, F, T> TypedValueParser for MapValueParser<P, F> |
| 2032 |
where |
| 2033 |
P: TypedValueParser, |
| 2034 |
P::Value: Send + Sync + Clone, |
| 2035 |
F: Fn(P::Value) -> T + Clone + Send + Sync + 'static, |
| 2036 |
T: Send + Sync + Clone, |
| 2037 |
{ |
| 2038 |
type Value = T; |
| 2039 |
|
| 2040 |
fn parse_ref( |
| 2041 |
&self, |
| 2042 |
cmd: &crate::Command, |
| 2043 |
arg: Option<&crate::Arg>, |
| 2044 |
value: &std::ffi::OsStr, |
| 2045 |
) -> Result<Self::Value, crate::Error> { |
| 2046 |
let value = ok!(self.parser.parse_ref(cmd, arg, value)); |
| 2047 |
let value = (self.func)(value); |
| 2048 |
Ok(value) |
| 2049 |
} |
| 2050 |
|
| 2051 |
fn parse( |
| 2052 |
&self, |
| 2053 |
cmd: &crate::Command, |
| 2054 |
arg: Option<&crate::Arg>, |
| 2055 |
value: std::ffi::OsString, |
| 2056 |
) -> Result<Self::Value, crate::Error> { |
| 2057 |
let value = ok!(self.parser.parse(cmd, arg, value)); |
| 2058 |
let value = (self.func)(value); |
| 2059 |
Ok(value) |
| 2060 |
} |
| 2061 |
|
| 2062 |
fn possible_values( |
| 2063 |
&self, |
| 2064 |
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> { |
| 2065 |
self.parser.possible_values() |
| 2066 |
} |
| 2067 |
} |
| 2068 |
|
| 2069 |
|
| 2070 |
|
| 2071 |
|
| 2072 |
#[derive(Clone, Debug)] |
| 2073 |
pub struct TryMapValueParser<P, F> { |
| 2074 |
parser: P, |
| 2075 |
func: F, |
| 2076 |
} |
| 2077 |
|
| 2078 |
impl<P, F, T, E> TryMapValueParser<P, F> |
| 2079 |
where |
| 2080 |
P: TypedValueParser, |
| 2081 |
P::Value: Send + Sync + Clone, |
| 2082 |
F: Fn(P::Value) -> Result<T, E> + Clone + Send + Sync + 'static, |
| 2083 |
T: Send + Sync + Clone, |
| 2084 |
E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 2085 |
{ |
| 2086 |
fn new(parser: P, func: F) -> Self { |
| 2087 |
Self { parser, func } |
| 2088 |
} |
| 2089 |
} |
| 2090 |
|
| 2091 |
impl<P, F, T, E> TypedValueParser for TryMapValueParser<P, F> |
| 2092 |
where |
| 2093 |
P: TypedValueParser, |
| 2094 |
P::Value: Send + Sync + Clone, |
| 2095 |
F: Fn(P::Value) -> Result<T, E> + Clone + Send + Sync + 'static, |
| 2096 |
T: Send + Sync + Clone, |
| 2097 |
E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 2098 |
{ |
| 2099 |
type Value = T; |
| 2100 |
|
| 2101 |
fn parse_ref( |
| 2102 |
&self, |
| 2103 |
cmd: &crate::Command, |
| 2104 |
arg: Option<&crate::Arg>, |
| 2105 |
value: &std::ffi::OsStr, |
| 2106 |
) -> Result<Self::Value, crate::Error> { |
| 2107 |
let mid_value = ok!(self.parser.parse_ref(cmd, arg, value)); |
| 2108 |
let value = ok!((self.func)(mid_value).map_err(|e| { |
| 2109 |
let arg = arg |
| 2110 |
.map(|a| a.to_string()) |
| 2111 |
.unwrap_or_else(|| "...".to_owned()); |
| 2112 |
crate::Error::value_validation(arg, value.to_string_lossy().into_owned(), e.into()) |
| 2113 |
.with_cmd(cmd) |
| 2114 |
})); |
| 2115 |
Ok(value) |
| 2116 |
} |
| 2117 |
|
| 2118 |
fn possible_values( |
| 2119 |
&self, |
| 2120 |
) -> Option<Box<dyn Iterator<Item = crate::builder::PossibleValue> + '_>> { |
| 2121 |
self.parser.possible_values() |
| 2122 |
} |
| 2123 |
} |
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
|
| 2129 |
|
| 2130 |
|
| 2131 |
|
| 2132 |
|
| 2133 |
|
| 2134 |
|
| 2135 |
|
| 2136 |
|
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
|
| 2141 |
|
| 2142 |
|
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
|
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
#[derive(Clone, Debug)] |
| 2159 |
pub struct UnknownArgumentValueParser { |
| 2160 |
arg: Option<Str>, |
| 2161 |
suggestions: Vec<StyledStr>, |
| 2162 |
} |
| 2163 |
|
| 2164 |
impl UnknownArgumentValueParser { |
| 2165 |
|
| 2166 |
pub fn suggest_arg(arg: impl Into<Str>) -> Self { |
| 2167 |
Self { |
| 2168 |
arg: Some(arg.into()), |
| 2169 |
suggestions: Default::default(), |
| 2170 |
} |
| 2171 |
} |
| 2172 |
|
| 2173 |
|
| 2174 |
pub fn suggest(text: impl Into<StyledStr>) -> Self { |
| 2175 |
Self { |
| 2176 |
arg: Default::default(), |
| 2177 |
suggestions: vec![text.into()], |
| 2178 |
} |
| 2179 |
} |
| 2180 |
|
| 2181 |
|
| 2182 |
pub fn and_suggest(mut self, text: impl Into<StyledStr>) -> Self { |
| 2183 |
self.suggestions.push(text.into()); |
| 2184 |
self |
| 2185 |
} |
| 2186 |
} |
| 2187 |
|
| 2188 |
impl TypedValueParser for UnknownArgumentValueParser { |
| 2189 |
type Value = String; |
| 2190 |
|
| 2191 |
fn parse_ref( |
| 2192 |
&self, |
| 2193 |
cmd: &crate::Command, |
| 2194 |
arg: Option<&crate::Arg>, |
| 2195 |
value: &std::ffi::OsStr, |
| 2196 |
) -> Result<Self::Value, crate::Error> { |
| 2197 |
TypedValueParser::parse_ref_(self, cmd, arg, value, ValueSource::CommandLine) |
| 2198 |
} |
| 2199 |
|
| 2200 |
fn parse_ref_( |
| 2201 |
&self, |
| 2202 |
cmd: &crate::Command, |
| 2203 |
arg: Option<&crate::Arg>, |
| 2204 |
_value: &std::ffi::OsStr, |
| 2205 |
source: ValueSource, |
| 2206 |
) -> Result<Self::Value, crate::Error> { |
| 2207 |
match source { |
| 2208 |
ValueSource::DefaultValue => { |
| 2209 |
TypedValueParser::parse_ref_(&StringValueParser::new(), cmd, arg, _value, source) |
| 2210 |
} |
| 2211 |
ValueSource::EnvVariable | ValueSource::CommandLine => { |
| 2212 |
let arg = match arg { |
| 2213 |
Some(arg) => arg.to_string(), |
| 2214 |
None => "..".to_owned(), |
| 2215 |
}; |
| 2216 |
let err = crate::Error::unknown_argument( |
| 2217 |
cmd, |
| 2218 |
arg, |
| 2219 |
self.arg.as_ref().map(|s| (s.as_str().to_owned(), None)), |
| 2220 |
false, |
| 2221 |
crate::output::Usage::new(cmd).create_usage_with_title(&[]), |
| 2222 |
); |
| 2223 |
#[cfg(feature = "error-context")] |
| 2224 |
let err = { |
| 2225 |
debug_assert_eq!( |
| 2226 |
err.get(crate::error::ContextKind::Suggested), |
| 2227 |
None, |
| 2228 |
"Assuming `Error::unknown_argument` doesn't apply any `Suggested` so we can without caution" |
| 2229 |
); |
| 2230 |
err.insert_context_unchecked( |
| 2231 |
crate::error::ContextKind::Suggested, |
| 2232 |
crate::error::ContextValue::StyledStrs(self.suggestions.clone()), |
| 2233 |
) |
| 2234 |
}; |
| 2235 |
Err(err) |
| 2236 |
} |
| 2237 |
} |
| 2238 |
} |
| 2239 |
} |
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
|
| 2246 |
|
| 2247 |
|
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
|
| 2254 |
|
| 2255 |
|
| 2256 |
|
| 2257 |
|
| 2258 |
|
| 2259 |
|
| 2260 |
|
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
|
| 2268 |
|
| 2269 |
|
| 2270 |
|
| 2271 |
|
| 2272 |
|
| 2273 |
|
| 2274 |
|
| 2275 |
|
| 2276 |
pub trait ValueParserFactory { |
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
type Parser; |
| 2282 |
|
| 2283 |
|
| 2284 |
fn value_parser() -> Self::Parser; |
| 2285 |
} |
| 2286 |
impl ValueParserFactory for String { |
| 2287 |
type Parser = ValueParser; |
| 2288 |
fn value_parser() -> Self::Parser { |
| 2289 |
ValueParser::string() |
| 2290 |
} |
| 2291 |
} |
| 2292 |
impl ValueParserFactory for Box<str> { |
| 2293 |
type Parser = MapValueParser<StringValueParser, fn(String) -> Box<str>>; |
| 2294 |
fn value_parser() -> Self::Parser { |
| 2295 |
StringValueParser::new().map(String::into_boxed_str) |
| 2296 |
} |
| 2297 |
} |
| 2298 |
impl ValueParserFactory for std::ffi::OsString { |
| 2299 |
type Parser = ValueParser; |
| 2300 |
fn value_parser() -> Self::Parser { |
| 2301 |
ValueParser::os_string() |
| 2302 |
} |
| 2303 |
} |
| 2304 |
impl ValueParserFactory for Box<std::ffi::OsStr> { |
| 2305 |
type Parser = |
| 2306 |
MapValueParser<OsStringValueParser, fn(std::ffi::OsString) -> Box<std::ffi::OsStr>>; |
| 2307 |
fn value_parser() -> Self::Parser { |
| 2308 |
OsStringValueParser::new().map(std::ffi::OsString::into_boxed_os_str) |
| 2309 |
} |
| 2310 |
} |
| 2311 |
impl ValueParserFactory for std::path::PathBuf { |
| 2312 |
type Parser = ValueParser; |
| 2313 |
fn value_parser() -> Self::Parser { |
| 2314 |
ValueParser::path_buf() |
| 2315 |
} |
| 2316 |
} |
| 2317 |
impl ValueParserFactory for Box<std::path::Path> { |
| 2318 |
type Parser = |
| 2319 |
MapValueParser<PathBufValueParser, fn(std::path::PathBuf) -> Box<std::path::Path>>; |
| 2320 |
fn value_parser() -> Self::Parser { |
| 2321 |
PathBufValueParser::new().map(std::path::PathBuf::into_boxed_path) |
| 2322 |
} |
| 2323 |
} |
| 2324 |
impl ValueParserFactory for bool { |
| 2325 |
type Parser = ValueParser; |
| 2326 |
fn value_parser() -> Self::Parser { |
| 2327 |
ValueParser::bool() |
| 2328 |
} |
| 2329 |
} |
| 2330 |
impl ValueParserFactory for u8 { |
| 2331 |
type Parser = RangedI64ValueParser<u8>; |
| 2332 |
fn value_parser() -> Self::Parser { |
| 2333 |
let start: i64 = u8::MIN.into(); |
| 2334 |
let end: i64 = u8::MAX.into(); |
| 2335 |
RangedI64ValueParser::new().range(start..=end) |
| 2336 |
} |
| 2337 |
} |
| 2338 |
impl ValueParserFactory for i8 { |
| 2339 |
type Parser = RangedI64ValueParser<i8>; |
| 2340 |
fn value_parser() -> Self::Parser { |
| 2341 |
let start: i64 = i8::MIN.into(); |
| 2342 |
let end: i64 = i8::MAX.into(); |
| 2343 |
RangedI64ValueParser::new().range(start..=end) |
| 2344 |
} |
| 2345 |
} |
| 2346 |
impl ValueParserFactory for u16 { |
| 2347 |
type Parser = RangedI64ValueParser<u16>; |
| 2348 |
fn value_parser() -> Self::Parser { |
| 2349 |
let start: i64 = u16::MIN.into(); |
| 2350 |
let end: i64 = u16::MAX.into(); |
| 2351 |
RangedI64ValueParser::new().range(start..=end) |
| 2352 |
} |
| 2353 |
} |
| 2354 |
impl ValueParserFactory for i16 { |
| 2355 |
type Parser = RangedI64ValueParser<i16>; |
| 2356 |
fn value_parser() -> Self::Parser { |
| 2357 |
let start: i64 = i16::MIN.into(); |
| 2358 |
let end: i64 = i16::MAX.into(); |
| 2359 |
RangedI64ValueParser::new().range(start..=end) |
| 2360 |
} |
| 2361 |
} |
| 2362 |
impl ValueParserFactory for u32 { |
| 2363 |
type Parser = RangedI64ValueParser<u32>; |
| 2364 |
fn value_parser() -> Self::Parser { |
| 2365 |
let start: i64 = u32::MIN.into(); |
| 2366 |
let end: i64 = u32::MAX.into(); |
| 2367 |
RangedI64ValueParser::new().range(start..=end) |
| 2368 |
} |
| 2369 |
} |
| 2370 |
impl ValueParserFactory for i32 { |
| 2371 |
type Parser = RangedI64ValueParser<i32>; |
| 2372 |
fn value_parser() -> Self::Parser { |
| 2373 |
let start: i64 = i32::MIN.into(); |
| 2374 |
let end: i64 = i32::MAX.into(); |
| 2375 |
RangedI64ValueParser::new().range(start..=end) |
| 2376 |
} |
| 2377 |
} |
| 2378 |
impl ValueParserFactory for u64 { |
| 2379 |
type Parser = RangedU64ValueParser<u64>; |
| 2380 |
fn value_parser() -> Self::Parser { |
| 2381 |
RangedU64ValueParser::new() |
| 2382 |
} |
| 2383 |
} |
| 2384 |
impl ValueParserFactory for i64 { |
| 2385 |
type Parser = RangedI64ValueParser<i64>; |
| 2386 |
fn value_parser() -> Self::Parser { |
| 2387 |
RangedI64ValueParser::new() |
| 2388 |
} |
| 2389 |
} |
| 2390 |
impl<T> ValueParserFactory for std::num::Saturating<T> |
| 2391 |
where |
| 2392 |
T: ValueParserFactory, |
| 2393 |
<T as ValueParserFactory>::Parser: TypedValueParser<Value = T>, |
| 2394 |
T: Send + Sync + Clone, |
| 2395 |
{ |
| 2396 |
type Parser = |
| 2397 |
MapValueParser<<T as ValueParserFactory>::Parser, fn(T) -> std::num::Saturating<T>>; |
| 2398 |
fn value_parser() -> Self::Parser { |
| 2399 |
T::value_parser().map(std::num::Saturating) |
| 2400 |
} |
| 2401 |
} |
| 2402 |
impl<T> ValueParserFactory for std::num::Wrapping<T> |
| 2403 |
where |
| 2404 |
T: ValueParserFactory, |
| 2405 |
<T as ValueParserFactory>::Parser: TypedValueParser<Value = T>, |
| 2406 |
T: Send + Sync + Clone, |
| 2407 |
{ |
| 2408 |
type Parser = MapValueParser<<T as ValueParserFactory>::Parser, fn(T) -> std::num::Wrapping<T>>; |
| 2409 |
fn value_parser() -> Self::Parser { |
| 2410 |
T::value_parser().map(std::num::Wrapping) |
| 2411 |
} |
| 2412 |
} |
| 2413 |
impl<T> ValueParserFactory for Box<T> |
| 2414 |
where |
| 2415 |
T: ValueParserFactory, |
| 2416 |
<T as ValueParserFactory>::Parser: TypedValueParser<Value = T>, |
| 2417 |
T: Send + Sync + Clone, |
| 2418 |
{ |
| 2419 |
type Parser = MapValueParser<<T as ValueParserFactory>::Parser, fn(T) -> Box<T>>; |
| 2420 |
fn value_parser() -> Self::Parser { |
| 2421 |
T::value_parser().map(Box::new) |
| 2422 |
} |
| 2423 |
} |
| 2424 |
impl<T> ValueParserFactory for std::sync::Arc<T> |
| 2425 |
where |
| 2426 |
T: ValueParserFactory, |
| 2427 |
<T as ValueParserFactory>::Parser: TypedValueParser<Value = T>, |
| 2428 |
T: Send + Sync + Clone, |
| 2429 |
{ |
| 2430 |
type Parser = MapValueParser<<T as ValueParserFactory>::Parser, fn(T) -> std::sync::Arc<T>>; |
| 2431 |
fn value_parser() -> Self::Parser { |
| 2432 |
T::value_parser().map(std::sync::Arc::new) |
| 2433 |
} |
| 2434 |
} |
| 2435 |
|
| 2436 |
#[doc(hidden)] |
| 2437 |
#[derive(Debug)] |
| 2438 |
#[allow(non_camel_case_types)] |
| 2439 |
pub struct _infer_ValueParser_for<T>(std::marker::PhantomData<T>); |
| 2440 |
|
| 2441 |
impl<T> _infer_ValueParser_for<T> { |
| 2442 |
#[doc(hidden)] |
| 2443 |
#[allow(clippy::new_without_default)] |
| 2444 |
pub fn new() -> Self { |
| 2445 |
Self(Default::default()) |
| 2446 |
} |
| 2447 |
} |
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
|
| 2452 |
#[doc(hidden)] |
| 2453 |
#[derive(Debug)] |
| 2454 |
pub struct _AnonymousValueParser(ValueParser); |
| 2455 |
|
| 2456 |
#[doc(hidden)] |
| 2457 |
pub mod impl_prelude { |
| 2458 |
use super::*; |
| 2459 |
|
| 2460 |
#[doc(hidden)] |
| 2461 |
#[allow(non_camel_case_types)] |
| 2462 |
pub trait _impls_ValueParserFactory: private::_impls_ValueParserFactorySealed { |
| 2463 |
type Parser; |
| 2464 |
fn value_parser(&self) -> Self::Parser; |
| 2465 |
} |
| 2466 |
impl<P: ValueParserFactory> _impls_ValueParserFactory for &&&&&&_infer_ValueParser_for<P> { |
| 2467 |
type Parser = P::Parser; |
| 2468 |
fn value_parser(&self) -> Self::Parser { |
| 2469 |
P::value_parser() |
| 2470 |
} |
| 2471 |
} |
| 2472 |
|
| 2473 |
#[doc(hidden)] |
| 2474 |
#[allow(non_camel_case_types)] |
| 2475 |
pub trait _impls_ValueEnum: private::_impls_ValueEnumSealed { |
| 2476 |
type Output; |
| 2477 |
|
| 2478 |
fn value_parser(&self) -> Self::Output; |
| 2479 |
} |
| 2480 |
impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> _impls_ValueEnum |
| 2481 |
for &&&&&_infer_ValueParser_for<E> |
| 2482 |
{ |
| 2483 |
type Output = EnumValueParser<E>; |
| 2484 |
|
| 2485 |
fn value_parser(&self) -> Self::Output { |
| 2486 |
EnumValueParser::<E>::new() |
| 2487 |
} |
| 2488 |
} |
| 2489 |
|
| 2490 |
#[doc(hidden)] |
| 2491 |
#[allow(non_camel_case_types)] |
| 2492 |
pub trait _impls_From_OsString: private::_impls_From_OsStringSealed { |
| 2493 |
fn value_parser(&self) -> _AnonymousValueParser; |
| 2494 |
} |
| 2495 |
impl<FromOsString> _impls_From_OsString for &&&&_infer_ValueParser_for<FromOsString> |
| 2496 |
where |
| 2497 |
FromOsString: From<std::ffi::OsString> + std::any::Any + Clone + Send + Sync + 'static, |
| 2498 |
{ |
| 2499 |
fn value_parser(&self) -> _AnonymousValueParser { |
| 2500 |
_AnonymousValueParser( |
| 2501 |
OsStringValueParser::new() |
| 2502 |
.map(|s| FromOsString::from(s)) |
| 2503 |
.into(), |
| 2504 |
) |
| 2505 |
} |
| 2506 |
} |
| 2507 |
|
| 2508 |
#[doc(hidden)] |
| 2509 |
#[allow(non_camel_case_types)] |
| 2510 |
pub trait _impls_From_OsStr: private::_impls_From_OsStrSealed { |
| 2511 |
fn value_parser(&self) -> _AnonymousValueParser; |
| 2512 |
} |
| 2513 |
impl<FromOsStr> _impls_From_OsStr for &&&_infer_ValueParser_for<FromOsStr> |
| 2514 |
where |
| 2515 |
FromOsStr: |
| 2516 |
for<'s> From<&'s std::ffi::OsStr> + std::any::Any + Clone + Send + Sync + 'static, |
| 2517 |
{ |
| 2518 |
fn value_parser(&self) -> _AnonymousValueParser { |
| 2519 |
_AnonymousValueParser( |
| 2520 |
OsStringValueParser::new() |
| 2521 |
.map(|s| FromOsStr::from(&s)) |
| 2522 |
.into(), |
| 2523 |
) |
| 2524 |
} |
| 2525 |
} |
| 2526 |
|
| 2527 |
#[doc(hidden)] |
| 2528 |
#[allow(non_camel_case_types)] |
| 2529 |
pub trait _impls_From_String: private::_impls_From_StringSealed { |
| 2530 |
fn value_parser(&self) -> _AnonymousValueParser; |
| 2531 |
} |
| 2532 |
impl<FromString> _impls_From_String for &&_infer_ValueParser_for<FromString> |
| 2533 |
where |
| 2534 |
FromString: From<String> + std::any::Any + Clone + Send + Sync + 'static, |
| 2535 |
{ |
| 2536 |
fn value_parser(&self) -> _AnonymousValueParser { |
| 2537 |
_AnonymousValueParser(StringValueParser::new().map(|s| FromString::from(s)).into()) |
| 2538 |
} |
| 2539 |
} |
| 2540 |
|
| 2541 |
#[doc(hidden)] |
| 2542 |
#[allow(non_camel_case_types)] |
| 2543 |
pub trait _impls_From_str: private::_impls_From_strSealed { |
| 2544 |
fn value_parser(&self) -> _AnonymousValueParser; |
| 2545 |
} |
| 2546 |
impl<FromStr> _impls_From_str for &_infer_ValueParser_for<FromStr> |
| 2547 |
where |
| 2548 |
FromStr: for<'s> From<&'s str> + std::any::Any + Clone + Send + Sync + 'static, |
| 2549 |
{ |
| 2550 |
fn value_parser(&self) -> _AnonymousValueParser { |
| 2551 |
_AnonymousValueParser(StringValueParser::new().map(|s| FromStr::from(&s)).into()) |
| 2552 |
} |
| 2553 |
} |
| 2554 |
|
| 2555 |
#[doc(hidden)] |
| 2556 |
#[allow(non_camel_case_types)] |
| 2557 |
pub trait _impls_FromStr: private::_impls_FromStrSealed { |
| 2558 |
fn value_parser(&self) -> _AnonymousValueParser; |
| 2559 |
} |
| 2560 |
impl<Parse> _impls_FromStr for _infer_ValueParser_for<Parse> |
| 2561 |
where |
| 2562 |
Parse: std::str::FromStr + std::any::Any + Clone + Send + Sync + 'static, |
| 2563 |
<Parse as std::str::FromStr>::Err: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 2564 |
{ |
| 2565 |
fn value_parser(&self) -> _AnonymousValueParser { |
| 2566 |
let func: fn(&str) -> Result<Parse, <Parse as std::str::FromStr>::Err> = |
| 2567 |
Parse::from_str; |
| 2568 |
_AnonymousValueParser(ValueParser::new(func)) |
| 2569 |
} |
| 2570 |
} |
| 2571 |
} |
| 2572 |
|
| 2573 |
|
| 2574 |
|
| 2575 |
|
| 2576 |
|
| 2577 |
|
| 2578 |
|
| 2579 |
|
| 2580 |
|
| 2581 |
|
| 2582 |
|
| 2583 |
|
| 2584 |
|
| 2585 |
|
| 2586 |
|
| 2587 |
|
| 2588 |
|
| 2589 |
|
| 2590 |
|
| 2591 |
|
| 2592 |
|
| 2593 |
|
| 2594 |
|
| 2595 |
|
| 2596 |
|
| 2597 |
|
| 2598 |
|
| 2599 |
|
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
|
| 2605 |
|
| 2606 |
|
| 2607 |
|
| 2608 |
|
| 2609 |
|
| 2610 |
|
| 2611 |
|
| 2612 |
|
| 2613 |
|
| 2614 |
|
| 2615 |
|
| 2616 |
|
| 2617 |
|
| 2618 |
|
| 2619 |
|
| 2620 |
|
| 2621 |
|
| 2622 |
|
| 2623 |
|
| 2624 |
|
| 2625 |
#[macro_export] |
| 2626 |
macro_rules! value_parser { |
| 2627 |
($name:ty) => {{ |
| 2628 |
use $crate::builder::impl_prelude::*; |
| 2629 |
let auto = $crate::builder::_infer_ValueParser_for::<$name>::new(); |
| 2630 |
(&&&&&&auto).value_parser() |
| 2631 |
}}; |
| 2632 |
} |
| 2633 |
|
| 2634 |
mod private { |
| 2635 |
use super::*; |
| 2636 |
|
| 2637 |
#[allow(non_camel_case_types)] |
| 2638 |
pub trait _impls_ValueParserFactorySealed {} |
| 2639 |
impl<P: ValueParserFactory> _impls_ValueParserFactorySealed for &&&&&&_infer_ValueParser_for<P> {} |
| 2640 |
|
| 2641 |
#[allow(non_camel_case_types)] |
| 2642 |
pub trait _impls_ValueEnumSealed {} |
| 2643 |
impl<E: crate::ValueEnum> _impls_ValueEnumSealed for &&&&&_infer_ValueParser_for<E> {} |
| 2644 |
|
| 2645 |
#[allow(non_camel_case_types)] |
| 2646 |
pub trait _impls_From_OsStringSealed {} |
| 2647 |
impl<FromOsString> _impls_From_OsStringSealed for &&&&_infer_ValueParser_for<FromOsString> where |
| 2648 |
FromOsString: From<std::ffi::OsString> + std::any::Any + Send + Sync + 'static |
| 2649 |
{ |
| 2650 |
} |
| 2651 |
|
| 2652 |
#[allow(non_camel_case_types)] |
| 2653 |
pub trait _impls_From_OsStrSealed {} |
| 2654 |
impl<FromOsStr> _impls_From_OsStrSealed for &&&_infer_ValueParser_for<FromOsStr> where |
| 2655 |
FromOsStr: for<'s> From<&'s std::ffi::OsStr> + std::any::Any + Send + Sync + 'static |
| 2656 |
{ |
| 2657 |
} |
| 2658 |
|
| 2659 |
#[allow(non_camel_case_types)] |
| 2660 |
pub trait _impls_From_StringSealed {} |
| 2661 |
impl<FromString> _impls_From_StringSealed for &&_infer_ValueParser_for<FromString> where |
| 2662 |
FromString: From<String> + std::any::Any + Send + Sync + 'static |
| 2663 |
{ |
| 2664 |
} |
| 2665 |
|
| 2666 |
#[allow(non_camel_case_types)] |
| 2667 |
pub trait _impls_From_strSealed {} |
| 2668 |
impl<FromStr> _impls_From_strSealed for &_infer_ValueParser_for<FromStr> where |
| 2669 |
FromStr: for<'s> From<&'s str> + std::any::Any + Send + Sync + 'static |
| 2670 |
{ |
| 2671 |
} |
| 2672 |
|
| 2673 |
#[allow(non_camel_case_types)] |
| 2674 |
pub trait _impls_FromStrSealed {} |
| 2675 |
impl<Parse> _impls_FromStrSealed for _infer_ValueParser_for<Parse> |
| 2676 |
where |
| 2677 |
Parse: std::str::FromStr + std::any::Any + Send + Sync + 'static, |
| 2678 |
<Parse as std::str::FromStr>::Err: Into<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 2679 |
{ |
| 2680 |
} |
| 2681 |
} |
| 2682 |
|
| 2683 |
#[cfg(test)] |
| 2684 |
mod test { |
| 2685 |
use super::*; |
| 2686 |
|
| 2687 |
#[test] |
| 2688 |
fn ensure_typed_applies_to_parse() { |
| 2689 |
fn parse(_: &str) -> Result<usize, std::io::Error> { |
| 2690 |
Ok(10) |
| 2691 |
} |
| 2692 |
let cmd = crate::Command::new("cmd"); |
| 2693 |
let arg = None; |
| 2694 |
assert_eq!( |
| 2695 |
TypedValueParser::parse_ref(&parse, &cmd, arg, std::ffi::OsStr::new("foo")).unwrap(), |
| 2696 |
10 |
| 2697 |
); |
| 2698 |
} |
| 2699 |
} |