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
// SPDX-License-Identifier: EUPL-1.2
//! turning a raw `&str` into a typed value.
//!
//! no blanket impl over `FromStr`: that would block a bespoke [`FromArg`] for
//! any type that already has `FromStr` (uuids, ip addrs). instead std scalars
//! are wired up here, [`from_str!`] opts a `FromStr` type in with one line, and
//! you hand-write [`FromArg`] for anything exotic (hex colours, durations)
//! without a coherence fight.
use fmt;
use crate*;
/// a value that would not parse, plus context for the message. the parser wraps
/// it into [`crate::Error::Value`] once it knows which arg it came from.
/// parse a single token into `Self`. impl it for your own field types:
///
/// ```
/// use pound::{
/// FromArg,
/// ValueError,
/// };
///
/// struct Rgb(u8, u8, u8);
///
/// impl FromArg for Rgb {
/// fn from_arg(s: &str) -> Result<Self, ValueError> {
/// let s = s.strip_prefix('#').unwrap_or(s);
/// if s.len() != 6 {
/// return Err(ValueError::new(s, "expected a 6-digit hex colour"));
/// }
/// let byte =
/// |i: usize| u8::from_str_radix(&s[i..i + 2], 16).map_err(|e| ValueError::new(s, e));
/// Ok(Rgb(byte(0)?, byte(2)?, byte(4)?))
/// }
/// }
/// ```
/// impl [`FromArg`] for one or more types via their [`FromStr`].
///
/// ```
/// # struct Uuid;
/// # impl std::str::FromStr for Uuid {
/// # type Err = std::convert::Infallible;
/// # fn from_str(_: &str) -> Result<Self, Self::Err> { Ok(Uuid) }
/// # }
/// pound::from_str!(Uuid);
/// ```
///
/// [`FromStr`]: std::str::FromStr
from_str!
// `PathBuf` lives in `std` (it wraps `OsString`), so its value impl is the one
// scalar that cannot ride along in a `no_std` build.
from_str!