parse_arg 1.0.1

Traits and implementations for parsing command-line arguments.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
295
296
297
298
299
300
301
302
303
304
305
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
334
335
336
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
365
366
367
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
396
397
398
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
427
428
429
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
458
459
460
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
489
490
491
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
521
522
523
524
525
526
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
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
//! This crate provides traits to bridge various libraries providing parsable types with libraries
//! providing command line parsing implementations.
//! The core of the crate is `ParseArg` trait. It works much like `FromStr` trait, but with
//! these differences:
//! 
//! * It operates on `&OsStr` instead of `&str`, thus allowing wider range of possible inputs.
//! * It provides `parse_owned_arg()` method which can be specialized to avoid allocations.
//! * It requires the implementor to provide `describe_type()` to print human-readable description.
//!   of expected input.
//! * It requires the error type to implement `Display` in order to enable user-friendly interface.
//! 
//! Further, the crate provides `ParseArgFromStr` trait, which causes any type implementing it to
//! auto-implement `ParseArg` trait. This is handy when implementing `ParseArg` for types that
//! already have `FromStr` implemented, so that boilerplate is reduced.
//!
//! Any libraries that wish to help their consumers implement parsing their types from command line
//! may add this crate as an optional dependency and implement the `ParseArg` trait (directly or
//! indirectly) for their types.
//!
//! Any binaries wishing to use these traits should enable the `parse_arg` feature of the
//! librariess that use this crate and use a CLI parses implementation crate that uses it too.
//! Currently the only known implementation is [`configure_me`](https://docs.rs/configure_me) which
//! is also capable of parsing configuration files.
//!
//! # MSRV
//!
//! The minimum supported Rust version of the crate is 1.63 and will always be whichever Rust
//! version the current Debian stable (12 - Bookworm at the time of writing) supports.

#![deny(missing_docs)]

use std::fmt;
use std::str::FromStr;
use std::ffi::{OsStr, OsString};

/// Defines an interface for types that can be created by parsing command-line argument.
///
/// This trait is similar to `FromStr`. See the crate documentation for list of important
/// differences.
pub trait ParseArg: Sized {
    /// Type returned in the `Err` variant of `Result` when parsing fails.
    type Error: fmt::Display;

    /// Parses the argument.
    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error>;

    /// Writes human-readable description of the type to the writer.
    ///
    /// The description should be in English composed in such way that appending it to string "The
    /// input must be " sounds natural. E.g. if the description is "a number", the resulting phrase
    /// will be "The input must be a number".
    ///
    /// This way, it can be used as a documentation/hint for the user.
    fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result;

    /// Parses the argument consuming it.
    ///
    /// Implementors are encouraged to specialize this method if the resulting implementation is
    /// more performant - e.g. if it avoids allocation.
    ///
    /// The users are encouraged to use this method instead of `parse_arg` if they own the string
    /// and will not need it after call to this function. (Typical when working with
    /// `std::env::args_os()`.)
    fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
        Self::parse_arg(&arg)
    }
}

impl<T: ParseArg> ParseArg for std::cell::Cell<T> {
    type Error = T::Error;

    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
        T::parse_arg(arg).map(Into::into)
    }

    fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
        T::describe_type(writer)
    }

    fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
        T::parse_owned_arg(arg).map(Into::into)
    }
}

impl<T: ParseArg> ParseArg for std::cell::RefCell<T> {
    type Error = T::Error;

    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
        T::parse_arg(arg).map(Into::into)
    }

    fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
        T::describe_type(writer)
    }

    fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
        T::parse_owned_arg(arg).map(Into::into)
    }
}

impl<T: ParseArg> ParseArg for std::cell::UnsafeCell<T> {
    type Error = T::Error;

    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
        T::parse_arg(arg).map(Into::into)
    }

    fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
        T::describe_type(writer)
    }

    fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
        T::parse_owned_arg(arg).map(Into::into)
    }
}

impl<T: ParseArg> ParseArg for std::sync::Mutex<T> {
    type Error = T::Error;

    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
        T::parse_arg(arg).map(Into::into)
    }

    fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
        T::describe_type(writer)
    }

    fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
        T::parse_owned_arg(arg).map(Into::into)
    }
}

