lerna 2.0.3

Lerna is a framework for elegantly configuring complex applications
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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
//! Default element types for configuration composition

/// Result of resolving a default
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ResultDefault {
    /// Path to the config file
    pub config_path: Option<String>,
    /// Parent config path
    pub parent: Option<String>,
    /// Package for the config
    pub package: Option<String>,
    /// Whether this is a _self_ reference
    pub is_self: bool,
    /// Whether this is the primary config
    pub primary: bool,
    /// Override key for this default
    pub override_key: Option<String>,
}

impl Default for ResultDefault {
    fn default() -> Self {
        Self {
            config_path: None,
            parent: None,
            package: None,
            is_self: false,
            primary: false,
            override_key: None,
        }
    }
}

impl ResultDefault {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_config_path(mut self, path: String) -> Self {
        self.config_path = Some(path);
        self
    }

    pub fn with_package(mut self, package: String) -> Self {
        self.package = Some(package);
        self
    }

    pub fn with_parent(mut self, parent: String) -> Self {
        self.parent = Some(parent);
        self
    }

    pub fn as_self(mut self) -> Self {
        self.is_self = true;
        self
    }

    pub fn as_primary(mut self) -> Self {
        self.primary = true;
        self
    }
}

/// Type of default (config or group)
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DefaultType {
    /// A config file default
    Config,
    /// A config group default
    Group,
    /// Virtual root node
    VirtualRoot,
}

/// Base information for an input default
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InputDefaultBase {
    /// Package for this default
    pub package: Option<String>,
    /// Parent base directory
    pub parent_base_dir: Option<String>,
    /// Parent package
    pub parent_package: Option<String>,
    /// Package header from the config
    pub package_header: Option<String>,
    /// Whether this is the primary config
    pub primary: bool,
}

impl Default for InputDefaultBase {
    fn default() -> Self {
        Self {
            package: None,
            parent_base_dir: None,
            parent_package: None,
            package_header: None,
            primary: false,
        }
    }
}

impl InputDefaultBase {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn update_parent(
        &mut self,
        parent_base_dir: Option<String>,
        parent_package: Option<String>,
    ) {
        if self.parent_package.is_none() || self.parent_package == parent_package {
            self.parent_package = parent_package;
        }
        if self.parent_base_dir.is_none() || self.parent_base_dir == parent_base_dir {
            self.parent_base_dir = parent_base_dir;
        }
    }

    /// Get the package, optionally falling back to package header
    pub fn get_package(&self, default_to_package_header: bool) -> Option<&str> {
        if self.package.is_none() && default_to_package_header {
            self.package_header.as_deref()
        } else {
            self.package.as_deref()
        }
    }
}

/// A config file default
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConfigDefault {
    /// Base default information
    pub base: InputDefaultBase,
    /// Path to the config (relative or absolute)
    pub path: Option<String>,
    /// Whether this default is optional
    pub optional: bool,
    /// Whether this default is deleted
    pub deleted: bool,
}

impl Default for ConfigDefault {
    fn default() -> Self {
        Self {
            base: InputDefaultBase::default(),
            path: None,
            optional: false,
            deleted: false,
        }
    }
}

impl ConfigDefault {
    pub fn new(path: String) -> Self {
        Self {
            path: Some(path),
            ..Self::default()
        }
    }

    pub fn optional(mut self) -> Self {
        self.optional = true;
        self
    }

    pub fn is_self(&self) -> bool {
        self.path.as_deref() == Some("_self_")
    }

    /// Get the config name (basename)
    pub fn get_name(&self) -> Option<&str> {
        self.path.as_ref().map(|p| {
            if let Some(idx) = p.rfind('/') {
                &p[idx + 1..]
            } else {
                p.as_str()
            }
        })
    }

    /// Get the group path (parent directory)
    pub fn get_group_path(&self) -> String {
        let parent_base = self.base.parent_base_dir.as_deref().unwrap_or("");
        let path = self.path.as_deref().unwrap_or("");

        let (path, absolute) = if path.starts_with('/') {
            (&path[1..], true)
        } else {
            (path, false)
        };

        let group = if let Some(idx) = path.rfind('/') {
            &path[..idx]
        } else {
            ""
        };

        if absolute {
            group.to_string()
        } else if parent_base.is_empty() {
            group.to_string()
        } else if group.is_empty() {
            parent_base.to_string()
        } else {
            format!("{}/{}", parent_base, group)
        }
    }

