typeshare-engine 0.4.1

Behavioral engine for typeshare: parsing, writing, configuration, and everything in between.
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
use std::{
    ffi::{OsStr, OsString},
    str::FromStr,
};

use itertools::Itertools;
use serde::de::{self, value::BorrowedStrDeserializer};

use super::args::{ArgSpec, ArgType, CliArgsSet};

#[derive(Debug, thiserror::Error)]
pub enum ConfigDeserializeError {
    #[error("error from Deserialize type: {0}")]
    Custom(String),

    #[error("command line argument value wasn't valid UTF-8: {0:?}")]
    NonUtf8CliArgument(OsString),

    #[error("error parsing command line value {0:?}")]
    ParseError(
        String,
        #[source] Box<dyn std::error::Error + Send + Sync + 'static>,
    ),
}

impl de::Error for ConfigDeserializeError {
    fn custom<T>(msg: T) -> Self
    where
        T: std::fmt::Display,
    {
        Self::Custom(msg.to_string())
    }
}

/// Deserializer type that combines the values in `config`, which come from a
/// config file, with the values in `args`, which come from the CLI.
pub struct ConfigDeserializer<'a, 'config> {
    config: &'config toml::Table,
    args: &'config clap::ArgMatches,
    spec: &'a CliArgsSet,
}

impl<'a, 'config> ConfigDeserializer<'a, 'config> {
    pub fn new(
        config: &'config toml::Table,
        args: &'config clap::ArgMatches,
        spec: &'a CliArgsSet,
    ) -> Self {
        Self { config, args, spec }
    }
}

impl<'a, 'de> de::Deserializer<'de> for ConfigDeserializer<'a, 'de> {
    type Error = ConfigDeserializeError;
    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_map(ConfigMapAccess {
            state: ConfigMapState::CliValues {
                iterator: self.spec.iter(),
                config: self.config,
                args: self.args,
            },
            spec: self.spec,
            saved_value: None,
        })
    }

    serde::forward_to_deserialize_any! {
        bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
        bytes byte_buf option unit unit_struct newtype_struct seq tuple
        tuple_struct map struct enum identifier
    }

    fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_unit()
    }
}

enum SavedValue<'a> {
    True,
    CliArg(&'a OsStr),
    TomlValue(&'a toml::Value),
}

enum ConfigMapState<'config, ArgsIter> {
    CliValues {
        iterator: ArgsIter,

        config: &'config toml::Table,
        args: &'config clap::ArgMatches,
    },
    TomlValues {
        iterator: toml::map::Iter<'config>,
    },
}

struct ConfigMapAccess<'a, 'config, ArgsIter> {
    state: ConfigMapState<'config, ArgsIter>,
    spec: &'a CliArgsSet,

    saved_value: Option<SavedValue<'config>>,
}