impl<T: ParseArg> ParseArg for std::sync::RwLock<T> {
    type Error = T::Error;

    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
        T::parse_arg(arg).map(Into::into)
    }

    fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
        T::describe_type(writer)
    }

    fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
        T::parse_owned_arg(arg).map(Into::into)
    }
}

impl<T: ParseArg> ParseArg for std::mem::ManuallyDrop<T> {
    type Error = T::Error;

    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
        T::parse_arg(arg).map(std::mem::ManuallyDrop::new)
    }

    fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
        T::describe_type(writer)
    }

    fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
        T::parse_owned_arg(arg).map(std::mem::ManuallyDrop::new)
    }
}

impl<T: ParseArg> ParseArg for std::rc::Rc<T> {
    type Error = T::Error;

    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
        T::parse_arg(arg).map(Into::into)
    }

    fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
        T::describe_type(writer)
    }

    fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
        T::parse_owned_arg(arg).map(Into::into)
    }
}

#[cfg(target_has_atomic = "ptr")]
impl<T: ParseArg> ParseArg for std::sync::Arc<T> {
    type Error = T::Error;

    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
        T::parse_arg(arg).map(Into::into)
    }

    fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
        T::describe_type(writer)
    }

    fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
        T::parse_owned_arg(arg).map(Into::into)
    }
}

/// Possible error when parsing certain arguments.
///
/// This is used for bridging implementations of `FromStr`, because they require UTF-8 encoded inputs.
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub enum ParseArgError<E> {
    /// Parsing as implemented by `FromStr` failed.
    FromStr(E),
    /// The input isn't UTF-8 encoded.
    InvalidUtf8,
}

impl<E: fmt::Display> fmt::Display for ParseArgError<E> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            // We flatten the error since we don't have anything to say about it
            ParseArgError::FromStr(err) => fmt::Display::fmt(err, f),
            ParseArgError::InvalidUtf8 => write!(f, "invalid UTF-8 encoding"),
        }
    }
}

/// `Debug` is implemented via `Display` in order to make using `?` operator in `main()` nice.
impl<E: fmt::Display> fmt::Debug for ParseArgError<E> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl<E: fmt::Display> From<E> for ParseArgError<E> {
    fn from(error: E) -> Self {
        ParseArgError::FromStr(error)
    }
}

impl<E: std::error::Error> std::error::Error for ParseArgError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            // We flatten the error since we don't have anything to say about it
            ParseArgError::FromStr(error) => error.source(),
            ParseArgError::InvalidUtf8 => None,
        }
    }
}

/// Shorthand for implementing `ParseArg` for types that already implement `FromStr`.
///
/// This trait allows to implement `ParseArg` cheaply, just by providing the description. Prefer
/// this approach if your type already impls `FromStr` without copying the string. In case the
/// implementation can be made more performant by directly implementing `ParseArg`, prefer direct
/// implementation. (This is what `String` does for example.)
///
/// **This trait should only be implemented - do not use it as a bound! Use `ParseArg` as a bound
/// because it is more general and provides same features.**
pub trait ParseArgFromStr: FromStr where <Self as FromStr>::Err: fmt::Display {
    /// Writes human-readable description of the type to the writer.
    ///
    /// For full information, read the documentation for `ParseArgs::describe_type`.
    ///
    /// `ParseArgs::describe_type` is delegated to this method when `ParseArgFromStr` is
    /// implemented.
    fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result;
}

impl<T> ParseArg for T where T: ParseArgFromStr, <T as FromStr>::Err: fmt::Display {
    type Error = ParseArgError<<T as FromStr>::Err>;

    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
        arg.to_str().ok_or(ParseArgError::InvalidUtf8)?.parse().map_err(Into::into)
    }

    fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
        <Self as ParseArgFromStr>::describe_type(writer)
    }
}

/// Optimized implementation - doesn't allocate in `parse_owned_arg`.
impl ParseArg for String {
    type Error = ParseArgError<std::string::ParseError>;

    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
        arg.to_str().ok_or(ParseArgError::InvalidUtf8).map(Into::into).map_err(Into::into)
    }

    fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
        write!(writer, "a UTF-8 encoded string")
    }

    fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
        arg.into_string().map_err(|_| ParseArgError::InvalidUtf8)
    }
}

