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, source: Source) -> ProductSource<(Fields, Source), Presence>
40 where
41 Source: CliNamedSource,
42 {
43 ProductSource { fields: (self.fields, source), presence: PhantomData }
44 }
45
46 pub fn optional(self) -> ProductSource<Fields, Optional> {
48 ProductSource { fields: self.fields, presence: PhantomData }
49 }
50}
51
52impl SumSource<()> {
53 pub fn new() -> Self {
55 Self { alternatives: (), presence: PhantomData }
56 }
57}
58
59impl Default for SumSource<()> {
60 fn default() -> Self {
61 Self::new()
62 }
63}
64
65impl<Alternatives, Presence> SumSource<Alternatives, Presence> {
66 pub fn or<Source>(self, source: Source) -> SumSource<(Alternatives, Source), Presence>
68 where
69 Source: CliNamedSource,
70 {
71 SumSource { alternatives: (self.alternatives, source), presence: PhantomData }
72 }
73
74 pub fn optional(self) -> SumSource<Alternatives, Optional> {
76 SumSource { alternatives: self.alternatives, presence: PhantomData }
77 }
78}
79
80fn parts(name: &'static str, part: fn(&'static str) -> Part, mut arguments: Vec<ArgumentInfo>) -> Vec<ArgumentInfo> {
82 for argument in &mut arguments {
83 argument.part_of = Some(part(name));
84 }
85 arguments
86}
87
88impl<Value, Fields> CliSourceFor<Value> for ProductSource<Fields, Whole>
89where
90 Value: Product,
91 Fields: CliSources<Value::Fields> + CliNamedSources,
92{
93 fn cli_describe(&self, name: &'static str, _: &mut usize, out: &mut Vec<ArgumentInfo>) {
94 out.extend(parts(name, Part::Product, self.fields.cli_arguments(Value::FIELDS)));
95 }
96}
97
98impl<Value, Fields> CliSourceFor<Option<Value>> for ProductSource<Fields, Optional>
99where
100 Value: Product,
101 Fields: CliSources<Value::Fields> + CliNamedSources,
102{
103 fn cli_describe(&self, name: &'static str, _: &mut usize, out: &mut Vec<ArgumentInfo>) {
104 out.extend(parts(name, Part::Product, self.fields.cli_arguments(Value::FIELDS)));
105 }
106}
107
108impl<Value, Fields, Decoder, Error> CliSourceDecode<Value, Decoder, Error> for ProductSource<Fields, Whole>
109where
110 Value: Product,
111 Fields: CliDecodeSources<Value::Fields, Decoder, Error>,
112{
113 fn cli_decode_source(
114 &self,
115 arguments: &mut &[ArgumentInfo],
116 decoder: &Decoder,
117 ) -> Result<Value, CliArgumentError<Error>> {
118 self.fields.cli_decode(take(arguments, Value::FIELDS.len()), decoder).map(Value::from_fields)
119 }
120}
121
122impl<Value, Fields, Decoder, Error> CliSourceDecode<Option<Value>, Decoder, Error> for ProductSource<Fields, Optional>
125where
126 Value: Product,
127 Fields: CliSources<Value::Fields> + CliDecodeSources<Value::Fields, Decoder, Error>,
128 Decoder: CliWordsAlg,
129{
130 fn cli_decode_source(
131 &self,
132 arguments: &mut &[ArgumentInfo],
133 decoder: &Decoder,
134 ) -> Result<Option<Value>, CliArgumentError<Error>> {
135 let fields = take(arguments, Value::FIELDS.len());
136 if fields.iter().all(|field| decoder.cli_words(field).is_empty()) {
137 return Ok(None);
138 }
139 self.fields.cli_decode(fields, decoder).map(|fields| Some(Value::from_fields(fields)))
140 }
141}
142
143fn take<'a>(arguments: &mut &'a [ArgumentInfo], count: usize) -> &'a [ArgumentInfo] {
145 let (parts, rest) = arguments.split_at(count);
146 *arguments = rest;
147 parts
148}
149
150fn chosen<Error>(arguments: &[ArgumentInfo], given: &[bool]) -> Result<Option<usize>, CliArgumentError<Error>> {
152 let mut chosen = given.iter().enumerate().filter(|(_, given)| **given).map(|(index, _)| index);
153 match (chosen.next(), chosen.next()) {
154 (None, _) => Ok(None),
155 (Some(index), None) => Ok(Some(index)),
156 (Some(first), Some(second)) => Err(CliArgumentError::Conflict(arguments[first].name, arguments[second].name)),
157 }
158}
159
160macro_rules! sum {
161 ($sum:ident; $(($source:ident, $alternative:ident, $value:ident, $variant:ident, $index:tt)),+) => {
162 impl<Presence, $($source),+> SumSource<nested!((); $($source)+), Presence> {
163 fn describe<Value, $($alternative),+>(&self, name: &'static str) -> Vec<ArgumentInfo>
165 where
166 Value: Sum<Alternatives = $sum<$($alternative),+>>,
167 nested!((); $($source)+): CliSources<($($alternative,)+)>,
168 {
169 parts(name, Part::Sum, self.alternatives.cli_arguments(Value::ALTERNATIVES))
170 }
171
172 fn chosen_value<Value, Decoder, Error, $($alternative),+>(
174 &self,
175 arguments: &mut &[ArgumentInfo],
176 decoder: &Decoder,
177 ) -> Result<Option<Value>, CliArgumentError<Error>>
178 where
179 Value: Sum<Alternatives = $sum<$($alternative),+>>,
180 Decoder: CliWordsAlg,
181 $($source: CliSourceDecode<$alternative, Decoder, Error>,)+
182 {
183 let arguments = take(arguments, Value::ALTERNATIVES.len());
184 let given = arguments.iter().map(|argument| !words(argument, decoder).is_empty()).collect::<Vec<_>>();
185 let nested_pattern!((); $($value)+) = &self.alternatives;
186 Ok(match chosen(arguments, &given)? {
187 None => None,
188 $(Some($index) => {
189 let value = $value.cli_decode_source(&mut &arguments[$index..], decoder)?;
190 Some(Value::from_alternative($sum::$variant(value)))
191 })+
192 Some(_) => unreachable!("a sum chose an alternative it does not have"),
193 })
194 }
195 }
196
197 impl<Value, $($source, $alternative),+> CliSourceFor<Value> for SumSource<nested!((); $($source)+), Whole>
198 where
199 Value: Sum<Alternatives = $sum<$($alternative),+>>,
200 nested!((); $($source)+): CliSources<($($alternative,)+)> + CliNamedSources,
201 {
202 fn cli_describe(&self, name: &'static str, _: &mut usize, out: &mut Vec<ArgumentInfo>) {
203 out.extend(self.describe::<Value, $($alternative),+>(name));
204 }
205 }
206
207 impl<Value, $($source, $alternative),+> CliSourceFor<Option<Value>> for SumSource<nested!((); $($source)+), Optional>
208 where
209 Value: Sum<Alternatives = $sum<$($alternative),+>>,
210 nested!((); $($source)+): CliSources<($($alternative,)+)> + CliNamedSources,
211 {
212 fn cli_describe(&self, name: &'static str, _: &mut usize, out: &mut Vec<ArgumentInfo>) {
213 out.extend(self.describe::<Value, $($alternative),+>(name));
214 }
215 }
216
217 impl<Value, Decoder, Error, $($source, $alternative),+> CliSourceDecode<Value, Decoder, Error>
218 for SumSource<nested!((); $($source)+), Whole>
219 where
220 Value: Sum<Alternatives = $sum<$($alternative),+>>,
221 nested!((); $($source)+): CliSources<($($alternative,)+)>,
222 Decoder: CliWordsAlg,
223 $($source: CliSourceDecode<$alternative, Decoder, Error>,)+
224 {
225 fn cli_decode_source(
226 &self,
227 arguments: &mut &[ArgumentInfo],
228 decoder: &Decoder,
229 ) -> Result<Value, CliArgumentError<Error>> {
230 let name = match arguments.first().and_then(|argument| argument.part_of) {
232 Some(Part::Sum(name) | Part::Product(name)) => name,
233 None => "",
234 };
235 self.chosen_value::<Value, Decoder, Error, $($alternative),+>(arguments, decoder)?.ok_or(CliArgumentError::Missing(name))
236 }
237 }
238
239 impl<Value, Decoder, Error, $($source, $alternative),+> CliSourceDecode<Option<Value>, Decoder, Error>
240 for SumSource<nested!((); $($source)+), Optional>
241 where
242 Value: Sum<Alternatives = $sum<$($alternative),+>>,
243 nested!((); $($source)+): CliSources<($($alternative,)+)>,
244 Decoder: CliWordsAlg,
245 $($source: CliSourceDecode<$alternative, Decoder, Error>,)+
246 {
247 fn cli_decode_source(
248 &self,
249 arguments: &mut &[ArgumentInfo],
250 decoder: &Decoder,
251 ) -> Result<Option<Value>, CliArgumentError<Error>> {
252 self.chosen_value::<Value, Decoder, Error, $($alternative),+>(arguments, decoder)
253 }
254 }
255 };
256}
257
258sum!(Sum2; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1));
259sum!(Sum3; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2));
260sum!(Sum4; (S0, A0, s0, A, 0), (S1, A1, s1, B, 1), (S2, A2, s2, C, 2), (S3, A3, s3, D, 3));
261sum!(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));
262sum!(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));
263sum!(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));
264sum!(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));
265sum!(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));
266sum!(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));
267sum!(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));
268sum!(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));
269sum!(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));
270sum!(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));
271sum!(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));
272sum!(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));