    /// Get the full config path
    pub fn get_config_path(&self) -> String {
        let parent_base = self.base.parent_base_dir.as_deref().unwrap_or("");
        let path = self.path.as_deref().unwrap_or("");

        let (path, absolute) = if path.starts_with('/') {
            (&path[1..], true)
        } else {
            (path, false)
        };

        if absolute {
            path.to_string()
        } else if parent_base.is_empty() {
            path.to_string()
        } else {
            format!("{}/{}", parent_base, path)
        }
    }

    /// Get the default package based on group path
    pub fn get_default_package(&self) -> String {
        self.get_group_path().replace("/", ".")
    }
}

/// A config group default
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GroupDefault {
    /// Base default information
    pub base: InputDefaultBase,
    /// The config group name
    pub group: String,
    /// The selected option value(s)
    pub value: GroupValue,
    /// Whether this default is optional
    pub optional: bool,
    /// Whether this default is deleted
    pub deleted: bool,
    /// Whether this is an override
    pub is_override: bool,
    /// Whether this was added externally
    pub external_append: bool,
    /// Whether the config name was overridden
    pub config_name_overridden: bool,
}

/// Value for a group default (single or multiple)
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum GroupValue {
    Single(String),
    Multiple(Vec<String>),
}

impl GroupValue {
    pub fn as_single(&self) -> Option<&str> {
        match self {
            GroupValue::Single(s) => Some(s),
            GroupValue::Multiple(_) => None,
        }
    }

    pub fn as_multiple(&self) -> Option<&Vec<String>> {
        match self {
            GroupValue::Multiple(v) => Some(v),
            GroupValue::Single(_) => None,
        }
    }

    pub fn is_missing(&self) -> bool {
        match self {
            GroupValue::Single(s) => s == "???",
            GroupValue::Multiple(_) => false,
        }
    }
}

impl Default for GroupDefault {
    fn default() -> Self {
        Self {
            base: InputDefaultBase::default(),
            group: String::new(),
            value: GroupValue::Single(String::new()),
            optional: false,
            deleted: false,
            is_override: false,
            external_append: false,
            config_name_overridden: false,
        }
    }
}

impl GroupDefault {
    pub fn new(group: String, value: String) -> Self {
        Self {
            group,
            value: GroupValue::Single(value),
            ..Self::default()
        }
    }

    pub fn new_multi(group: String, values: Vec<String>) -> Self {
        Self {
            group,
            value: GroupValue::Multiple(values),
            ..Self::default()
        }
    }

    pub fn optional(mut self) -> Self {
        self.optional = true;
        self
    }

    pub fn as_override(mut self) -> Self {
        self.is_override = true;
        self
    }

    /// Get the group path
    pub fn get_group_path(&self) -> String {
        let parent_base = self.base.parent_base_dir.as_deref().unwrap_or("");

        if self.group.starts_with('/') {
            self.group[1..].to_string()
        } else if parent_base.is_empty() {
            self.group.clone()
        } else {
            format!("{}/{}", parent_base, self.group)
        }
    }

    /// Get the config path for a specific value
    pub fn get_config_path(&self, value: &str) -> String {
        let group_path = self.get_group_path();
        if group_path.is_empty() {
            value.to_string()
        } else {
            format!("{}/{}", group_path, value)
        }
    }

    /// Get the default package based on group path
    pub fn get_default_package(&self) -> String {
        self.get_group_path().replace("/", ".")
    }

    /// Get the override key
    pub fn get_override_key(&self) -> String {
        let default_pkg = self.get_default_package();
        let final_pkg = self.get_final_package(false);
        let key = self.get_group_path();

        if default_pkg != final_pkg {
            let pkg = if final_pkg.is_empty() {
                "_global_"
            } else {
                &final_pkg
            };
            format!("{}@{}", key, pkg)
        } else {
            key
        }
    }