/// This implementation is a no-op or clone, since `OsString` is already `OsString`.
impl ParseArg for OsString {
    type Error = std::string::ParseError;

    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
        Ok(arg.into())
    }

    fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
        write!(writer, "any string")
    }

    fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
        Ok(arg)
    }
}

/// To my knowledge this is a no-op or clone.
impl ParseArg for std::path::PathBuf {
    type Error = std::string::ParseError;

    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
        Ok(arg.into())
    }

    fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
        write!(writer, "a path")
    }

    fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
        Ok(arg.into())
    }
}

macro_rules! impl_unsized {
    ($($type:ty),*) => {
        $(
            /// This implementation simply parses the "fat" owned type and converts it into the box.
            impl ParseArg for Box<$type> {
                type Error = <<$type as ToOwned>::Owned as ParseArg>::Error;

                fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
                    <<$type as ToOwned>::Owned as ParseArg>::parse_arg(arg).map(Into::into)
                }

                fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
                    <<$type as ToOwned>::Owned as ParseArg>::describe_type(writer)
                }

                fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
                    <<$type as ToOwned>::Owned as ParseArg>::parse_owned_arg(arg).map(Into::into)
                }
            }

            impl ParseArg for std::borrow::Cow<'static, $type> {
                type Error = <<$type as ToOwned>::Owned as ParseArg>::Error;

                fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
                    <<$type as ToOwned>::Owned as ParseArg>::parse_arg(arg).map(Into::into)
                }

                fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
                    <<$type as ToOwned>::Owned as ParseArg>::describe_type(writer)
                }

                fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> {
                    <<$type as ToOwned>::Owned as ParseArg>::parse_owned_arg(arg).map(Into::into)
                }
            }
        )*
    }
}

impl_unsized!(str, std::path::Path, std::ffi::OsStr);

macro_rules! impl_trivial_rc {
    ($($type:ty),*) => {
        $(
            impl ParseArg for std::rc::Rc<$type> {
                type Error = std::convert::Infallible;

                fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
                    let arg: &$type = arg.as_ref();
                    Ok(arg.into())
                }

                fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
                    <<$type as ToOwned>::Owned as ParseArg>::describe_type(writer)
                }
            }

            #[cfg(target_has_atomic = "ptr")]
            impl ParseArg for std::sync::Arc<$type> {
                type Error = std::convert::Infallible;

                fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
                    let arg: &$type = arg.as_ref();
                    Ok(arg.into())
                }

                fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
                    <<$type as ToOwned>::Owned as ParseArg>::describe_type(writer)
                }
            }
        )*
    }
}

impl_trivial_rc!(std::path::Path, std::ffi::OsStr);

impl ParseArg for std::rc::Rc<str> {
    type Error = ParseArgError<std::string::ParseError>;

    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
        arg.to_str().map(Into::into).ok_or(ParseArgError::InvalidUtf8)
    }

    fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
        String::describe_type(writer)
    }
}

#[cfg(target_has_atomic = "ptr")]
impl ParseArg for std::sync::Arc<str> {
    type Error = ParseArgError<std::string::ParseError>;

    fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
        arg.to_str().map(Into::into).ok_or(ParseArgError::InvalidUtf8)
    }

    fn describe_type<W: fmt::Write>(writer: W) -> fmt::Result {
        String::describe_type(writer)
    }
}

macro_rules! impl_unsigned {
    ($($type:ty),*) => {
        $(
            impl ParseArgFromStr for $type {
                fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
                    write!(writer, "a non-negative integer up to {}", <$type>::MAX)
                }
            }
        )*
    }
}

macro_rules! impl_signed {
    ($($type:ty),*) => {
        $(
            impl ParseArgFromStr for $type {
                fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
                    write!(writer, "an integer at least {} and up to {}", <$type>::MIN, <$type>::MAX)
                }
            }
        )*
    }
}

