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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! This module contains definitions useful for working directly with [`Hoard`]s.
//!
//! A [`Hoard`] is a collection of at least one [`Pile`], where a [`Pile`] is a single file
//! or directory that may appear in different locations on a system depending on that system's
//! configuration. The path used is determined by the most specific match in the *environment
//! condition*, which is a string like `foo|bar|baz` where `foo`, `bar`, and `baz` are the
//! names of [`Environment`](super::environment::Environment)s defined in the configuration file.
//! All environments in the condition must match the current system for its matching path to be
//! used.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::config::builder::envtrie::{EnvTrie, Error as TrieError};
use crate::env_vars::{Error as EnvError, PathWithEnv};
use crate::hoard::PileConfig;
use crate::newtypes::{EnvironmentName, EnvironmentString, NonEmptyPileName};

type ConfigMultiple = crate::config::hoard::MultipleEntries;
type ConfigSingle = crate::config::hoard::Pile;
type ConfigHoard = crate::config::hoard::Hoard;

/// Errors that may occur while processing a [`Builder`](super::Builder) [`Hoard`] into and
/// [`Config`](crate::config::Config) [`Hoard`](crate::hoard::Hoard).
#[derive(Debug, Error)]
pub enum Error {
    /// Error while evaluating a [`Pile`]'s [`EnvTrie`].
    #[error("error while processing environment requirements: {0}")]
    EnvTrie(#[from] TrieError),
    /// Error while expanding environment variables in a path.
    #[error("error while expanding environment variables in path: {0}")]
    ExpandEnv(#[from] EnvError),
}

/// A single pile in the hoard.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Pile {
    /// Configuration specific to this pile.
    ///
    /// Will be merged with higher-level configuration. If no configuration is specified
    /// (i.e., merging results in `None`), a default configuration will be used.
    pub config: Option<PileConfig>,
    /// Mapping of environment strings to a string path that may contain environment variables.
    ///
    /// See [`PathWithEnv`] for more on path format.
    #[serde(flatten)]
    pub items: BTreeMap<EnvironmentString, PathWithEnv>,
}

impl Pile {
    #[tracing::instrument(level = "debug", name = "process_pile")]
    fn process_with(
        self,
        envs: &BTreeMap<EnvironmentName, bool>,
        exclusivity: &[Vec<EnvironmentName>],
    ) -> Result<ConfigSingle, Error> {
        let Pile { config, items } = self;
        let trie = EnvTrie::new(&items, exclusivity)?;
        let path = trie
            .get_path(envs)?
            .cloned()
            .map(PathWithEnv::process)
            .transpose()?;

        Ok(ConfigSingle {
            config: config.unwrap_or_default(),
            path,
        })
    }

    pub(crate) fn layer_config(&mut self, config: Option<&PileConfig>) {
        PileConfig::layer_options(&mut self.config, config);
    }
}

/// A set of multiple related piles (i.e. in a single hoard).
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct MultipleEntries {
    /// Any custom configuration that applies to all contained files.
    ///
    /// If `None`, a default configuration will be used during processing.
    pub config: Option<PileConfig>,
    /// A mapping of pile name to not-yet-processed [`Pile`]s.
    #[serde(flatten)]
    pub items: BTreeMap<NonEmptyPileName, Pile>,
}

impl MultipleEntries {
    #[tracing::instrument(level = "debug", name = "process_multiple_entries")]
    fn process_with(
        self,
        envs: &BTreeMap<EnvironmentName, bool>,
        exclusivity: &[Vec<EnvironmentName>],
    ) -> Result<ConfigMultiple, super::Error> {
        let MultipleEntries { config, items } = self;
        let items = items
            .into_iter()
            .map(|(pile, mut entry)| {
                tracing::debug!(%pile, "processing pile");
                entry.layer_config(config.as_ref());
                let entry = entry.process_with(envs, exclusivity).map_err(Error::from)?;
                Ok((pile, entry))
            })
            .collect::<Result<_, super::Error>>()?;

        Ok(ConfigMultiple { piles: items })
    }

    pub(crate) fn layer_config(&mut self, config: Option<&PileConfig>) {
        PileConfig::layer_options(&mut self.config, config);
    }
}

/// A definition of a Hoard.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Hoard {
    /// A single anonymous [`Pile`].
    Single(Pile),
    /// Multiple named [`Pile`]s.
    Multiple(MultipleEntries),
}

