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
// Copyright © 2024 Claire Bts
//
// This file is part of CLIP
//
// CLIP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// CLIP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
use TokenStream;
/// FromStr Derive attribute
///
/// It is the derive macro for the FromStr trait
///
/// Only for Unit enum, any other type is unsupported and will an error at compile time.
///
/// This macro is just a conveniant way to parse a string into the derived enumeration. It is case
/// insensitive. The behavior is actually the same as for the TryParse trait.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate clip_derive;
/// use clip_derive::FromStr;
/// use std::str::FromStr;
///
/// ##[derive(Debug, PartialEq, FromStr)]
/// enum Random { One, Two, Three }
///
/// # fn main() {
/// assert_eq!(Random::from_str("one"), Ok(Random::One));
/// assert_eq!(Random::from_str("THREE"), Ok(Random::Three));
/// assert!(Random::from_str("Four").is_err());
/// # }
/// ```
/// TryParse devive attribute
///
/// It is the Derive macro for the TryParse trait
///
/// Supports Struct and Enum but not Union
///
/// The try_parse helper attribute may be associated to a field to use the TryParse::try_parse
/// method instead of the default str.parse one.
///
/// Since the FromStr and TryParse trait cannot be checked for a certain trait, generated errors
/// can lead to crypted error messages. However since there is a few number of errors' sources:
/// - trying to derive an union, which should give an explicit error
/// - having an attributeless fields for which the type doesn't implement FromStr
/// - having a `#[try_parse]` attributed field for which the type doesn't implement TryParse
/// If the error seems hard to decrypt, chances are high that the problem is one of the last two.
///
/// # Struct
/// Only positional arguments are supported, meaning there is no concept of optional or flags for
/// now. The position of a field within itself will determine the expected position within a parsed
/// line.
///
/// For instance the following structure will expect `<titi> <tata> <toto>` in this order only.
/// ```
/// # extern crate clip_core;
/// # #[macro_use] extern crate clip_derive;
/// # use clip_derive;
/// # mod clipv {
/// # pub use clip_derive::*;
/// # pub use clip_core::*;
/// # }
/// use clipv::TryParse;
///
/// ##[derive(TryParse)]
/// struct Toto {
/// titi: String,
/// tata: u8,
/// toto: u8,
/// }
/// ```
///
///
/// # Enum
/// For an enumeration, the first positional parameter corresponds to the Variant (case insensitive
/// match) that should be initialized and the following value are used if for the Variant
/// initialisation.
///
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate clip_derive;
/// # extern crate clip_core;
/// # mod clipv {
/// # pub use clip_derive::*;
/// # pub use clip_core::*;
/// # }
/// use clipv::{parser::{Parsed, TryParse}, TryParse};
///
/// ##[derive(Debug, PartialEq, TryParse)]
/// enum Tata { One, Two, Three }
/// ##[derive(TryParse)]
/// struct Toto {
/// ##[try_parse] tata: Tata,
/// titi: u8
/// }
///
/// fn main() {
/// let arguments = [ "one", "32" ];
/// let result = Toto::try_parse(arguments.iter());
/// assert!(result.is_ok());
/// let Parsed (parsed, _) = result.unwrap();
/// assert_eq!(parsed.tata, Tata::One);
/// assert_eq!(parsed.titi, 32);
/// }
/// ```
///