const fn max_from_len(len: usize) -> u128 {
    assert!(len <= 16);

    let mut bytes = [0; 16];
    let mut i = 0;
    while i < len {
        bytes[i] = 0xFF;
        i += 1;
    }
    u128::from_le_bytes(bytes)
}

const fn max_from_len_signed(len: usize) -> i128 {
    (max_from_len(len) / 2) as i128
}

const fn min_from_len_signed(len: usize) -> i128 {
    -((max_from_len(len) / 2) as i128) - 1
}

macro_rules! impl_non_zero_unsigned {
    ($($type:ident),*) => {
        $(
            impl ParseArgFromStr for std::num::$type {
                fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
                    const MAX: u128 = max_from_len(std::mem::size_of::<std::num::$type>());
                    write!(writer, "a positive (non-zero) integer up to {}", MAX)
                }
            }
        )*
    }
}

macro_rules! impl_non_zero_signed {
    ($($type:ident),*) => {
        $(
            impl ParseArgFromStr for std::num::$type {
                fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
                    const MAX: i128 = max_from_len_signed(std::mem::size_of::<std::num::$type>());
                    const MIN: i128 = min_from_len_signed(std::mem::size_of::<std::num::$type>());
                    write!(writer, "a non-zero integer at least {} and up to {}", MIN, MAX)
                }
            }
        )*
    }
}

fn parse_and_convert<Intermediate: FromStr, Res, F: FnOnce(Intermediate) -> Res>(s: &str, f: F) -> Result<Res, Intermediate::Err> {
    s.parse().map(f)
}

macro_rules! impl_atomic_unsigned {
    ($($type:ident => $atomic_kind:literal),*) => {
        $(
            #[cfg(target_has_atomic = $atomic_kind)]
            impl ParseArg for std::sync::atomic::$type {
                type Error = ParseArgError<std::num::ParseIntError>;

                fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
                    parse_and_convert(arg.to_str().ok_or(ParseArgError::InvalidUtf8)?, std::sync::atomic::$type::new).map_err(Into::into)
                }

                fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
                    const MAX: u128 = max_from_len(std::mem::size_of::<std::sync::atomic::$type>());
                    write!(writer, "a non-negative integer up to {}", MAX)
                }
            }
        )*
    }
}

macro_rules! impl_atomic_signed {
    ($($type:ident => $atomic_kind:literal),*) => {
        $(
            #[cfg(target_has_atomic = $atomic_kind)]
            impl ParseArg for std::sync::atomic::$type {
                type Error = ParseArgError<std::num::ParseIntError>;

                fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> {
                    parse_and_convert(arg.to_str().ok_or(ParseArgError::InvalidUtf8)?, std::sync::atomic::$type::new).map_err(Into::into)
                }

                fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
                    const MAX: i128 = max_from_len_signed(std::mem::size_of::<std::sync::atomic::$type>());
                    const MIN: i128 = min_from_len_signed(std::mem::size_of::<std::sync::atomic::$type>());
                    write!(writer, "an integer at least {} and up to {}", MIN, MAX)
                }
            }
        )*
    }
}

macro_rules! impl_float {
    ($($type:ident),*) => {
        $(
            impl ParseArgFromStr for $type {
                fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
                    write!(writer, "a real number at least {} and up to {}", std::$type::MIN, std::$type::MAX)
                }
            }
        )*
    }
}

impl_unsigned! { u8, u16, u32, u64, u128, usize }
impl_signed! { i8, i16, i32, i64, i128, isize }
impl_non_zero_unsigned! { NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize }
impl_non_zero_signed! { NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize }
impl_atomic_unsigned! { AtomicU8 => "8", AtomicU16 => "16", AtomicU32 => "32", AtomicU64 => "64", AtomicUsize => "ptr" }
impl_atomic_signed! { AtomicI8 => "8", AtomicI16 => "16", AtomicI32 => "32", AtomicI64 => "64", AtomicIsize => "ptr" }
impl_float! { f32, f64 }

impl ParseArgFromStr for std::net::IpAddr {
    fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
        write!(writer, "an IP address (either version 4 or 6)")
    }
}

impl ParseArgFromStr for std::net::Ipv4Addr {
    fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
        write!(writer, "a version 4 IP address")
    }
}