impl Hoard {
    /// Resolve with path(s) to use for the `Hoard`.
    ///
    /// Uses the provided information to determine which environment combination is the best match
    /// for each [`Pile`] and thus which path to use for each one.
    ///
    /// # Errors
    ///
    /// Any [`enum@Error`] that occurs while evaluating the `Hoard`.
    #[tracing::instrument(level = "debug", name = "process_hoard")]
    pub fn process_with(
        self,
        envs: &BTreeMap<EnvironmentName, bool>,
        exclusivity: &[Vec<EnvironmentName>],
    ) -> Result<crate::config::hoard::Hoard, super::Error> {
        match self {
            Hoard::Single(single) => {
                tracing::debug!("processing anonymous pile");
                Ok(ConfigHoard::Anonymous(
                    single
                        .process_with(envs, exclusivity)
                        .map_err(super::Error::from)?,
                ))
            }
            Hoard::Multiple(multiple) => {
                tracing::debug!("processing named pile(s)");
                Ok(ConfigHoard::Named(
                    multiple.process_with(envs, exclusivity)?,
                ))
            }
        }
    }

    pub(crate) fn layer_config(&mut self, config: Option<&PileConfig>) {
        match self {
            Hoard::Single(pile) => pile.layer_config(config),
            Hoard::Multiple(multi) => multi.layer_config(config),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::hoard::pile_config::{
        AsymmetricEncryption, Config as PileConfig, Encryption, SymmetricEncryption,
    };

    use super::*;

    mod process {
        use std::path::PathBuf;

        use maplit::btreemap;

        use crate::hoard::Pile as RealPile;
        use crate::paths::SystemPath;

        use super::*;

        #[test]
        fn env_vars_are_expanded() {
            let pile = Pile {
                config: None,
                #[cfg(unix)]
                items: btreemap! {
                    "foo".parse().unwrap() => "${HOME}/something".into()
                },
                #[cfg(windows)]
                items: btreemap! {
                    "foo".parse().unwrap() => "${USERPROFILE}/something".into()
                },
            };

            #[cfg(unix)]
            let home = std::env::var("HOME").expect("failed to read $HOME");
            #[cfg(windows)]
            let home = std::env::var("USERPROFILE").expect("failed to read $USERPROFILE");
            let expected = RealPile {
                config: PileConfig::default(),
                path: Some(
                    SystemPath::try_from(PathBuf::from(format!("{home}/something"))).unwrap(),
                ),
            };

            let envs = btreemap! { "foo".parse().unwrap() =>  true };
            let result = pile
                .process_with(&envs, &[])
                .expect("pile should process without issues");

            assert_eq!(result, expected);
        }
    }

    mod serde {
        use maplit::btreemap;
        use serde_test::{assert_de_tokens_error, assert_tokens, Token};

        use super::*;

        #[test]
        fn single_entry_no_config() {
            let hoard = Hoard::Single(Pile {
                config: None,
                items: btreemap! {
                    "bar_env|foo_env".parse().unwrap() => "/some/path".into()
                },
            });

            assert_tokens(
                &hoard,
                &[
                    Token::Map { len: None },
                    Token::Str("config"),
                    Token::None,
                    Token::Str("bar_env|foo_env"),
                    Token::Str("/some/path"),
                    Token::MapEnd,
                ],
            );
        }

        #[test]
        fn single_entry_with_config() {
            let hoard = Hoard::Single(Pile {
                config: Some(PileConfig {
                    encryption: Some(Encryption::Asymmetric(AsymmetricEncryption {
                        public_key: "public key".to_string(),
                    })),
                    ..PileConfig::default()
                }),
                items: btreemap! {
                    "bar_env|foo_env".parse().unwrap() => "/some/path".into()
                },
            });

            assert_tokens(
                &hoard,
                &[
                    Token::Map { len: None },
                    Token::Str("config"),
                    Token::Some,
                    Token::Struct {
                        name: "Config",
                        len: 5,
                    },
                    Token::Str("hash_algorithm"),
                    Token::None,
                    Token::Str("encrypt"),
                    Token::Some,
                    Token::Struct {
                        name: "AsymmetricEncryption",
                        len: 2,
                    },
                    Token::Str("type"),
                    Token::Str("asymmetric"),
                    Token::Str("public_key"),
                    Token::Str("public key"),
                    Token::StructEnd,
                    Token::Str("ignore"),
                    Token::Seq { len: Some(0) },
                    Token::SeqEnd,
                    Token::Str("file_permissions"),
                    Token::None,
                    Token::Str("folder_permissions"),
                    Token::None,
                    Token::StructEnd,
                    Token::Str("bar_env|foo_env"),
                    Token::Str("/some/path"),
                    Token::MapEnd,
                ],
            );
        }

        #[test]
        fn multiple_entry_no_config() {
            let hoard = Hoard::Multiple(MultipleEntries {
                config: None,
                items: btreemap! {
                    "item1".parse().unwrap() => Pile {
                        config: None,
                        items: btreemap! {
                            "bar_env|foo_env".parse().unwrap() => "/some/path".into()
                        }
                    },
                },
            });

            assert_tokens(
                &hoard,
                &[
                    Token::Map { len: None },
                    Token::Str("config"),
                    Token::None,
                    Token::Str("item1"),
                    Token::Map { len: None },
                    Token::Str("config"),
                    Token::None,
                    Token::Str("bar_env|foo_env"),
                    Token::Str("/some/path"),
                    Token::MapEnd,
                    Token::MapEnd,
                ],
            );
        }

        #[test]
        fn multiple_entry_with_config() {
            let hoard = Hoard::Multiple(MultipleEntries {
                config: Some(PileConfig {
                    encryption: Some(Encryption::Symmetric(SymmetricEncryption::Password(
                        "correcthorsebatterystaple".into(),
                    ))),
                    ..PileConfig::default()
                }),
                items: btreemap! {
                    "item1".parse().unwrap() => Pile {
                        config: None,
                        items: btreemap! {
                            "bar_env|foo_env".parse().unwrap() => "/some/path".into()
                        }
                    },
                },
            });

            assert_tokens(
                &hoard,
                &[
                    Token::Map { len: None },
                    Token::Str("config"),
                    Token::Some,
                    Token::Struct {
                        name: "Config",
                        len: 5,
                    },
                    Token::Str("hash_algorithm"),
                    Token::None,
                    Token::Str("encrypt"),
                    Token::Some,
                    Token::Map { len: Some(2) },
                    Token::Str("type"),
                    Token::Str("symmetric"),
                    Token::Str("password"),
                    Token::Str("correcthorsebatterystaple"),
                    Token::MapEnd,
                    Token::Str("ignore"),
                    Token::Seq { len: Some(0) },
                    Token::SeqEnd,
                    Token::Str("file_permissions"),
                    Token::None,
                    Token::Str("folder_permissions"),
                    Token::None,
                    Token::StructEnd,
                    Token::Str("item1"),
                    Token::Map { len: None },
                    Token::Str("config"),
                    Token::None,
                    Token::Str("bar_env|foo_env"),
                    Token::Str("/some/path"),
                    Token::MapEnd,
                    Token::MapEnd,
                ],
            );
        }

        #[test]
        fn test_invalid_glob() {
            assert_de_tokens_error::<PileConfig>(
                &[
                    Token::Struct {
                        name: "Config",
                        len: 5,
                    },
                    Token::Str("hash_algorithm"),
                    Token::None,
                    Token::Str("encrypt"),
                    Token::None,
                    Token::Str("file_permissions"),
                    Token::None,
                    Token::Str("folder_permissions"),
                    Token::None,
                    Token::Str("ignore"),
                    Token::Seq { len: Some(2) },
                    Token::Str("**/valid*"),
                    Token::Str("invalid**"),
                    Token::SeqEnd,
                    Token::StructEnd,
                ],
                "Pattern syntax error near position 6: recursive wildcards must form a single path component",
            );
        }

        #[test]
        fn test_valid_globs() {
            let config = PileConfig {
                ignore: vec![
                    glob::Pattern::new("**/valid*").unwrap(),
                    glob::Pattern::new("*/also_valid/**").unwrap(),
                ],
                ..PileConfig::default()
            };

            assert_tokens::<PileConfig>(
                &config,
                &[
                    Token::Struct {
                        name: "Config",
                        len: 5,
                    },
                    Token::Str("hash_algorithm"),
                    Token::None,
                    Token::Str("encrypt"),
                    Token::None,
                    Token::Str("ignore"),
                    Token::Seq { len: Some(2) },
                    Token::Str("**/valid*"),
                    Token::Str("*/also_valid/**"),
                    Token::SeqEnd,
                    Token::Str("file_permissions"),
                    Token::None,
                    Token::Str("folder_permissions"),
                    Token::None,
                    Token::StructEnd,
                ],
            );
        }
    }
}