1use crate::{
2 ArgumentInfo, CliArgumentError, CliDecodeSources, CliNamedSource, CliNamedSources, CliSourceDecode, CliSourceFor,
3 CliSources, CliWordsAlg, Optional, Part, Whole, nested, nested_pattern, source::words,
4};
5use alux_shape::{
6 Product, Sum, Sum2, Sum3, Sum4, Sum5, Sum6, Sum7, Sum8, Sum9, Sum10, Sum11, Sum12, Sum13, Sum14, Sum15, Sum16,
7};
8use std::marker::PhantomData;
9
10#[derive(Clone, Copy, Debug)]
12pub struct ProductSource<Fields, Presence = Whole> {
13 fields: Fields,
14 presence: PhantomData<Presence>,
15}
16
17#[derive(Clone, Copy, Debug)]
19pub struct SumSource<Alternatives, Presence = Whole> {
20 alternatives: Alternatives,
21 presence: PhantomData<Presence>,
22}
23
24impl ProductSource<()> {
25 pub fn new() -> Self {
27 Self { fields: (), presence: PhantomData }
28 }
29}
30
31impl Default for ProductSource<()> {
32 fn default() -> Self {
33 Self::new()
34 }
35}
36
37impl<Fields, Presence> ProductSource<Fields, Presence> {
38 pub fn field<Source>(self, name: &'static str, source: Source) -> ProductSource<(Fields, Source), Presence>
43 where
44 Source: CliNamedSource,
45 {
46 let _: &'static str = name;
47 ProductSource { fields: (self.fields, source), presence: PhantomData }
48 }
49
50 pub fn optional(self) -> ProductSource<Fields, Optional> {
52 ProductSource { fields: self.fields, presence: PhantomData }
53 }
54}
55
56impl SumSource<()> {
57 pub fn new() -> Self {
59 Self { alternatives: (), presence: PhantomData }
60 }
61}
62
63impl Default for SumSource<()> {
64 fn default() -> Self {
65 Self::new()
66 }
67}
68
69impl<Alternatives, Presence> SumSource<Alternatives, Presence> {
70 pub fn or<Source>(self, name: &'static str, source: Source) -> SumSource<(Alternatives, Source), Presence>
75 where
76 Source: CliNamedSource,
77 {
78 let _: &'static str = name;
79 SumSource { alternatives: (self.alternatives, source), presence: PhantomData }
80 }
81
82 pub fn optional(self) -> SumSource<Alternatives, Optional> {
84 SumSource { alternatives: self.alternatives, presence: PhantomData }
85 }
86}
87
88fn parts(name: &'static str, part: fn(&'static str) -> Part, mut arguments: Vec<ArgumentInfo>) -> Vec<ArgumentInfo> {
90 for argument in &mut arguments {
91 argument.part_of = Some(part(name));
92 }
93 arguments
94}
95
96impl<Value, Fields> CliSourceFor<Value> for ProductSource<Fields, Whole>
97where
98 Value: Product,
99 Fields: CliSources<Value::Fields> + CliNamedSources,
100{
101 fn cli_describe(&self, name: &'static str, _: &mut usize, out: &mut Vec<ArgumentInfo>) {
102 out.extend(parts(name, Part::Product, self.fields.cli_arguments(Value::FIELDS)));
103 }
104}
105
106impl<Value, Fields> CliSourceFor<Option<Value>> for ProductSource<Fields, Optional>
107where
108 Value: Product,
109 Fields: CliSources<Value::Fields> + CliNamedSources,
110{
111 fn cli_describe(&self, name: &'static str, _: &mut usize, out: &mut Vec<ArgumentInfo>) {
112 out.extend(parts(name, Part::Product, self.fields.cli_arguments(Value::FIELDS)));
113 }
114}
115
116impl<Value, Fields, Decoder, Error> CliSourceDecode<Value, Decoder, Error> for ProductSource<Fields, Whole>
117where
118 Value: Product,
119 Fields: CliDecodeSources<Value::Fields, Decoder, Error>,
120{
121 fn cli_decode_source(
122 &self,
123 arguments: &mut &[ArgumentInfo],
124 decoder: &Decoder,
125 ) -> Result<Value, CliArgumentError<Error>> {
126 self.fields.cli_decode(take(arguments, Value::FIELDS.len()), decoder).map(Value::from_fields)
127 }
128}
129
130impl<Value, Fields, Decoder, Error> CliSourceDecode<Option<Value>, Decoder, Error> for ProductSource<Fields, Optional>
133where
134 Value: Product,
135 Fields: CliSources<Value::Fields> + CliDecodeSources<Value::Fields, Decoder, Error>,
136 Decoder: CliWordsAlg,
137{
138 fn cli_decode_source(
139 &self,
140 arguments: &mut &[ArgumentInfo],
141 decoder: &Decoder,
142 ) -> Result<Option<Value>, CliArgumentError<Error>> {
143 let fields = take(arguments, Value::FIELDS.len());
144 if fields.iter().all(|field| decoder.cli_words(field).is_empty()) {
145 return Ok(None);
146 }
147 self.fields.cli_decode(fields, decoder).map(|fields| Some(Value::from_fields(fields)))
148 }
149}
150
151fn take<'a>(arguments: &mut &'a [ArgumentInfo], count: usize) -> &'a [ArgumentInfo] {
153 let (parts, rest) = arguments.split_at(count);
154 *arguments = rest;
155 parts
156}
157
158fn chosen<Error>(arguments: &[ArgumentInfo], given: &[bool]) -> Result<Option<usize>, CliArgumentError<Error>> {
160 let mut chosen = given.iter().enumerate().filter(|(_, given)| **given).map(|(index, _)| index);
161 match (chosen.next(), chosen.next()) {
162 (None, _) => Ok(None),
163 (Some(index), None) => Ok(Some(index)),
164 (Some(first), Some(second)) => Err(CliArgumentError::Conflict(arguments[first].name, arguments[second].name)),
165 }
166}
167
168macro_rules! sum {
169 ($sum:ident; $(($source:ident, $alternative:ident, $value:ident, $variant:ident, $index:tt)),+) => {
170 impl<Presence, $($source),+> SumSource<nested!((); $($source)+), Presence> {
171 fn describe<Value, $($alternative),+>(&self, name: &'static str) -> Vec<ArgumentInfo>
173 where
174 Value: Sum<Alternatives = $sum<$($alternative),+>>,
175 nested!((); $($source)+): CliSources<($($alternative,)+)>,
176 {
177 parts(name, Part::Sum, self.alternatives.cli_arguments(Value::ALTERNATIVES))
178 }
179
180 fn chosen_value<Value, Decoder, Error, $($alternative),+>(
182 &self,
183 arguments: &mut &[ArgumentInfo],
184 decoder: &Decoder,
185 ) -> Result<Option<Value>, CliArgumentError<Error>>
186 where
187 Value: Sum<Alternatives = $sum<$($alternative),+>>,
188 Decoder: CliWordsAlg,
189 $($source: CliSourceDecode<$alternative, Decoder, Error>,)+
190 {
191 let arguments = take(arguments, Value::ALTERNATIVES.len());
192 let given = arguments.iter().map(|argument| !words(argument, decoder).is_empty()).collect::<Vec<_>>();
193 let nested_pattern!((); $($value)+) = &self.alternatives;
194 Ok(match chosen(arguments, &given)? {
195 None => None,
196 $(Some($index) => {
197 let value = $value.cli_decode_source(&mut &arguments[$index..], decoder)?;
198 Some(Value::from_alternative($sum::$variant(value)))
199 })+
200 Some(_) => unreachable!("a sum chose an alternative it does not have"),
201 })
202 }
203 }
204
205 impl<Value, $($source, $alternative),+> CliSourceFor<Value> for SumSource<nested!((); $($source)+), Whole>
206 where
207 Value: Sum<Alternatives = $sum<$($alternative),+>>,
208 nested!((); $($source)+): CliSources<($($alternative,)+)> + CliNamedSources,
209 {
210 fn cli_describe(&self, name: &'static str, _: &mut usize, out: &mut Vec<ArgumentInfo>) {
211 out.extend(self.describe::<Value, $($alternative),+>(name));
212 }
213 }
214
215 impl<Value, $($source, $alternative),+> CliSourceFor<Option<Value>> for SumSource<nested!((); $($source)+), Optional>
216 where
217 Value: Sum<Alternatives = $sum<$($alternative),+>>,
218 nested!((); $($source)+): CliSources<($($alternative,)+)> + CliNamedSources,
219 {
220 fn cli_describe(&self, name: &'static str, _: &mut usize, out: &mut Vec<ArgumentInfo>) {
221 out.extend(self.describe::<Value, $($alternative),+>(name));
222 }
223 }
224
225 impl<Value, Decoder, Error, $($source, $alternative),+> CliSourceDecode<Value, Decoder, Error>
226 for SumSource<nested!((); $($source)+), Whole>
227 where
228 Value: Sum<Alternatives = $sum<$($alternative),+>>,
229 nested!((); $($source)+): CliSources<($($alternative,)+)>,
230 Decoder: CliWordsAlg,
231 $($source: CliSourceDecode<$alternative, Decoder, Error>,)+
232 {
233 fn cli_decode_source(
234 &self,
235 arguments: &mut &[ArgumentInfo],
236 decoder: &Decoder,
237 ) -> Result<Value, CliArgumentError<Error>> {
238 let name = match arguments.first().and_then(|argument| argument.part_of) {
240 Some(Part::Sum(name) | Part::Product(name)) => name,
241 None => "",
242 };
243 self.chosen_value::<Value, Decoder, Error, $($alternative),+>(arguments, decoder)?.ok_or(CliArgumentError::Missing(name))
244 }
245 }
246
247 impl<Value, Decoder, Error, $($source, $alternative),+> CliSourceDecode<Option<Value>, Decoder, Error>
248 for SumSource<nested!((); $($source)+), Optional>
249 where
250 Value: Sum<Alternatives = $sum<$($alternative),+>>,
251 nested!((); $($source)+): CliSources<($($alternative,)+)>,
252 Decoder: CliWordsAlg,
253 $($source: CliSourceDecode<$alternative, Decoder, Error>,)+
254 {
255 fn cli_decode_source(
256 &self,
257 arguments: &mut &[ArgumentInfo],
258 decoder: &Decoder,
259 ) -> Result<Option<Value>, CliArgumentError<Error>> {
260 self.chosen_value::<Value, Decoder, Error, $($alternative),+>(arguments, decoder)
261 }
262 }
263 };
264}
265
266sum!(Sum2; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1));
267sum!(Sum3; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2));
268sum!(Sum4; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2), (S3, A3, s3, D, 3));
269sum!(Sum5; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2), (S3, A3, s3, D, 3), (S4, A4, s4, E, 4));
270sum!(Sum6; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2), (S3, A3, s3, D, 3), (S4, A4, s4, E, 4), (S5, A5, s5, F, 5));
271sum!(Sum7; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2), (S3, A3, s3, D, 3), (S4, A4, s4, E, 4), (S5, A5, s5, F, 5), (S6, A6, s6, G, 6));
272sum!(Sum8; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2), (S3, A3, s3, D, 3), (S4, A4, s4, E, 4), (S5, A5, s5, F, 5), (S6, A6, s6, G, 6), (S7, A7, s7, H, 7));
273sum!(Sum9; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2), (S3, A3, s3, D, 3), (S4, A4, s4, E, 4), (S5, A5, s5, F, 5), (S6, A6, s6, G, 6), (S7, A7, s7, H, 7), (S8, A8, s8, I, 8));
274sum!(Sum10; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2), (S3, A3, s3, D, 3), (S4, A4, s4, E, 4), (S5, A5, s5, F, 5), (S6, A6, s6, G, 6), (S7, A7, s7, H, 7), (S8, A8, s8, I, 8), (S9, A9, s9, J, 9));
275sum!(Sum11; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2), (S3, A3, s3, D, 3), (S4, A4, s4, E, 4), (S5, A5, s5, F, 5), (S6, A6, s6, G, 6), (S7, A7, s7, H, 7), (S8, A8, s8, I, 8), (S9, A9, s9, J, 9), (S10, A10, s10, K, 10));
276sum!(Sum12; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2), (S3, A3, s3, D, 3), (S4, A4, s4, E, 4), (S5, A5, s5, F, 5), (S6, A6, s6, G, 6), (S7, A7, s7, H, 7), (S8, A8, s8, I, 8), (S9, A9, s9, J, 9), (S10, A10, s10, K, 10), (S11, A11, s11, L, 11));
277sum!(Sum13; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2), (S3, A3, s3, D, 3), (S4, A4, s4, E, 4), (S5, A5, s5, F, 5), (S6, A6, s6, G, 6), (S7, A7, s7, H, 7), (S8, A8, s8, I, 8), (S9, A9, s9, J, 9), (S10, A10, s10, K, 10), (S11, A11, s11, L, 11), (S12, A12, s12, M, 12));
278sum!(Sum14; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2), (S3, A3, s3, D, 3), (S4, A4, s4, E, 4), (S5, A5, s5, F, 5), (S6, A6, s6, G, 6), (S7, A7, s7, H, 7), (S8, A8, s8, I, 8), (S9, A9, s9, J, 9), (S10, A10, s10, K, 10), (S11, A11, s11, L, 11), (S12, A12, s12, M, 12), (S13, A13, s13, N, 13));
279sum!(Sum15; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2), (S3, A3, s3, D, 3), (S4, A4, s4, E, 4), (S5, A5, s5, F, 5), (S6, A6, s6, G, 6), (S7, A7, s7, H, 7), (S8, A8, s8, I, 8), (S9, A9, s9, J, 9), (S10, A10, s10, K, 10), (S11, A11, s11, L, 11), (S12, A12, s12, M, 12), (S13, A13, s13, N, 13), (S14, A14, s14, O, 14));
280sum!(Sum16; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2), (S3, A3, s3, D, 3), (S4, A4, s4, E, 4), (S5, A5, s5, F, 5), (S6, A6, s6, G, 6), (S7, A7, s7, H, 7), (S8, A8, s8, I, 8), (S9, A9, s9, J, 9), (S10, A10, s10, K, 10), (S11, A11, s11, L, 11), (S12, A12, s12, M, 12), (S13, A13, s13, N, 13), (S14, A14, s14, O, 14), (S15, A15, s15, P, 15));