impl ParseArgFromStr for std::net::Ipv6Addr {
    fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
        write!(writer, "a version 6 IP address")
    }
}

impl ParseArgFromStr for std::net::SocketAddr {
    fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
        write!(writer, "a version 4 or 6 network socket address (IP:port)")
    }
}

impl ParseArgFromStr for std::net::SocketAddrV4 {
    fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
        write!(writer, "a version 4 socket address (IP:port)")
    }
}

impl ParseArgFromStr for std::net::SocketAddrV6 {
    fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
        write!(writer, "a version 6 socket address (IP:port)")
    }
}

impl ParseArgFromStr for bool {
    fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
        write!(writer, "a boolean (true or false)")
    }
}

impl ParseArgFromStr for char {
    fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
        write!(writer, "a single character (Unicode code point)")
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn numbers() {
        use ::ParseArg;

        let val: u8 = ParseArg::parse_arg("42".as_ref()).unwrap();
        assert_eq!(val, 42);
        let val: u16 = ParseArg::parse_arg("42".as_ref()).unwrap();
        assert_eq!(val, 42);
        let val: u32 = ParseArg::parse_arg("42".as_ref()).unwrap();
        assert_eq!(val, 42);
        let val: u64 = ParseArg::parse_arg("42".as_ref()).unwrap();
        assert_eq!(val, 42);
        let val: u128 = ParseArg::parse_arg("42".as_ref()).unwrap();
        assert_eq!(val, 42);

        let val: usize = ParseArg::parse_arg("42".as_ref()).unwrap();
        assert_eq!(val, 42);
        let val: i8 = ParseArg::parse_arg("42".as_ref()).unwrap();
        assert_eq!(val, 42);
        let val: i16 = ParseArg::parse_arg("42".as_ref()).unwrap();
        assert_eq!(val, 42);
        let val: i32 = ParseArg::parse_arg("42".as_ref()).unwrap();
        assert_eq!(val, 42);
        let val: i64 = ParseArg::parse_arg("42".as_ref()).unwrap();
        assert_eq!(val, 42);
        let val: i128 = ParseArg::parse_arg("42".as_ref()).unwrap();
        assert_eq!(val, 42);

        let val: isize = ParseArg::parse_arg("-42".as_ref()).unwrap();
        assert_eq!(val, -42);
        let val: i8 = ParseArg::parse_arg("-42".as_ref()).unwrap();
        assert_eq!(val, -42);
        let val: i16 = ParseArg::parse_arg("-42".as_ref()).unwrap();
        assert_eq!(val, -42);
        let val: i32 = ParseArg::parse_arg("-42".as_ref()).unwrap();
        assert_eq!(val, -42);
        let val: i64 = ParseArg::parse_arg("-42".as_ref()).unwrap();
        assert_eq!(val, -42);
        let val: i128 = ParseArg::parse_arg("-42".as_ref()).unwrap();
        assert_eq!(val, -42);
        let val: isize = ParseArg::parse_arg("42".as_ref()).unwrap();
        assert_eq!(val, 42);

        let val: f32 = ParseArg::parse_arg("42.42".as_ref()).unwrap();
        assert_eq!(val, 42.42);
        let val: f64 = ParseArg::parse_arg("42.42".as_ref()).unwrap();
        assert_eq!(val, 42.42);
    }

    #[test]
    fn min_max() {
        fn check<T: Into<u128>>(max: T) {
            let max = max.into();
            assert_eq!(super::max_from_len(std::mem::size_of::<T>()), max);
        }

        fn check_signed<T: Into<i128>>(min: T, max: T) {
            let min = min.into();
            let max = max.into();
            assert_eq!(super::min_from_len_signed(std::mem::size_of::<T>()), min);
            assert_eq!(super::max_from_len_signed(std::mem::size_of::<T>()), max);
        }

        check(u8::MAX);
        check(u16::MAX);
        check(u128::MAX);
        check_signed(i8::MIN, i8::MAX);
        check_signed(i16::MIN, i16::MAX);
        check_signed(i128::MIN, i128::MAX);
    }
}