    /// Get the final package
    pub fn get_final_package(&self, default_to_package_header: bool) -> String {
        let parent_package = self.base.parent_package.as_deref().unwrap_or("");
        let package = self.base.get_package(default_to_package_header);

        let pkg = match package {
            Some(p) => p.to_string(),
            None => self.get_group_path().replace("/", "."),
        };

        if parent_package.is_empty() {
            pkg
        } else if pkg.is_empty() {
            parent_package.to_string()
        } else {
            format!("{}.{}", parent_package, pkg)
        }
    }

    pub fn is_missing(&self) -> bool {
        self.value.is_missing()
    }
}

/// A node in the defaults tree
#[derive(Clone, Debug)]
pub struct DefaultsTreeNode {
    /// The node content (can be any type of default)
    pub node: DefaultNodeContent,
    /// Child nodes
    pub children: Option<Vec<DefaultsTreeNode>>,
}

/// Content of a defaults tree node
#[derive(Clone, Debug)]
pub enum DefaultNodeContent {
    VirtualRoot,
    Config(ConfigDefault),
    Group(GroupDefault),
}

impl DefaultsTreeNode {
    pub fn virtual_root() -> Self {
        Self {
            node: DefaultNodeContent::VirtualRoot,
            children: None,
        }
    }

    pub fn config(default: ConfigDefault) -> Self {
        Self {
            node: DefaultNodeContent::Config(default),
            children: None,
        }
    }

    pub fn group(default: GroupDefault) -> Self {
        Self {
            node: DefaultNodeContent::Group(default),
            children: None,
        }
    }

    pub fn with_children(mut self, children: Vec<DefaultsTreeNode>) -> Self {
        self.children = Some(children);
        self
    }

    pub fn is_virtual_root(&self) -> bool {
        matches!(self.node, DefaultNodeContent::VirtualRoot)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_result_default() {
        let rd = ResultDefault::new()
            .with_config_path("db/mysql".to_string())
            .with_package("db".to_string())
            .as_primary();

        assert_eq!(rd.config_path, Some("db/mysql".to_string()));
        assert_eq!(rd.package, Some("db".to_string()));
        assert!(rd.primary);
        assert!(!rd.is_self);
    }

    #[test]
    fn test_config_default_is_self() {
        let cd = ConfigDefault::new("_self_".to_string());
        assert!(cd.is_self());

        let cd = ConfigDefault::new("db/mysql".to_string());
        assert!(!cd.is_self());
    }

    #[test]
    fn test_config_default_get_name() {
        let cd = ConfigDefault::new("db/mysql".to_string());
        assert_eq!(cd.get_name(), Some("mysql"));

        let cd = ConfigDefault::new("config".to_string());
        assert_eq!(cd.get_name(), Some("config"));
    }

    #[test]
    fn test_config_default_group_path() {
        let mut cd = ConfigDefault::new("db/mysql".to_string());
        cd.base.parent_base_dir = Some("".to_string());
        assert_eq!(cd.get_group_path(), "db");

        let mut cd = ConfigDefault::new("mysql".to_string());
        cd.base.parent_base_dir = Some("db".to_string());
        assert_eq!(cd.get_group_path(), "db");
    }

    #[test]
    fn test_group_default() {
        let gd = GroupDefault::new("db".to_string(), "mysql".to_string());
        assert_eq!(gd.group, "db");
        assert_eq!(gd.value.as_single(), Some("mysql"));
    }

    #[test]
    fn test_group_default_multi() {
        let gd = GroupDefault::new_multi(
            "db".to_string(),
            vec!["mysql".to_string(), "postgres".to_string()],
        );
        assert_eq!(
            gd.value.as_multiple(),
            Some(&vec!["mysql".to_string(), "postgres".to_string()])
        );
    }

    #[test]
    fn test_group_value_is_missing() {
        let v = GroupValue::Single("???".to_string());
        assert!(v.is_missing());

        let v = GroupValue::Single("mysql".to_string());
        assert!(!v.is_missing());
    }

    #[test]
    fn test_defaults_tree_node() {
        let root = DefaultsTreeNode::virtual_root().with_children(vec![
            DefaultsTreeNode::config(ConfigDefault::new("config".to_string())),
            DefaultsTreeNode::group(GroupDefault::new("db".to_string(), "mysql".to_string())),
        ]);

        assert!(root.is_virtual_root());
        assert!(root.children.is_some());
        assert_eq!(root.children.as_ref().unwrap().len(), 2);
    }
}