impl<'a, 'de, ArgsIter> de::MapAccess<'de> for ConfigMapAccess<'a, 'de, ArgsIter>
where
    ArgsIter: Iterator<Item = ArgSpec<'a>>,
{
    type Error = ConfigDeserializeError;

    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
    where
        K: de::DeserializeSeed<'de>,
    {
        // Basic algorithm:
        //
        // - Loop over the args spec. For each key, look it up in the matches,
        //   and fall back to the config if it's absent.
        // - Loop over the config, skipping keys that were present in the
        //   args spec.
        loop {
            return match self.state {
                ConfigMapState::CliValues {
                    ref mut iterator,
                    config,
                    args,
                } => {
                    // Get the next argument that we believe might come from
                    // the command line. If there is None, we move on to
                    // parsing all remaining values from the config file.
                    let Some(arg_spec) = iterator.next() else {
                        self.state = ConfigMapState::TomlValues {
                            iterator: config.iter(),
                        };
                        continue;
                    };

                    // First, try to get the value from the command line. The
                    // arg_spec tells us if the command line was supposed to
                    // contain a flag (with no argument) or an argument.
                    let value = match arg_spec.arg_type {
                        ArgType::Bool => {
                            args.get_flag(arg_spec.full_key).then_some(SavedValue::True)
                        }

                        ArgType::Value => args
                            .try_get_raw(arg_spec.full_key)
                            .unwrap_or_else(|_| {
                                panic!(
                                    "argument --{} wasn't recognized by clap; \
                                    this is probably a typeshare bug",
                                    arg_spec.full_key
                                )
                            })
                            .map(|values| {
                                values.exactly_one().unwrap_or_else(|_| {
                                    panic!(
                                        "More than one argument given for --{}; \
                                        clap should have prevented this",
                                        arg_spec.full_key
                                    )
                                })
                            })
                            .map(SavedValue::CliArg),
                    };

                    // If no value was present on the command line for this arg,
                    // fall back to the command line
                    let value =
                        value.or_else(|| config.get(arg_spec.key).map(SavedValue::TomlValue));

                    // If no value was present either on the command line or
                    // in the config file, we skip this key entirely
                    let Some(value) = value else {
                        continue;
                    };

                    // Got a value!
                    self.saved_value = Some(value);
                    seed.deserialize(BorrowedStrDeserializer::new(arg_spec.key))
                        .map(Some)
                }

                // This part is much simpler: iterate over the config file,
                // skipping fields that match those found in the argspec.
                // Once this iterator is exhausted, the deserializing is
                // done!
                ConfigMapState::TomlValues { ref mut iterator } => iterator
                    .find(|(key, _)| !self.spec.contains_key(key))
                    .map(|(key, value)| {
                        self.saved_value = Some(SavedValue::TomlValue(value));
                        seed.deserialize(BorrowedStrDeserializer::new(key))
                    })
                    .transpose(),
            };
        }
    }

    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
    where
        V: de::DeserializeSeed<'de>,
    {
        // We definitely deserialized a key, which means we saved a value to
        // deserialize.
        match self
            .saved_value
            .take()
            .expect("called next_value_seed out of order")
        {
            SavedValue::True => seed.deserialize(de::value::BoolDeserializer::new(true)),
            SavedValue::CliArg(os_str) => {
                seed.deserialize(CliArgumentDeserializer { value: os_str })
            }
            SavedValue::TomlValue(value) => {
                seed.deserialize(super::toml::ValueDeserializer::new(value))
            }
        }
    }
}

/// Deserializer for a value we got from the command line. Generally handles
/// FromStr for this value.
struct CliArgumentDeserializer<'a> {
    value: &'a OsStr,
}

impl<'a> CliArgumentDeserializer<'a> {
    pub fn get_bytes(&self) -> &'a [u8] {
        self.value.as_encoded_bytes()
    }

    pub fn get_str(&self) -> Result<&'a str, ConfigDeserializeError> {
        self.value
            .to_str()
            .ok_or_else(|| ConfigDeserializeError::NonUtf8CliArgument(self.value.to_owned()))
    }

    pub fn parse<T: FromStr>(&self) -> Result<T, ConfigDeserializeError>
    where
        T::Err: std::error::Error + Send + Sync + 'static,
    {
        let s = self.get_str()?;

        s.parse()
            .map_err(|err| ConfigDeserializeError::ParseError(s.to_owned(), Box::new(err)))
    }
}

impl<'de> de::Deserializer<'de> for CliArgumentDeserializer<'de> {
    type Error = ConfigDeserializeError;

    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_borrowed_bytes(self.get_bytes())
    }

    serde::forward_to_deserialize_any! {
        bool bytes byte_buf unit unit_struct seq tuple
        tuple_struct map struct ignored_any
    }

    fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_i8(self.parse()?)
    }

    fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_i16(self.parse()?)
    }

    fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_i32(self.parse()?)
    }

    fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_i64(self.parse()?)
    }

    fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_i128(self.parse()?)
    }

    fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_u8(self.parse()?)
    }

    fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_u16(self.parse()?)
    }

    fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_u32(self.parse()?)
    }

    fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_u64(self.parse()?)
    }

    fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_u128(self.parse()?)
    }

    fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_f32(self.parse()?)
    }

    fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_f64(self.parse()?)
    }

    fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_borrowed_str(self.get_str()?)
    }

    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_borrowed_str(self.get_str()?)
    }

    fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_borrowed_str(self.get_str()?)
    }

    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_some(self)
    }

    fn deserialize_newtype_struct<V>(
        self,
        _name: &'static str,
        visitor: V,
    ) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_newtype_struct(self)
    }

    fn deserialize_enum<V>(
        self,
        name: &'static str,
        variants: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        // BorrowedStrDeserializer elegantly handles string -> unit enum
        // conversions
        de::value::BorrowedStrDeserializer::new(self.get_str()?)
            .deserialize_enum(name, variants, visitor)
    }

    fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        visitor.visit_borrowed_str(self.get_str()?)
    }
}