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
use super::helper::ReconstructVal;
use crate::{Config, Record, Span, Value};
use serde::{Deserialize, Serialize};
use std::str::FromStr;

#[derive(Serialize, Deserialize, Clone, Debug, Copy)]
pub enum ErrorStyle {
    Plain,
    Fancy,
}

impl FromStr for ErrorStyle {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_ascii_lowercase().as_str() {
            "fancy" => Ok(Self::Fancy),
            "plain" => Ok(Self::Plain),
            _ => Err("expected either 'fancy' or 'plain'"),
        }
    }
}

impl ReconstructVal for ErrorStyle {
    fn reconstruct_value(&self, span: Span) -> Value {
        Value::string(
            match self {
                ErrorStyle::Fancy => "fancy",
                ErrorStyle::Plain => "plain",
            },
            span,
        )
    }
}

pub(super) fn reconstruct_datetime_format(config: &Config, span: Span) -> Value {
    let mut record = Record::new();
    if let Some(normal) = &config.datetime_normal_format {
        record.push("normal", Value::string(normal, span));
    }
    if let Some(table) = &config.datetime_table_format {
        record.push("table", Value::string(table, span));
    }
    Value::record(record, span)
}