|
1 | | -use super::{Format, template::FormatTemplate}; |
| 1 | +use super::{Format, MultiFormat, template::FormatTemplate}; |
2 | 2 | use crate::errors::*; |
3 | 3 | use serde::de::{MapAccess, Visitor}; |
4 | 4 | use serde::{Deserialize, Deserializer, de}; |
| 5 | +use smart_default::SmartDefault; |
5 | 6 | use std::fmt; |
6 | 7 | use std::str::FromStr; |
7 | 8 |
|
@@ -83,6 +84,49 @@ impl Config { |
83 | 84 | } |
84 | 85 | } |
85 | 86 |
|
| 87 | +#[derive(Deserialize, Debug, Clone, SmartDefault)] |
| 88 | +#[serde(untagged)] |
| 89 | +pub enum MaybeMultiConfig { |
| 90 | + #[default] |
| 91 | + Split { |
| 92 | + format: Option<Config>, |
| 93 | + format_alt: Option<Config>, |
| 94 | + }, |
| 95 | + Multiple { |
| 96 | + format: Vec<Config>, |
| 97 | + }, |
| 98 | +} |
| 99 | + |
| 100 | +impl MaybeMultiConfig { |
| 101 | + pub fn with_default(&self, default_full: &str) -> Result<MultiFormat> { |
| 102 | + self.with_defaults(default_full, "") |
| 103 | + } |
| 104 | + |
| 105 | + pub fn with_defaults(&self, default_full: &str, default_short: &str) -> Result<MultiFormat> { |
| 106 | + Ok(MultiFormat::new(match self { |
| 107 | + MaybeMultiConfig::Multiple { format: configs } if configs.is_empty() => { |
| 108 | + vec![Config::default().with_defaults(default_full, default_short)?] |
| 109 | + } |
| 110 | + MaybeMultiConfig::Multiple { format: configs } => configs |
| 111 | + .iter() |
| 112 | + .map(|config| config.with_defaults("", "")) |
| 113 | + .collect::<Result<Vec<_>>>()?, |
| 114 | + MaybeMultiConfig::Split { format, format_alt } => { |
| 115 | + let mut formats = Vec::new(); |
| 116 | + if let Some(format) = format { |
| 117 | + formats.push(format.with_defaults("", "")?); |
| 118 | + } else { |
| 119 | + formats.push(Config::default().with_defaults(default_full, default_short)?); |
| 120 | + } |
| 121 | + if let Some(format_alt) = format_alt { |
| 122 | + formats.push(format_alt.with_defaults("", "")?); |
| 123 | + } |
| 124 | + formats |
| 125 | + } |
| 126 | + })) |
| 127 | + } |
| 128 | +} |
| 129 | + |
86 | 130 | impl FromStr for Config { |
87 | 131 | type Err = Error; |
88 | 132 |
|
|
0 commit comments